Friday, February 27, 2009

Passing list item ID from SharePoint custom context menu item

We had to add a new menu item in the SharePoint ECB menu in one of our form library. We added it using a simple javascript as specified here. We were able to see the newly added menu item in all the SharePoint list items kept in the form library.

The issue was, We had to pass the ID field of the list item as a parameter to a custom .aspx, which we were redirecting while clicking the new menu item. It was required to find, in which item the menu was clicked. After spending some time with this investigation we found its very simple and straight forward.
"currentItemID" was giving the value. We can even retrieve the list item name using "ctx.listName". Here is the code we used

Wednesday, February 25, 2009

Getting the listItems reside inside a SharePoint folder

One of our project's form library was designed with folders due to the 2000 items per view limitation. (Forms submitted in January will be kept inside a folder called "Jan08")
As part of some other requirement we had to access only the items inside a particular folder. Here is the code we found after some time of investigation with the SPQuery object.

try
{
//25/02/2009- FEB Get only the items specific to a folder
SPSite site = new SPSite(
http://mysite:6666/sites/lat);
SPWeb web = site.OpenWeb();
SPList list = web.Lists["DocumentLibrary1"];
SPQuery query = new SPQuery();
SPFolder myFolder = list.RootFolder.SubFolders["MyFolder_001"];
query.Folder = myFolder;
SPListItemCollection folderItems = list.GetItems(query);
foreach (SPListItem item in folderItems)
{
//Do something here with the items belongs to the folder "MyFolder_001"
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}


Hope if it helps for some people.