How To Get Information About a SharePoint List Item Using its URL

I recently had the need to get information about an Document Library item based on that item’s URL. Particularly I was looking to know the item’s ID and the List Name/ID that it belong to so that could the use the GetListItems operation of the Lists web services (using SOAP interface). All I had was the URL, and thus my first inclination was to parse it and get the information. Here is a sample of what my URL looked like:

https://mysite/sites/PT/Shared Documents/TestFolder/sub1/sub2

I could remove the site’s Web URL from the it, and then work with the remainder of the URL string and the output of the GetListCollection operation of the Site Data web service to match it up to a list. The logic could get complicated with the amount of parsing that is needed with this approach and the fact that SharePoint allows you to have URL name components that are different from the List name (in my URL above, ‘Shared Documents’ actually maps to a Document Library named ‘Documents’).

There had to be a better way!

I have worked with allot of API’s, both in consumption and even in managing software releases of (proprietary) products where new API’s where being introduced, which gave me insite into the design of such beasts. I have to say, that SharePoint’s API is one of the most complete and robust that I have seen. Certainly they had already covered this use-case and should have an API for it – Right?

My first stop was over at the excellent SPServices library created by Marc Anderson (@sympmarc). Looking at the documentation, I did not find anything, but remembered that it there is at least one web service operation able to return a Site/Web root URL based on “deeper” URL. Marc uses it in his $().SPServices.SPGetCurrentSite utility. Marc does a tremendous job in wrapping as many API methods as possible, but I don’t think he claims to have full coverage – there are just too many! I have seen him add additional ones over the years that I have been following him and SPServices, specially as folks in lively SPServices forums point out what’s missing. So I kept digging.

In looking at the Microsoft documentation for the Site Data web service, there is in fact an operation in that API that will do exactly what I need – SiteData.GetURLSegments. You give it a URL string to an item, and it returns the List and the Item IDs. I used this method with an Document Library item URL and it worked perfectly (O365, SP2010 and SP2013 front-ends) and SP2007.

I found the operation to work with the following types of items:

  • a List Item
  • a Document Library item
  • and a Document Library Folder

It does not work with List Item’s attachment URLs, nor does it work with a Document Library root url (ex. /yousite/Shared Documents/). I have also found that Document Library URL’s that use the Forms do not work. It needs to be a URL directly to an item in the List.

I went ahead and posted my finding on the SPServices forum, along with the use case, and crated my own utility to use this method. The code below, which is available via this Gist) uses jQuery to make the request and return to the caller a Promise which when resolved will provide not only the XML response but also a javascript object with the parsed output.

Advertisements

2 thoughts on “How To Get Information About a SharePoint List Item Using its URL

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s