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'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;
                    }
                }
            });
share|improve this question

2 Answers 2

up vote 1 down vote accepted

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!

share|improve this answer

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...

share|improve this answer
    
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

 
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.