0

Is there a possibility to retrieve a file by its document id url without knowing the list in which the file is stored? I know I can do a caml query on a list. But actually the file can be stored in several lists. For performance reasons, I don't want to loop through all lists. I already tried to retrieve the document this way, but actually this does not work.

ClientContext = new ClientContext(SharePointUrl);
ClientContext.Credentials = Credentials;
docUrl = docUrl.GetRelativeUrl(docUrl);
File file = ClientContext.Web.GetFileByServerRelativeUrl(docUrl);
ListItem item = file.ListItemAllFields;
ClientContext.Load(item, i => i.Id, i => i.File);
ClientContext.ExecuteQuery();

The error is

Microsoft.SharePoint.Client.ServerException: 'The object specified does not belong to a list.'

with this relative url

/sites/products/_layouts/DocIdRedir.aspx?ID=PRODUCTS-1-20239
1

Would it help if you got the actual URL of the document instead of the DocIdRedir.aspx URL? Because you could just call the URL and capture the URL it is redirecting to: like

string documentIdUrl = "https://***/_layouts/15/DocIdRedir.aspx?ID=FDQETV2AVKRD-2102554853-1";
HttpWebRequest request = WebRequest.CreateHttp(documentIdUrl);
request.UseDefaultCredentials = true;
request.AllowAutoRedirect = false;
WebResponse response = request.GetResponse();
Console.WriteLine(response.Headers["Location"]);

Note that I did use a local SharePoint environment, which is why I have used the default credentials.

Also, since we disabled the auto-redirect, we don't have to download the entire document, we just obtain the URL SharePoint wants us to redirect to and use it to resolve the actual list-item.

  • Thanks. That's a workaround which helps. But is there really no method to use the DocumentIdUrl to get the file in the csom? – dns_nx Jan 17 at 12:16
  • Actually, the default implementation will use the SharePoint Search first before looking the document up. So you can use CSOM to perform a Keyword search query like this "docid:{0} OR dlcdocid:{0}", inserting the document ID at "{0}". Using the search you can also include various filters to further narrow and improve the search. However, DocIdRedir.aspx also includes a fallback version, used when the SharePoint search won't find any documents (in case they weren't indexed yet). – J. Tihon Jan 18 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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