Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm missing something that must be so simple that it's not even worth asking... but I have an object ID (GUID)... How do I figure out the document it points to?

For example I have a GUID of 137DA01F-9AFD-5d9d-80C7-02AF85C822A8 and need to know what file/url it points to.

share|improve this question

3 Answers 3

up vote 21 down vote accepted

If you know the SPWeb but not the document library, you can use SPWeb.GetFile:

SPFile file = web.GetFile(guid);
share|improve this answer
    
+1 Best solution! Haven't seen this method yet. Just tested: it works perfectly. Interesting thing: SPListItem.UniqueId and SPListItem.File.UniqueId are the same guids. –  Andrey Markeev Apr 30 '11 at 13:42
    
@omlin - Thanks! GetFile and GetFolder They also have overloads with URLs, which can be very useful - it lets you access files ad folders without getting the library first. –  Kobi Apr 30 '11 at 13:48
    
Yep, actually I knew about these methods, but only about Url overloads :) And even if I have seen GUID overloads, how could one suppose, that listItem.UniqueId can be equal to listItem.File.UniqueId?! Very uncommon usage for property with such name, isn't it? :) –  Andrey Markeev Apr 30 '11 at 15:50
    
Thanks Kobi. I'll give it a shot as soon as I can! –  Iunknown Apr 30 '11 at 15:52

Simpliest case

Consider you know that document is stored in standard "Shared Documents" library from the Team Site Template, you should use this code:

var library = web.GetListFromUrl("http://site/Shared%20Documents/Forms/AllItems.aspx");
var file = library.Items[new Guid("137DA01F-9AFD-5d9d-80C7-02AF85C822A8")].File;
// file.Url - site-relative url of the file
// file.OpenBinary() - get file contents (returns byte[])

MSDN references:

  1. SPListItemCollection.Item(Guid)
  2. SPFile.OpenBinary (with sample code)

Library with folders

In more complex case, when you have some folders in your document library, probably the best way will be to use the CAML query for this purpose:

var query = new SPQuery();
query.ViewAttributes = "Scope=\"Recursive\"";
query.View = 
@"<Where>
  <Eq>
    <FieldRef Name=\"UniqueId\">
    <Value Type='Text'>137DA01F-9AFD-5d9d-80C7-02AF85C822A8</Value>
  </Eq>
</Where>";
var items = library.GetItems(query);

Library unknown

If you even don't know in which library the file is stored, you should go through all the libraries of the site:

 foreach (var library in web.Lists)
 {
     if (!library.Hidden && library is SPDocumentLibrary)
     {
         // ... try to get the file and so on
     }
 }

Document IDs

Also, you should surely read about Document IDs feature, which allows you to reference a document from anywhere:

http://blogs.technet.com/b/blairb/archive/2009/10/20/new-document-id-feature-in-sharepoint-2010.aspx

Note: Your Guid, of course, is not a document ID.

share|improve this answer

There's the SPList.GetItemByUniqueId function, but if you don't know which list to search you're out of luck.

If you're using SharePoint 2010, look at the Document ID functionality (which attaches a unique ID to a list item and then uses Search to find the item). Depending upon your requirements, you might consider implementing "FindItemInSiteCollectionByUniqueId" using a similar approach to Document IDs, making the guid a Managed Property and conducting a search via code.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.