0

I am trying to not have to create a different content type for a document library. Instead I was thinking if given the files in a document library I can find out which file template was used for that document.

Is there such thing? Can I query it with JavaScript/SPFX webpart?

Thanks!

New contributor
Ricardo Silva is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
0

We can use the below REST API code to read the specific file from SharePoint:

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;
            var fileTemplate= result.Template;

         });

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

Note:

  • var path = result.EncodedAbsUrl; - This gives the absolute URL of the file
  • now type result.Template which will give the template name - may be the exact template attribute name will be different, then in the debug mode (F12) after result. type "Templ" - then we will get the exact attribute name.

Reference:

Get documents By ID from document library using Rest API

  • Thanks, Although that does not quite answer the question, there is not such a name and you just showing how to query a document library, we need to expand the content type. I am posting a separated for the sake of clarity. – Ricardo Silva Mar 25 at 18:52
0

For whoever is looking for the same. You can achieve by expanding the ContentType of the the file. here is the api path:

_api/web/lists/GetByTitle('Success%20Stories')/Items?$select=ContentType/DocumentTemplate&$expand=ContentType

Hope it helps someone else.

New contributor
Ricardo Silva is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

Ricardo Silva is a new contributor. Be nice, and check out our Code of Conduct.

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.