in SharePoint 2007

Retrieving an Item from the RootWeb and Subwebs using its UniqueId

Sometimes, it is necessary to retrieve an item given its ID (unique identifier). There is always the SPWeb.GetFile(Guid itemGuid) method, but that does not return an item if that item exists in a sub site. The sub site may be a level down or two or ‘n’ levels down.

Using SPWeb.GetSiteData, it is much easier to query for the Item anywhere from your root web.

Below is the code snippet:

SPSiteDataQuery oQuery = new SPSiteDataQuery();

oQuery.Lists = "<Lists BaseType='1'/>";

oQuery.RowLimit = 100;

oQuery.Webs = "<Webs Scope="Recursive" />";


queryBuilder.Append("<Where><Eq><FieldRef Name="UniqueId" />");

queryBuilder.Append("<Value Type="Lookup">");




oQuery.Query = queryBuilder.ToString();


DataTable dtResult = web.GetSiteData(oQuery);


if (dtResult.Rows.Count > 0)


    foreach (DataRow row in dtResult.Rows)


        string listId=row[0].ToString();

        string webId=row[1].ToString();

        string itemId = row[2].ToString();




There is also the SPSite.GetSiteData method if you want to use. If you are planning to use the CrossListQueryCache class, then make sure you read this blog entry!

Write a Comment


Time limit is exhausted. Please reload the CAPTCHA.