0

I'm querying my Search Server and getting some results... How can i get the SPListItem linked to that object?

I tried with many ways, but every method fails..

Here's my code:

            string fullsite = SPContext.Current.Web.Url + "/sites/ReplyCorp";

            Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite scSite = new SPSite(fullsite))   //Collego impersonificando l'utente
                {
                    using (SPWeb webRoot = scSite.OpenWeb())
                    {
                        KeywordQuery keywordQuery = new KeywordQuery(scSite);
                        keywordQuery.ResultTypes = ResultType.RelevantResults;
                        keywordQuery.RowLimit = 1;
                        keywordQuery.QueryText = "contentId:" + updateContentObj.contentId;
                        ResultTableCollection searchResultTables = keywordQuery.Execute();

                        ResultTable searchResult = searchResultTables[ResultType.RelevantResults];


                        searchResult.Read();                            
                        SPListItem listItem = null; 

                        string url = searchResult["Path"].ToString();
                        Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate()
                        {
                            using (SPSite site = new SPSite(url))
                            {
                                using (SPWeb web = site.OpenWeb())
                                {
                                    debug += "MY URL" + web.Url + "PPPPP" + url;

                                    listItem = web.GetListItem(url);
                                }
                            }
                        });

                        debug += listItem.Name;
                    }
                }
            });
  • Why do you have Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate(){}); twice? Just once is enough – user42233 May 11 '15 at 14:25
3

In SharePoint 2013 ListID, ListItemID properties can be added to the result set of KeywordQuery, SearchExecutor:

var properties = keywordQuery.SelectProperties;

properties.Add("ListID");
properties.Add("ListItemID");

See: Overview of crawled and managed properties in SharePoint Server 2013 https://technet.microsoft.com/en-us/library/jj219630.aspx

2

I find a way to get that SPListItem... I modified my code this way:

 Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite scSite = new SPSite(fullsite))  
                {
                    using (SPWeb webRoot = scSite.OpenWeb())
                    {
                        scSite.AllowUnsafeUpdates = true;
                        webRoot.AllowUnsafeUpdates = true;
                        SPListItem listItem = null;//= GetItemByContentId(updateContentObj.contentId);

                        KeywordQuery keywordQuery = new KeywordQuery(scSite);
                        keywordQuery.ResultTypes = ResultType.RelevantResults;
                        keywordQuery.RowLimit = 1;
                        keywordQuery.QueryText = "contentId:" + updateContentObj.contentId;
                        ResultTableCollection searchResultTables = keywordQuery.Execute();

                        ResultTable searchResult = searchResultTables[ResultType.RelevantResults];

                        searchResult.Read();

                        string url = searchResult["Path"].ToString();

                        Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(delegate()
                        {
                            using (SPSite site = new SPSite(url))
                            {
                                using (SPWeb web = site.OpenWeb())
                                {
                                    web.AllowUnsafeUpdates = true;
                                    site.AllowUnsafeUpdates = true;

                                    Match m = Regex.Match(url + "&Pippo", "ID=[0-9]+");
                                    int listItemID = int.Parse(Regex.Match(m.Value, "[0-9]+").Value);
                                    listItem = web.Lists["Contenuti"].Items.GetItemById(listItemID);
                                        }
                            }
                        });

                        debug += listItem.Name;
                    }
                }
            });
            return new ResponseObject() { Message = debug, SPResultStatus = ResponseObjectSPResultStatus.SUCCESS };
        }

In simple words i get the ID and the LIST from the URL and then i retrieve manualli the item!

0

ur doing searches not related to listitems... Your searching your whole site, so it can return lists, listitems, ... I'd use another way if you want to look for SPListItems...

  • But I must search across the whole site collection.. How can i do to "filter" just to SPListItem and look for that? Thank you – Ziba Leah Feb 9 '12 at 9:43
  • Your comment below is how it's done. (atleast with SPSiteDataQuery, I never user KeyWordQuery) – Frederik P. Feb 9 '12 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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