How to: Get the Item Id from a document URL

Posted Monday, November 24, 2008 3:16 PM by CoreyRoth

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.

Filed under:

Comments

# How to: Get the Item Id from a document URL | deleteblog.com

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

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

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

# re: How to: Get the Item Id from a document URL

Monday, January 4, 2010 4:02 AM by Timothy Mills

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

# re: How to: Get the Item Id from a document URL

Tuesday, March 2, 2010 3:59 PM by nima

Thanks

# re: How to: Get the Item Id from a document URL

Thursday, September 22, 2011 6:01 PM by David

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.

# re: How to: Get the Item Id from a document URL

Tuesday, September 27, 2011 6:01 PM by CoreyRoth

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

msdn.microsoft.com/.../microsoft.sharepoint.splistitem_properties.aspx

# re: How to: Get the Item Id from a document URL

Monday, February 4, 2013 2:50 PM by sayitfast

Thanks!!!

Leave a Comment

(required)
(required)
(optional)
(required)