2

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?

1
  • I can't imagine there would be any other way than to dissect the URL so that you can get the list. Like SPList list = web.GeList("/Lists/ListPath/"); It's only able to return the full URL? Mar 13, 2012 at 18:57

3 Answers 3

2

How about the SPWeb.GetListItem method?

It takes in the server relative or absolute URL and returns a SPListItem object.

0

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);
}
}
2
  • 2
    to 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
0

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.

Your Answer

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

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