Forum Discussion
EinmalIM
Aug 02, 2021Brass Contributor
Content Type cannot be changed for Office docs with custom forms in document libs
This seems like a bug how SharePoint handles Office documents: we cannot change a Microsoft Office documents content type in a SharePoint document library if the content type has a customized form.
Repro steps:
- Create a new document library in a SharePoint site
- activate Content Types, add any second Content-Type (e.g. Picture)
- upload a blank Excel or Word file into the library,
- change the Forms of the default Content Type (Document, see sample code below)
- attempt to change the document's content type - the change is not effective
If you upload any other file like txt or pdf you can change the content type as expected.
You can customize the forms in SharePoint in the allitems.aspx view from the browsers dev tools with the following JavaScript code:
const spWebUrl = _spPageContextInfo.webAbsoluteUrl; // assuming to be on a SharePoint AllItems.aspx list-view page
const docLibId = _spPageContextInfo.listId; // assuming to be on a SharePoint AllItems.aspx list-view page
const testCtId = '0x010100B68D7A00400FB34BA1A31C9ABBB97168'; // GET THE ID OF THE DEFAULT-CONTENT-TYPE ("Document") FROM THE LIBRARY SETTINGS PAGE
const formUrl = _spPageContextInfo.webServerRelativeUrl + '/SitePages/TestForm.aspx'; // custom form location
// to switch to custom forms, uncomment the following line:
setFormsForCT(spWebUrl, docLibId, testCtId, formUrl, formUrl, formUrl);
// check the console and network tab output whether the operation was successful
// to restore standard forms, uncomment the following line:
//restoreFormsForCT(spWebUrl, docLibId, testCtId);
async function setFormsForCT(webUrl, listId, ctId, dispFormUrl, editFormUrl, newFormUrl) {
await fetch(`${webUrl}/_api/web/lists('${encodeURIComponent(listId)}')/contenttypes('${encodeURIComponent(ctId)}')`, {
method: 'POST',
headers: {
'Accept': 'application/json; odata.metadata=minimal',
'Content-type': 'application/json; charset=utf-8',
'X-HTTP-Method': 'MERGE', // updating
'IF-MATCH': '*',
'X-RequestDigest': _spPageContextInfo.formDigestValue, // assuming valid and defined
},
body: JSON.stringify({
"DisplayFormUrl": dispFormUrl,
"EditFormUrl": editFormUrl,
"NewFormUrl": newFormUrl
}),
});
}
async function restoreFormsForCT(webUrl, listId, ctId) {
return setFormsForCT(webUrl, listId, ctId, "", "", "");
}
Anybody else, seeing the same problem?
Any workarounds, except removing and restoring the custom form to change the content type?
No RepliesBe the first to reply