如何使用JSOM更改SharePoint在线列表的列类型?

问题描述

我想使用JSOM更改列表的列类型。 “可结算”列的类型为布尔值,我需要将其类型更改为“单行文本”。

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

这是代码

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());
}

解决方法

您需要使用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)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...