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:
- SPListItemCollection.Item(Guid)
- 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.
GetFileandGetFolderThey 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:48listItem.UniqueIdcan be equal tolistItem.File.UniqueId?! Very uncommon usage for property with such name, isn't it? :) – Andrey Markeev Apr 30 '11 at 15:50