Reputation: 1261
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.
Upvotes: 2
Views: 464
Reputation: 14880
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
.
Upvotes: 1
Reputation: 610
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.
Upvotes: 0