1

I'm using FullTextSqlQuery to search items on specific list. Query returns results properly. Now I'd like to get SPListItem from search results. As I searched Internet, I found couple of examples, but they all are based on document libraries - getting file and associated with that file SPListItem.

My results contain path to item in such format:

https://<site>/<web>/Lists/<list>/DispForm.aspx?ID=5

I tried to use SPWeb's method GetListItem(path) with server relative url from above path, but returned item is null

So is there any way to get SPListItem from list based on item path returned from search results?

2
  • what you're actually trying to do Commented Dec 28, 2012 at 12:15
  • I want to obtain SPListItem from given search result. Search query is written so it returns results from only one list, and I'd like to get those items.
    – jjczopek
    Commented Dec 28, 2012 at 12:22

1 Answer 1

1

Try getting the SPListItem by the ID provided in the URL.

For example if you have this URL: https://<site>/<web>/Lists/<list>/DispForm.aspx?ID=5 the ID of the item is 5. So you can use the SPList.GetItemById() method and pass the item ID as argument.

EDIT:

There is another way to get the SPListItem by using the SPWeb.GetFile() with the owsItemUrl value as argument. After you get the SPFile object you can use the SPFile.Item property to get the SPListItem.

Example:

If the FullTextSqlQuery is called query:

ResultTableCollection result = query.Execute();
DataTable dt = new DataTable();
if (result.Count > 0)
{
    using (ResultTable relevantResults = result[ResultType.RelevantResults])
    {
        dt.Load(relevantResults, LoadOption.OverwriteChanges);
        foreach (DataRow row in dt.Rows)
        {
            if (row["ContentType"].ToString() == "Item")
            {
                using (SPSite lookupSite = new SPSite(row["owsItemUrl"].ToString()))
                {
                    using (SPWeb web = lookupSite.OpenWeb())
                    {
                        SPFile file = web.GetFile(row["owsItemUrl"].ToString());
                        SPListItem spListItem = file.Item;

                    }
                }
            }
        }
    }
}
4
  • This the think I'd like to avoid, but in the end it might be only solution.
    – jjczopek
    Commented Dec 28, 2012 at 12:24
  • @jjczopek Plese try my new edits, maybe that will help.
    – user3470
    Commented Dec 28, 2012 at 13:14
  • I can't add owsItemUrl to my query, because it throws error. Same thing when I add ContentType. :/
    – jjczopek
    Commented Dec 28, 2012 at 13:55
  • You can use my first suggestion or try my answer to your other question: sharepoint.stackexchange.com/questions/54826/…
    – user3470
    Commented Dec 28, 2012 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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