2

Looking for a way to find a document registered in Sharepoint. I can find document in a documet library or list by following code.

SPSite oSPSite = new SPSite(_serverUrl); SPWeb oSPWeb = oSPSite.OpenWeb(); SPList oSPList; SPListItemCollection oSPListItemCollection; oSPList = oSPWeb.Lists["Listname"]; SPListItem listItem = null; listItem = oSPList.GetItemByUniqueId(new Guid(spGuid)); 

But do i need to iterate trough all list if i dont know in which list the document is registered or is there a more efficient way.

1
  • Do you know the URL of the document? Commented Jan 17, 2012 at 10:44

2 Answers 2

1

If the UniqueId is the only information you have then you have to create a SPSiteDataQuery to retrieve the URL of the document:

SPWeb web = // ... SPSiteDataQuery q = new SPSiteDataQuery(); q.Query = String.Format( "<Where><Eq><FieldRef Name='UniqueId' /><Value Type='Lookup'>{0}</Value></Eq></Where>", spGuid); q.Lists = "<Lists BaseType="1" />"; // restrict to document libraries q.RowLimit = 1; // q.Webs = "<Webs Scope='SiteCollection' />"; add to broaden the search on the whole site collection q.ViewFields = "<FieldRef Name='EncodedAbsUrl' />"; DataTable tbl = web.GetSiteData(q); if (tbl.Rows.Count == 0) throw new FileNotFoundException(...); return tbl.Rows[0]["EncodedAbsUrl"]; 

Then you can load the SPFile (the document) with SPWeb.GetFile(string). If you just need the SPListItem you can access this via SPFile.Item.

Sign up to request clarification or add additional context in comments.

Comments

0

If you don't know in what list the document is placed you will need to iterate over the available SPList objects from the SPWeb.

3 Comments

Thanks for your answer. Think i found a solution. If i know guid of document i can do ....fileItem = oSPWeb.GetFile(new Guid(spGuid)); where spGuid is string guid
Yeah, that would be a better solution if you have the guid :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.