getting the file template used for a specific file in a document library
-
08-02-2021 - |
Frage
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!
Lösung 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.
Andere Tipps
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:
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit sharepoint.stackexchange