I have a FullTextSqlQuery that is returning the URL for a list item. I would like to create a new SPListItem object from that URL. Is there an easy way to do this, or will I need to peel apart the URL to determine the site/list/listitem?
3 Answers
How about the SPWeb.GetListItem method?
It takes in the server relative or absolute URL and returns a SPListItem object.
Try this (not verified)
using(spsite site = new spsite(url))
{
using(spweb web = site.openweb())
{
string itemurl = url.replace(web.url, "");
itemurl = web.serverrelativeurl + itemurl;
splistitem item = web.getlistitem(itemurl);
}
}
-
2to add: they have a great method in SharePoint, SPUtility.ConcatUrls, which allows to assure your urls are combined correctly. it would be a good practice to use it every time you concatenate urls :) Mar 13, 2012 at 20:29
-
I don't think that is what he means... It seems he has a full URL for 1 item and he wants to create a new SPItem with that other item's URL. Mar 13, 2012 at 23:10
Seems that the SPWeb.GetListItem method doesn't work with list elements but only with files..
I have the same problem, I tried this way:
How to get an SPListItem from a Search Query Programmatically
It works but I think it's a little inefficent.