【问题标题】:SHAREPOINT 2013: How can I read Site column contents and modify them via javascript csom?SHAREPOINT 2013:如何阅读站点列内容并通过 javascript csom 修改它们?
【发布时间】:2015-04-30 18:28:42
【问题描述】:

我对 Sharepoint 2013 比较陌生,我正在尝试访问并在页面上显示特定“站点列”内的内容,我已经阅读了很多关于此的内容,但我仍然不能完成我的任务。到目前为止,我得到了这个:

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

但是现在我不知道如何在 CollField 中访问/检索我需要的数据,也许我在某个地方弄错了?请帮忙。非常感谢。

【问题讨论】:

    标签: javascript sharepoint-2013 csom


    【解决方案1】:

    SP.FieldCollection object包含以下获取Field object的方法:

    • getById - 获取指定 ID 的字段
    • getByInternalNameOrTitle - 返回集合中具有指定内部名称或标题的第一个 Field 对象
    • getByTitle - 根据指定字段的标题返回集合中的第一个字段对象

    示例

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

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

    【讨论】:

    • 非常感谢.. 请再问一个问题.. 如果我想用数组的内容更新站点栏的内容,我该怎么办? (例如,我的站点列包含值为 1 2 3 4 5 并且我的数组包含 1 2 2 4 5)。
    • 当然,答案已经更新,它演示了如何为 SP.FieldChoice 字段设置选择值
    • 对不起,我不明白如何实现,请您参考一下好吗? stackoverflow.com/questions/29964007/…
    • 不用担心,请您澄清一下,该脚本应该从应用程序页面(SharePoint 应用程序的一部分)还是从主机网站(SharePoint 页面)执行?
    • 这是一个显示在 SharePoint 页面中的 Web 部件,非常感谢!!
    猜你喜欢
    • 2015-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多