Question

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!

Was it helpful?

Solution 2

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top