5

Scenario

I have a Document library named "My Documents". In that library couple of folders, for these each folders have another couple of folders (subfolders) and inside these folders, certain documents. Requirements are that all these document id must be unique.

Now if I have the document id. How to get perticular document from its id? When the document is found I want the path to that document.

How do I accomplish that using Rest API?

0
10

Finally i found solution, i got full file url through EncodedAbsUrl

My Document id is 6 and got full filepath from its id

var requestUri = _spPageContextInfo.webAbsoluteUrl +
             "/_api/web/lists/getByTitle('My Documents')/items?$select=EncodedAbsUrl&$filter=Id eq 6";


var requestHeaders = {
    "accept": "application/json;odata=verbose"
}

$.ajax({
    url: requestUri,
    type: 'GET',
    dataType: 'json',
    headers: requestHeaders,
    success: function (data) 
    {        
       $.each(data.d.results, function(i,result) {
            var path = result.EncodedAbsUrl;
         });

    },
    error: function ajaxError(response) {
        alert(response.status + ' ' + response.statusText);
    }
});
0

For retrieving document(s) by ID, the best approach is to use filter parameter from MS Documentation.

Example:

<url_SP_Site>/_api/web/lists/getbytitle('[Name_Document_Library]')/items?$select=Id&$filter=Id eq '[Id_you_search_for]'

If you want to search for multiple documents IDs, you can add and or or operators (I think you have more operators, see the documentation).

Examples:

<url_SP_Site>/_api/web/lists/getbytitle('[Name_Document_Library]')/items?$select=Id&$filter=Id eq '[Id_you_search_for]' and Id eq '[Another_Id_you_search_for]'
<url_SP_Site>/_api/web/lists/getbytitle('[Name_Document_Library]')/items?$select=Id&$filter=Id eq '[Id_you_search_for]' or Id eq '[Another_Id_you_search_for]'

After that you can use expand parameter with file value to have the relative URL:

[url_SP_Site]/_api/web/lists/getbytitle('[Name_Document_Library]')/items?$select=Id,File/ServerRelativeUrl&$expand=File&$filter=Id eq '[Id_you_search_for]' or Id eq '[Another_Id_you_search_for]'

When you get the response encoded in JSON Format, you can access easily with:

d/results([Your_Index])/File/ServerRelativeUrl

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.