After making a search to a SharePoint site, it isn’t always easy to get a reference to each item returned in the search. This is because we don’t know the Web and List where each item is located. The following example shows how this can be achieved:

class SiteCollectionSearch
{
    /// <summary>
    /// Search items by title
    /// </summary>
    /// <param name="web"></param>
    /// <returns></returns>
    private static DataTable Search(SPWeb web)
    {
        //search items by title
        StringBuilder query = new StringBuilder();
        query.Append("<Where>");
        query.Append("<Contains>");
        query.Append("<FieldRef Name='Title' />");
        query.Append("<Value Type='Text'>SharePoint</Value>");
        query.Append("</Contains>");
        query.Append("</Where>");
        query.RowLimit = 100;

        SPSiteDataQuery query = new SPSiteDataQuery();
        query.Webs = "<Webs Scope='SiteCollection' />";
        spQuery.Query = query.ToString();

        //perform the search
        DataTable searchResults = web.GetSiteData(query);

        //return results - since ViewFields property is not specified in
        //the query, WebId, ListId and Item Id are automatically returned
        return searchResults;
    }

    /// <summary>
    /// After a search, obtains each item by its Id.
    /// </summary>
    /// <param name="list"></param>
    private static void GetItemsByID()
    {
        SPSite site = new SPSite("http://portal");
        using (SPWeb web = site.OpenWeb())
        {
            //search documents by title
            using (DataTable docs = Search(web))
            {
                //iterate results
                foreach (DataRow row in docs.Rows)
                {
                    //get webId of the current item
                    Guid webId = new Guid(Convert.ToString(row["WebID"]));
                    //get listId of the current item
                    Guid listId = new Guid(Convert.ToString(row["ListID"]));
                    //get id of the current item
                    int itemId = Convert.ToString(row["ID"]);

                    //get the web where the item is located
                    using (SPWeb itemWeb = site.AllWebs[webId])
                    {
                        //get list using the list id
                        SPList list = itemWeb.Lists[listId];

                        SPListItem listItem = null;
                        try
                        {
                            //get item using its id
                            listItem = list.Items[itemId];
                        }
                        catch { }
                        if (listItem != null)
                        {
                            //do something with the item. Example: 
                            //update the title
                            listItem["Title"] = listItem["Title"] 
                                + " Updated";
                            listItem.Update();
                        }
                    }
                }
            }
        }
    }
}

In the previous example, the following actions are being performed:

  • A search is performed to entire site collection for documents containing the word “SharePoint”. As a result of that search, a DataTable instance is returned containing the search results;
  • Each row of the search results is iterated. For each row, three important values are obtained: the item’s ID, ListId and WebId. These are obtained automatically in the result of a SPSiteDataQuery when the ViewFields property is not set;
  • The SPWeb instance where the item is located is obtained using the previously obtained WebId;
  • The SPList instance where the item is located is obtained using the previously obtained ListId;
  • The SPListItem is obtained from the list using the item’s ID;
  • Finally, something is done with the item. For example, updating its title.

Related Articles

To learn why your business should migrate to SharePoint Online and Office 365, click here and here.

If you want to convert your tenant’s root classic site into a modern SharePoint site, click here.

If you are a SharePoint administrator or a SharePoint developer who wants to learn more about how to install a SharePoint farm in an automated way using PowerShell, I invite you to click here and here. The articles use AutoSPInstaller with a SharePoint 2016 farm but AutoSPInstaller support for SharePoint 2019 was already announced!

If you want to learn how to upgrade a SharePoint 2013 farm to SharePoint 2019, click here and here.

If you want to learn all the steps and precautions necessary to successfully keep your SharePoint farm updated and be ready to start your move to the cloud, click here.

If you learn how to greatly speed up your SharePoint farm update process to ensure your SharePoint farm keeps updated and you stay one step closer to start your move to the cloud, click here.

If you want to learn how to upgrade a SharePoint 2010 farm to SharePoint 2016, click here and here.

If you are new to SharePoint and Office 365 and want to learn all about it, take a look at these learning resources.

If you are work in a large organization who is using Office 365 or thinking to move to Office 365 and is considering between a single or multiple Office 365 tenants, I invite you to read this article.

If you or your customers are not ready to move entirely to the Cloud and Office 365, a hybrid scenario could be an interesting scenario and SharePoint 2019 RTM was recently announced with improved hybrid support! To learn all about SharePoint 2019 and all its features, click here.

If you want to know all about the latest SharePoint and Office 365 announcements from SharePoint Conference 2019, click here and here.

Happy SharePointing!

LEAVE A REPLY

Please enter your comment!
Please enter your name here