pontoNETpt
A comunidade PontoNetPT está direccionada a todos os programadores que trabalhem com a plataforma .NET.
SharePoint 2007 - Getting an SPListItem by ID after a Search

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.

Posted 23-6-2008 18:09 por Miguel Isidoro
Filed under: ,

Comments

Miguel Isidoro wrote re: SharePoint 2007 - Getting an SPListItem by ID after a Search
on 15-7-2008 3:02
Hello Ani,

Can you please explain with a little more detail, what you need to to?
Anonymous wrote re: SharePoint 2007 - Getting an SPListItem by ID after a Search
on 1-7-2009 2:25
I am also facing same problem, whne do I search in infopath form library, it is returning file reference (xml/xsn) of infopath rather then reference liek customized dispform.aspx?id=???.

As you mentioned how can I use your code in customized dispform.aspx, please help me out.

ashishkanoongo at hotmail dot com
Anonymous wrote re: SharePoint 2007 - Getting an SPListItem by ID after a Search
on 1-7-2009 2:25
I have list-item url, how I can get List(or ListName)?
Thanks!
Anonymous wrote re: SharePoint 2007 - Getting an SPListItem by ID after a Search
on 1-7-2009 2:25
Please help. I trying to edit a list item in an infopath form. I am trying to grab the itemid from the url to be able to load that list item into the infopath form. I googled endlessly and cannot find a solution to do this. Please help!
Anonymous wrote re: SharePoint 2007 - Getting an SPListItem by ID after a Search
on 2-7-2009 2:23
I am also facing same problem, whne do I search in infopath form library, it is returning file reference (xml/xsn) of infopath rather then reference liek customized dispform.aspx?id=???.

As you mentioned how can I use your code in customized dispform.aspx, please help me out.

ashishkanoongo at hotmail dot com
Anonymous wrote re: SharePoint 2007 - Getting an SPListItem by ID after a Search
on 2-7-2009 2:23
I have list-item url, how I can get List(or ListName)?
Thanks!
Anonymous wrote re: SharePoint 2007 - Getting an SPListItem by ID after a Search
on 2-7-2009 2:23
Please help. I trying to edit a list item in an infopath form. I am trying to grab the itemid from the url to be able to load that list item into the infopath form. I googled endlessly and cannot find a solution to do this. Please help!
bhsbrx wrote re: SharePoint 2007 - Getting an SPListItem by ID after a Search
on 21-7-2009 0:48

bgVwsR  <a href="nwzxpgilwrte.com/.../a>, [url=http://sltiiynuoefx.com/]sltiiynuoefx[/url], [link=http://myouqhexolod.com/]myouqhexolod[/link], http://ipzyqclkagew.com/

Add a Comment

(requerido)  
(opcional)
(requerido)  
Remember Me?
If you can't read this number refresh your screen
Enter the numbers above:  
Powered by Community Server (Commercial Edition), by Telligent Systems