Get List Item by Guid without List in SharePoint
Asked Answered
A

1

5

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.

Acrylic answered 9/8, 2012 at 17:23 Comment(0)
S
8

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.

Spittle answered 9/8, 2012 at 19:32 Comment(4)
Thank you, Lukasz! I very appreciate your help. In my case using of Client Object Model or web services are preferred solution. With your post now I think, that I will try to create a custom web service on the SharePoint side, that will use SPWeb.GetSiteData. It seems to me like appropriate way. Thank you!Acrylic
Ok, I'm glad to help :). Let me know if you encounter any issues. I still wonder if this can be done using Client Object Model.Spittle
Thanks for accepting my answer :). If you find an answer useful, please also upvote it ;).Spittle
Thanks :). Let me know if you need any help when applying this solution.Spittle

© 2022 - 2024 — McMap. All rights reserved.