Get List Item by Guid without List in SharePoint

Warlock picture Warlock · Aug 9, 2012 · Viewed 13.8k times · Source

Could anybody help me to get List Item by unique Id in SharePoint without using List? I know the List Item unique Id, but I haven't information about List. Using Client Object Model I can do the following:

using (SP.ClientContext clientContext = new SP.ClientContext(...))
{
    **SP.List list = clientContext.Web.Lists.GetById(listId);**
    SP.ListItem listItem = list.GetItemById(listItemId);
    clientContext.Load(listItem);
    clientContext.ExecuteQuery();
    ...
}

Unfortunately, in this code I use listId, but the problem is that in my task I don't know listId, I have only ListItem Guid. Since I have unique Id, I believe that there must be a way to get this Item without using any additional information.

Answer

Lukasz M picture Lukasz M · Aug 9, 2012

You've posted a snippet with a Client Object Model, so I suppose it's a preffered object model for the solution. However, if it's acceptable for You, I've found some information on how to achieve this using server side object model, which You may find helpful.

It seems that this can be done using SPWeb.GetFile(Guid itemGuid) if the item exist in the root site.

If you want it to work also for subsites, you can use SPWeb.GetSiteData method to invoke caml retrieving an item as explained in this article: http://www.chakkaradeep.com/post/Retrieving-an-Item-from-the-RootWeb-and-Subwebs-using-its-UniqueId.aspx.

You can also take a look at this thread on SO for some additional information: SharePoint: Get SPListItem by Unique ID.

If you have any issues using those methods, let me know. I'll also look for alternatives for this to run with Client Object Model and I'll post them if I find some, however, it seems to be more difficult to achieve.