I am currently trying to retrive a Sharepoint item given it's URL. The URL would be an absolute URL or a relative URL.
To achieve this, I am using the SPWeb.GetListItem(string url) method. I observed a strange behavior when I use this method on my Sharepoint server against a particular site collection.
Assume that I have Site Collection SC and sites S1 and S2 under SC. Each of S1 and S2 have some Announcement items under them. A sample URL for an announcement is shown below
for e.g. Announcement1 - http://myserver.com/sites/SC/S1/Lists/Announcements/1_.000
Announcement2 - http://myserver.com/sites/SC/S2/Lists/Announcements/1_.000
I then use the following code snippet to attempt fetching the Announcement item with it's URL.
using(SPSite spSite = new SPSite("http://myserver.com/sites/SC")
{
foreach(SPWeb web in spSite.AllWebs)
{
SPListItem item = web.GetListItem("http://myserver.com/sites/SC/S1/Lists/Announcements/1_.000");
if(item != null)
{
Console.WriteLine("Item web title : " + web.Title);
}
else
{
Console.WriteLine("Item not found in web : " + web.Title);
}
web.Dispose();
}
}
My expected output would be:
Item web title : S1
Item not found in web : S2
However, my actual output is:
Item web title : SC
Item web title : S1
Item web title : S2
This is strange since given a absolute URL, the item should have been found only within S1 and not within the SC and S2 sites.
Can someone help me understand what I am missing out here?