SHAREPOINT 2013:如何读取站点栏内容并通过 javascript csom 修改它们?

SHAREPOINT 2013: How can I read Site column contents and modify them via javascript csom?

我是 Sharepoint 2013 的新手,我正在尝试访问并在页面上显示特定 "Site Column" 中的内容,我已经阅读了很多相关内容,但我仍然无法完成我的任务。到目前为止我得到这个:

'use strict';
        var hostweburl;
        var appweburl;
        $(function () {
            SP.SOD.executeOrDelayUntilScriptLoaded(Function.createDelegate(this, function () {
                var currentcontext = new SP.ClientContext.get_current();

                var web = currentcontext.get_web();

                //Get all fields in site collection
                var collFields = web.get_availableFields().getByTitle("EngineType_EngineCycle");

                currentcontext.load(collFields);

                currentcontext.executeQueryAsync(Function.createDelegate(this, ExecuteOnSuccess), 
                Function.createDelegate(this, ExecuteOnFailure)); 
            }), 'SP.js'); 

            function ExecuteOnSuccess(sender, args) 
            { 
                var subsites = ''; 

                //for(int i=0; i< collF {
                //    if(collFields[i].Title == "siteColumnName"){
                //        alert("Got the Site col");
                //    }
                //}

            }
            function ExecuteOnFailure(sender, args) { 
                alert("error"); 
            } 
         });

但现在我不知道如何 access/retrieve 我在 CollField 中需要的数据,也许我哪里出错了?请帮忙。非常感谢。

SP.FieldCollection object contains the following methods for getting Field object:

  • getById - 获取指定ID的字段
  • getByInternalNameOrTitle - returns 具有指定内部名称或标题的第一个字段 object 来自 collection
  • getByTitle-returns第一个字段object在collection根据指定字段的标题

例子

以下示例演示如何检索网站栏:

function getSiteField(fieldName,success,failure)
{
     var ctx = SP.ClientContext.get_current(); 
     var rootWeb = ctx.get_site().get_rootWeb(); 
     var field = rootWeb.get_availableFields().getByInternalNameOrTitle(fieldName);
     ctx.load(field);
     ctx.executeQueryAsync(
         function(){
            success(field)
         },
         failure);
}

用法

SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function() {

   //get Title column and print its properties
   getSiteField('Title',
    function(field){
       //print field properties
       console.log(field.get_title()); 
       console.log(field.get_internalName());
       console.log(field.get_typeAsString());
       console.log(field.get_description());
    },
    function(sender,args){
       console.log(args.get_message());
    });

});

如何更新Field object

以下示例演示如何更新 SP.FieldChoice 字段属性:

function updateFieldChoice(fieldTitle,choiceValues,success,failure) {
    var ctx = SP.ClientContext.get_current();
    var web = ctx.get_web();
    var fieldChoice = ctx.castTo(web.get_availableFields().getByTitle(fieldTitle), SP.FieldChoice);
    fieldChoice.set_choices(choiceValues);
    fieldChoice.update();
    ctx.executeQueryAsync(
         function(){
            success(fieldChoice)
         },
         failure);
}

用法

var choiceValues = ["Low", "Normal", "Critical"];

updateFieldChoice('RequestStatus',choiceValues,
    function(field){
       console.log('Choice field has been updated'); 
    },
    function(sender,args){
       console.log(args.get_message());
    });