【问题标题】:How to change column type of SharePoint online list using JSOM?如何使用 JSOM 更改 SharePoint 在线列表的列类型?
【发布时间】:2021-01-09 08:28:53
【问题描述】:

我想使用 JSOM 更改列表的列类型。 “Billable”列的类型是 Boolean,我需要将列的类型更改为“Single line of text”。

有什么方法可以改变列的类型吗?

代码如下:

var oFields, clientContext;
function UpdateListField() {
    // You can optionally specify the Site URL here to get the context
    // If you don't specify the URL, the method will get the context of the current site
    // var clientContext = new SP.ClientContext("http://MyServer/sites/SiteCollection");
    clientContext = new SP.ClientContext(appUrl);

    var web = LawApp.Repositories.getWeb(clientContext, hostUrl);

    var olistCollection = web.get_lists();

    var oList = olistCollection.getByTitle("ODMatter");

    oFields = oList.get_fields();

    clientContext.load(oFields);

    // Execute the query to the server.
    clientContext.executeQueryAsync(onsuccess, onfailed);
}

function onsuccess() {

    // Iterate through Enumerator
    var oEnumerator = oFields.getEnumerator();

    while (oEnumerator.moveNext()) {
        var oField = oEnumerator.get_current();

        // Enter the field name here
        if (oField.get_title() == "Billable") {
            oField.FieldType("text");
            oField.update();
            break;
        }
    }

    // Execute the query to the server.
    clientContext.executeQueryAsync(FinalQuerySuccess, FinalQueryFailure);

}

function onfailed(sender, args) {
    console.log('Failed' + args.get_message() + '\n' + args.get_stackTrace());
}

function FinalQuerySuccess(sender, args) {
    updateCurrentVersion();
    console.log('Success');
}

function FinalQueryFailure(sender, args) {
    console.log('Failed' + args.get_message() + '\n' + args.get_stackTrace());
}

【问题讨论】:

    标签: javascript sharepoint-online sharepoint-list sharepoint-jsom


    【解决方案1】:

    你需要使用 SP.Field.set_typeAsString() 方法来改变列类型,下面是我的示例:

    var list = web.get_lists().getByTitle("Mylist");
    field = list.get_fields().getByInternalNameOrTitle("Billable");   
    field.set_typeAsString("Text");
    field.update();
    

    参考:https://docs.microsoft.com/en-us/previous-versions/office/sharepoint-visio/jj245910(v=office.15)

    【讨论】:

    • 非常感谢它现在的工作。你能告诉我我应该把这个方法传递给“多行文本”吗?您传入方法的“文本”正在将列类型更改为单行文本。 field.set_typeAsString("文本");
    • 我还需要将另一列的类型从“单行文本”更改为“多行文本”。
    • 要更改为“多行文本”,请使用 field.set_typeAsString("Note")。
    • 如果我的回答对你有帮助,请接受它作为答案并点赞:)
    • 你好 Michael Han_MSFT,你能帮我解决这个问题吗? stackoverflow.com/questions/64170838/…
    猜你喜欢
    • 1970-01-01
    • 2010-11-02
    • 2011-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多