views:

1252

answers:

2

Sometimes users require to change information in SharePoint list item that is not editable for them, for instance, a field that is hidden in edit form (in my case it was the records number).

I decided to create a small Windows GUI application that the administrator would run on the server and make the requested change. However, the simplest scenario to get an instance of a SPListItem I found was:

  • the admin enters the URL of the root site
  • an SPSite ojbect is created, using the given URL: SPSite oSite=new SPSite(this.txtURL.text);
  • admin enters the relative URL of the reqired web
  • an SPWeb object is created as SPWeb oWeb = oSite.OpenWeb(this.txtWebUrl.text);
  • a dropdown box is filled with all the list titles from oWeb.Lists
  • admin chooses a list from the listbox and enters the ID of the requested item;
  • the needed SPListItem is found as oWeb.Lists[this.lstAllLists.selectedValue].GetItemById(this.txtItemId.value);

This is a very long path and administrators do no like to do the typing, clicking and waiting.
They would like to copy the URL of the listitem's display form (from the web browser or somebody's email), paste it into the update tool, then just click "Find it!".

I need hints for how this can be done.

I know I could probably parse the URL with a regex, since it's typically in the form of http://server/sites/[somesite]/[someweb/somesubweb]/lists/[somelist]/forms/dispform.aspx?ID=[123], but variations exist - for instance, http://[server]/[DocumentLibrary]/Forms/RenamedDispForm.aspx?ID=[1234] has quite different structure than the first example.

So, the question is - is there some easy way to find an SPListItem by it's URL? Reconstructing an SPContext from the URL would be great.

EDIT: Just found out that it is possible to construct a valid SPSite object by passing it a much longer URL:

Dim oSite as New SPSite("http://server/sites/site/Lists/test/DispForm.aspx?ID=136")
+1  A: 

I found myself doing something very similar (although with SPAuditEntry objects). The solution I came up with involved parsing the URL to figure out the SPSite and SPWeb.

Since SPSite takes a longer URL, you may also be able to open the right SPWeb as well (using site.OpenWeb()). I decided to use the SPSite.AllWebs.Names to figure out exactly which SPWeb the item was in (extracting parts of the URL and then doing a binary search against my collection of SPWeb names). I'm guessing you would then have to use SPWeb.Lists to determine which list or library it was in.

Kit Menke
+2  A: 

Found a solution myself, the trick I did not know is that if you use a long URL in the constructor of the SPSite, it gives you the SPWeb object with the "deepest possible" address that matches your url (described here: http://msdn.microsoft.com/en-us/library/ms473155.aspx)

Still, I have to loop through all the lists to find out which list has the required URL. A simple function that does what I need:

SPListItem GetItemByUrl(string sUrl) {
    //no problem to construct a site with longer url than the actual site root
    SPSite oSite = new SPSite(sUrl);
    //this now opens a web /sites/firstsite/subsite
    SPWeb oWeb = oSite.OpenWeb();

    //as far as I've seen, it always has a trailing slash, however...
    int iLen = oWeb.Url.EndsWith("/") ? oWeb.Url.Length : oWeb.Url.Length + 1;

    //this is the part we still need to resolve
    //will look something like "lists/somelist/Forms/DispForm.aspx?ID=1"
    //or maybe "lists/somelist/DispForm.aspx?ID=1"
    string sUrlEnd = sUrl.Substring(iLen);

    SPList oActualList = null;
    foreach (SPList oList in oWeb.Lists)
    {
        if (sUrlEnd.StartsWith(oList.RootFolder.Url))
        {
            oActualList = oList;
            break;
        }
    }
    if (oActualList == null)
    {
        //if there is no list where the URL is the same as provided
        //we  can't hope to find the item
        return null;
    }
    //we need to find the ID from the URL
    //probably could do with regex, but splitting a 
    //query string into particles is not that hard.
    string[] sParts = (new Uri(sUrl)).Query.TrimStart('?').Split('&');
    int iID = 0; //maybe there is no ID parameter in the query?
    foreach (string sPart in sParts)
    {
        string[] sParamParts = sPart.Split('=');
        if (string.Compare(sParamParts[0], "ID", true) == 0)
        {
            if (int.TryParse(sParamParts[1], out iID) == true)
            {
                break;  //we're sure we have found the ID and it is a number
            }
        }
    }
    if (iID > 0)
    {
        return oActualList.GetItemById(iID);
    }
    else {
        return null;
    }
}
naivists
Thanks, very helpful! Going to use this to find a list by URL. One thing to add to this function is to make sure to dispose any SPSite or SPWeb object you instantiate, as long as it's not the Current object. (http://msdn.microsoft.com/en-us/library/aa973248.aspx)
lividsquirrel