Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a list of 6000 listitem GUIDs. I need to check if each item still exist on the Web.

Previously, I was using the SPWeb.GetFile(GUID) method to check the item.

But now I need to convert that process usinig the ClientObjectModel. I cannot find a way to retrieve the item using the GUID.

I even tried from the "List" object. And can't find a way to retrieve the item from its GUID neither.

Please help! :(

share|improve this question

2 Answers 2

up vote 2 down vote accepted

I'm afraid the only way is to use CAML query. Even worse, since you don't have a SPSiteDataQuery alternative in Client Object Model, you can't perform cross-site queries, so you have to query each list separately.

Example code for quering a list with SharePoint 2010 Client Object Model:

string siteUrl = "http://MyServer/sites/MySiteCollection";
string listTitle = "your list title here";
string guid = "put-your-guid-here";

ClientContext clientContext = new ClientContext(siteUrl);
Web site = clientContext.Web;
List targetList = site.Lists.GetByTitle(listTitle);

CamlQuery query = new CamlQuery();
query.ViewXml = String.Format("<View><Query><Where><Eq><FieldRef Name='UniqueId'/><Value Type='Guid'>{0}</Value></Contains></Where></Query></View>", guid);
ListItemCollection collListItem = targetList.GetItems(query);

clientContext.Load(collListItem);
clientContext.ExecuteQuery();

bool itemIsFound = (collListItem.Count == 1);

(the code has been tested and works)

share|improve this answer
    
Woahhh that's nasty :P. It will take forever to go through my 6000 items. Anyways since I don't have the choice, I will do it that way! :) thanks –  danbord Feb 3 '12 at 18:29
1  
@danbord, probably you should consider creating a custom web service which leverages the SPWeb.GetFile method - of course if you have a possibility (i.e. permissions) of doing this. I think this will work much faster. –  Andrey Markeev Feb 3 '12 at 18:32
    
Good suggestion! Thanks. –  danbord Feb 3 '12 at 18:50

As Andrey pointed out there is no SPList.GetItemByUniqueId Method alternative in CSOM API.

You could utilize the following extension method to load the list item by GUID in CSOM:

static class ListExtensions
{
    /// <summary>
    /// Load List Item by Guid 
    /// </summary>
    /// <param name="list"></param>
    /// <param name="itemGuid"></param>
    /// <returns></returns>
    public static ListItem LoadItemByUniqueId(this List list, Guid itemGuid)
    {
        var context = list.Context;
        var query = new CamlQuery
        {
            ViewXml = String.Format("<View><RowLimit>1</RowLimit><Query><Where><Eq><FieldRef Name='UniqueId'/><Value Type='Guid'>{0}</Value></Contains></Where></Query></View>", itemGuid),
        };
        var items = list.GetItems(query);
        context.Load(items);
        context.ExecuteQuery();
        return items.Count > 0 ? items[0] : null;
    }
}

Key points:

  • RowLimit property is specified in query to improve performance
share|improve this answer
    
Please fix the ViewXml in the extension method: - fieldname: "GUID" - closing tag for <Eq> --> </Eq> instead of </Contains> final ViewXml will be: 'ViewXml = String.Format("<View><RowLimit>1</RowLimit><Query><Where><Eq><FieldRef Name='GUID'/><Value Type='Guid'>{0}</Value></Eq></Where></Query></View>", itemGuid)' –  Koen van der Linden Aug 27 at 8:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.