Finding SharePoint list item by its display form's URL

naivists picture naivists · Jan 4, 2010 · Viewed 14.9k times · Source

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")

Answer

naivists picture naivists · Jan 5, 2010

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:

UPDATE @ 2012-08-01:

  • no need to loop through list collection, there is a GetList method in SPWeb object.
  • instead of doing regex on URLs, one can use HttpUtility.ParseQueryString method

Hence the code now looks like this:

Function GetItemByUrl(spUrl As String) As SPListItem
    'A site object does not care about additional parameters after site's URL
    Dim oSite As New SPSite(spUrl)
    'This returns the deepest SPWeb it can find
    Dim oWeb As SPWeb = oSite.OpenWeb()
    'here we parse out the ID parameter
    Dim oUri As New Uri(spUrl)
    'HttpUtility is from System.Web namespace
    Dim oQueryParams As System.Collections.Specialized.NameValueCollection 
    oQueryParams = HttpUtility.ParseQueryString(oUri.Query)

    Dim sParamval As String = oQueryParams.Get("ID")
    If (sParamval.Length <= 0) Then
        Return Nothing
    End If

    'This is how we get the list
    Dim oCurrentList As SPList = oWeb.GetList(spUrl)
    'And finally fetching the item
    Dim oListItem As SPListItem = oCurrentList.GetItemById(Int32.Parse(sParamval))
    Return oListItem
End Function