首页 > 解决方案 > 如果 Document-CT 在常规 SharePoint 文档库中具有自定义表单,则无法更改 Office 文档的内容类型

问题描述

这似乎是 SharePoint 处理 Office 文档的一个错误:如果内容类型具有自定义表单,我们无法更改 SharePoint 文档库中的 Microsoft Office 文档内容类型。

复制步骤:

  1. 在 SharePoint 网站中创建新文档库
  2. 激活内容类型,添加任何第二个内容类型(例如图片)
  3. 将空白 Excel 或 Word 文件上传到库中,
  4. 更改默认内容类型的表单(文档,请参见下面的示例代码)
  5. 尝试更改文档的内容类型 - 更改无效

如果您上传任何其他文件,例如 txt 或 pdf,您可以按预期更改内容类型。

您可以使用以下 JavaScript 代码从浏览器开发工具的 allitems.aspx 视图中自定义 SharePoint 中的表单:

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, "", "", ""); 
}

其他人,看到同样的问题吗?

任何解决方法,除了删除和恢复自定义表单以更改内容类型?

标签: sharepoint

解决方案


推荐阅读