locked
Rest API to get a file using the GUID RRS feed

  • Question

  • Is there a REST endpoint for sharepoint to get a file based on the UniqueId of the file.

    Also do unique id remain the same if a file is moved from one folder to another

    Thanks

    Atul


    • Edited by Atul Barve Friday, December 20, 2013 4:52 PM
    Friday, December 20, 2013 4:36 PM

All replies

  • No, there is no REST API for that. 

    In most cases, no, the ID will not remain the same.  What is the method by which you are "moving" the file?


    Chris Givens CEO, Architecting Connected Systems Blog Twitter

    Saturday, December 21, 2013 7:58 PM
  • Hi Atul Barve,

    If you mean the document unique id with Document ID Service Feature, through Site Sittings->Site Collection Features->Document ID Features.

    Then, the field is included in REST API, you can get it through “OData__dlc_DocId”.

    For example, first get the items using http://siteurl/_api/web/lists/getByTitle(‘libraryname’)/items

    This will get all items in the list, then view the page in XML format or JSON format, we can see the item fields, include:
    <d:OData__dlc_DocId>AWMJNX7ZDCK7-1-4</d:OData__dlc_DocId><d:OData__dlc_DocIdUrl m:type="SP.FieldUrlValue">

    “OData__dlc_DocId” is represent the document id field.

    Document ID is unique in the site collection, the value will not change when you move the document from one library into another library.

    Thanks,


    Qiao Wei
    TechNet Community Support

    Thursday, December 26, 2013 7:53 AM
  • Hi Qiao,

    I understand this is an old thread, but this closely matches with our requirement. In our situation, we know the value stored in "OData__dlc_DocId", but we don't which List contains this document. We have around 40 Lists, at the moment it seems to be a very time consuming task of looping through these 40 lists and "try to" get the document and exit the loop once the document is found. So, if the document exists in one of the 1st few Lists, then the response is quicker, otherwise the delay is based on where the document is located.

    To get the document, this is logic we use at present and would like to find out if there is a direct way of accessing the file using "OData__dlc_DocId" without having to loop through all the Lists.

    Step 1: Get all Lists using /_api/web/Lists. At present this returns 40 lists, but it may grow as our document library grows.

    Step 2: Loop through these lists and fire a query using /_api/web/Lists(guid'<guid_from_step1>')/GetItemById(<last_numeric_digits_from_OData__dlc_DocId>)

    Step 3: Check if the OData__dlc_DocId returned by the API is same as the OData__dlc_DocId for which we ran this query. If yes, then break the loop and return List's GUID, otherwise repeat Step 2 and 3 for the rest of the 39 Lists until the document is found.

    Accessing via GetFileByServerRelativeURL seems to be a quicker one, but unfortunately we have been given only OData__dlc_DocId as an entry point, so using this we need to find get all the relevant information and store in our own database for future queries. So in our case, any subsequent usage is faster as we use GetFileByServerRelativeURL, but the initial query to get all the data from OData__dlc_DocId seems to be a time consuming one. Do you know of any better alternative to retrieve the item / file info based on OData__dlc_DocId without looping through the Lists? Thank you in advance.

    Monday, January 11, 2016 1:37 PM
  • I know this is very old question. However I was unable to find any solution but found my own. In CSOM you actually have a method for this:

    ClientContexct.Web.Web.GetFileById(Guid docId)

    You can reproduce this for REST API call:

    <your site url>/_api/Web/GetFileById('docId')/$value

    where docId is same value as OData__dlc_DocId. If you remove /$vale you'll receive file's metadata. Also don't forget to use proper header for request:
    Accept : application/octet-stream

    I hope this helps anyone wondering by...


    Friday, May 17, 2019 7:10 PM