Dot Net Mafia

Group site for developer blogs dealing with (usually) .NET, SharePoint 2013, SharePoint 2010, Office 365, SharePoint Online, and other Microsoft products, as well as some discussion of general programming related concepts.

This Blog



Corey Roth [MVP]

A SharePoint MVP bringing you the latest time saving tips for SharePoint 2013, Office 365 / SharePoint Online and Visual Studio 2013.

How to: Get the Item Id from a document URL

Since I deal with Enterprise Search a lot, I often want to do things in SharePoint given nothing but a URL.  Unfortunately, there isn't an API call (that I know that can take a URL can conveniently spit out an Item Id).  You can probably use a CAML query, but you would have to know which site collection to start with.  So how do we get the id?  It takes a number of steps, but it is pretty simple.  We first start by opening an SPSite object given the full URL to the item.

using (SPSite siteCollection = new SPSite(url))


    using (SPWeb site = siteCollection.OpenWeb())

This assumes that the code is executing somewhere where opening an SPSite object for that URL is valid.  After that we open a SPWeb object which gives us an object representing the site that contains the document.  Next it is a matter of splitting the URL to get a file and folder URL.

// site url

string siteUrl = site.Url;


// get the position of the last slash so that the string can be split

int lastSlashPosition = url.LastIndexOf('/');


// folder url

string folderUrl = url.Substring(0, lastSlashPosition);


// fileUrl

string fileUrl = url.Substring(lastSlashPosition + 1);

We do this by using LastIndexOf('/) (which of course assumes that the URL contains slashes).  At this point it is easy to get a reference to the file object representing the document.

// get file object

SPFile file = folder.Files[fileUrl];

This gives us a reference to the file which means we can get a reference to the item and then its Id.  Conversely if you are interested in the SPFolder object, you can just use.

// get folder object

SPFolder folder = site.GetFolder(folderUrl);

We will now get the SPListItem object which will give us its Id.

// get the list item

SPListItem item = file.Item;


// get the list item id

int itemId = item.ID;


// get the uniqueId for the list item

Guid uniqueId = item.UniqueId;

Above, I have code to return the internal list item id as well as the unique id.  There are a few steps involved, but it seems to work pretty well.  You will of course want to add proper error handling and check for nulls along the way, but this should be a good start.  This is the same way I get the information I need for the Document Link Handler for Enterprise Search.

Published Nov 24 2008, 03:16 PM by CoreyRoth
Filed under:



How to: Get the Item Id from a document URL | said:

Pingback from  How to: Get the Item Id from a document URL |

November 24, 2008 3:19 PM

Links (11/30/2008) « Steve Pietrek - Everything SharePoint said:

Pingback from  Links (11/30/2008) « Steve Pietrek - Everything SharePoint

November 30, 2008 2:49 PM

Timothy Mills said:

When working with search if you want the Item ID I would just map a property to ows_ID?

January 4, 2010 4:02 AM

nima said:


March 2, 2010 3:59 PM

David said:

Is there a way to do the reverse?  I have the site collection ID's and list ID's, and need to come up with the name of the list quickly, when I need it.

September 22, 2011 6:01 PM

CoreyRoth said:

@David if you have the IDs, then you can use the SPListItem class.  To get the list name, use the ParentList property.

September 27, 2011 6:01 PM

sayitfast said:


February 4, 2013 2:50 PM

Leave a Comment


About CoreyRoth

Corey Roth is an independent SharePoint consultant specializing in ECM, Apps, and Search.
2015 dotnetmafia.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems