Wednesday, December 31, 2008

Create a new sharepoint feature: Add a custom action to Task list item's drop down menu.

Normally, when we click on a sharepoint list item, from the drop down menu we can see: "View item, Edit item, Manage properties, Delete Item..." Lets see how can we add a new item to this drop down menu...

For my work, I need to add a custom action named "Change Status" to change the status of a Task list item. So, I modified the "EditForm.aspx" and created a new form named "ApproveStatus3.aspx" by using sharepoint designer. This page only edits the Status field of that item. Then I followed these steps:

1. Create a new folder, called "ChangeStatus", in C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES
2. Create an empty Xml file, called "feature.xml" in the "ChangeStatus" folder or you can copy and paste the same "feature.xml" file from other feature folder
3. Empty the feature.xml file and add the following content

<?xml version="1.0" encoding="utf-8" ?>
<Feature Title="Change Status"
Description="Changes only the status of the leave letter."
Scope="Site"
Id="C0B8CB6D-6DD9-4c89-A1DC-E97A4745875C" xmlns="http://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location="Elements.xml" />
</ElementManifests>
</Feature>

4. Create an empty Xml file, called "Elements.xml" in the "ChangeStatus" folder or you can copy and paste the same "feature.xml" file from other feature folder
5. Empty the file and add the following content


<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

<CustomAction Id="ApprovalList.ChangeStatus"
RegistrationType="List"
RegistrationId="107"
Location="EditControlBlock"

Sequence="100"
Title="Change Status"

ImageUrl="/_layouts/images/TaskPane.gif">

<UrlAction Url="http://svradm002:11226/lms/Lists/ApprovalList/ApproveStatus3.aspx?ID={ItemId}&Source=http%3A%2F%2Fsvradm002%3A11226%2Flms%2FLists%2FApprovalList%2FMyItems%2Easpx" />
</CustomAction>
</Elements>

6. Run command prompt, and nevigate to sharepoint's BIN folder... cd C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN Press enter.
7. Install the new feature by running the following command:
Stsadm –o installfeature –name changestatus
8. Activate the new feature by running the following command:
Stsadm –o activatefeature -name changestatus -url
http://svradm002:11226/lms/

Here, registration id="107" menas this feature will be added with the drop down menu for items in task lists.

sequence="100" will probably place this item at the top most position of the drop down list. Increase the sequence number if you want to place it somewhere below.

in the url section,
http://svradm002:11226/lms/Lists/ApprovalList/ApproveStatus3.aspx? is the location where I saved my custorm editing form. ID={ItemId} passes the current item that has been clicked on. Source=http%3A%2F%2Fsvradm002%3A11226%2Flms%2FLists%2FApprovalList%2FMyItems%2Easpx holds the return address when the editing will be done. normally you can pass the address for "allitems.aspx" page from the browser's address box.




Thursday, December 4, 2008

Retrieve the user name from a sharepoint's "Person or Group" field.

Previously, in VPC, to get a name from a "person or group" field from a sharepoint list I had to use this code (in C#):

string onBehalfOf = properties.AfterProperties["On_x0020_Behalf_x0020_Of"].ToString();
int startingIndex = onBehalfOf.IndexOf("#") + 1;
int endingIndex = onBehalfOf.Length - 4;
string onBehalfName = onBehalfOf.Substring(startingIndex, endingIndex);


and I got the name. "On_x0020_Behalf_x0020_Of" is the column (field) name of a sharepoint list


"OnBehalfof" string gets value like this: "11;#M. Andalibur Rahman". So, by doing a substring by "#" u can get the name portion. But in the virtual Server, the "OnBehalOf" string gets only the number, "11", and the program crashes when it wants to do the substring operation. So, I had to figure out the solution by spending almost 1 day. And the solution is:

string currentValue= properties.AfterProperties["On_x0020_Behalf_x0020_Of"].ToString();
SPFieldUser userField= (SPFieldUser)properties.OpenWeb().Lists[properties.ListTitle].Fields.GetField ("On_x0020_Behalf_x0020_Of");
SPFieldUserValue fieldValue= (SPFieldUserValue)userField.GetFieldValue(currentValue);
string onBehalfName = fieldValue.User.Name;


U got the user name.
fieldValue.User will give you SPUser object
fieldValue.User.Id will give you the user Id, 11

Wednesday, December 3, 2008

Just joined...

"Helo world!"
Just joined blogspot... dont know why. hoping to have some good time here.