SHAREPOINT 2013:如何使用带有 javascript CSOM 的数组内容更新站点列? [英] SHAREPOINT 2013: How can I update a Site Column with the content of an array with javascript CSOM?

查看:50
本文介绍了SHAREPOINT 2013:如何使用带有 javascript CSOM 的数组内容更新站点列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Sharepoint 2013 的新手,我正在尝试使用数组的内容更新站点列的内容,我可以检索和可视化站点列的内容,用户可以更改和保存必要的部分并将更改保存到数组中,现在我必须用数组的内容更新站点列的内容,但是由于某种原因我无法完成,有什么建议/示例吗?到目前为止,这是我的代码,用于检索、可视化站点列并将移动内容存储到我的数组中.

I'm relative new to Sharepoint 2013, I'm trying to update the content of a Site column with the content of an array, I can retrieve and visualize the content of my site column, the user is able to change and save the necessary part and the changes are saved into an array, now I have to update the content of the site column with the content of the array, but for some kind of reasons I can't accomplish that, any suggestion/example? This is my code so far to retrieve, visualize the site column and store the mofication into my array.

<body>
        <select id="dropdown" name="dropdown" onchange="optSelect()">
            <option value="EngineType_Cylinders">EngineType_Cylinders</option>
            <option value="EngineType_EngineCycle">EngineType_EngineCycle</option>
            <option value="EngineType_EngineFamily">EngineType_EngineFamily</option>
            <option value="EngineType_Euro">EngineType_Euro</option>
            <option value="EngineType_FamilyEvolution">EngineType_FamilyEvolution</option>
            <option value="EngineType_GasEmissionLevel">EngineType_GasEmissionLevel</option>
            <option value="EngineType_Power">EngineType_Power</option>
            <option value="EngineType_PowerSupply">EngineType_PowerSupply</option>
            <option value="EngineType_Use">EngineType_Use</option>
        </select><br />

        <textarea id="textareadisplay" rows="25" cols="23"></textarea><br />
        <input type ="button" value="Update values" onclick="addItemsToColumns()" />
    </body>

我的 JavaScript

My Javascript

$(function () {
    SP.SOD.executeOrDelayUntilScriptLoaded(Function.createDelegate(this, function () {
       var select = document.getElementById('dropdown').value;
        console.log(select);
        getSiteColumns(select);

    }), 'SP.js');
});

var fieldChoice;
var choices;
var addFields = [];
var slc;
var clientContext;

function optSelect() {
    slc = document.getElementById('dropdown').value;
    getSiteColumns(slc);
}

function getSiteColumns(selection) {
   clientContext = SP.ClientContext.get_current();
    if (clientContext != undefined && clientContext != null) {

        var web = clientContext.get_web();

        fieldChoice = clientContext.castTo(web.get_availableFields().getByTitle(selection), SP.FieldChoice);

        clientContext.load(this.fieldChoice);
        clientContext.executeQueryAsync(Function.createDelegate(this, this.OnLoadSuccess), Function.createDelegate(this, this.OnLoadFailed));
    }
}

function OnLoadSuccess(sender, args) {
    choices = fieldChoice.get_choices();
    var textarea = document.getElementById("textareadisplay");
    textarea.value = choices.join("\n");

}

function OnLoadFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

function addItemsToColumns() {
    clientC = SP.ClientContext.get_current();
    var arrayForUpdate = $('#textareadisplay').val().split('\n');
    fieldChoice.set_item(, arrayForUpdate);
    fieldChoice.update();
    clientContext.executeQueryAsync(function () { }, function () { });


}

function OnUpdateSuccess(sender, args) {
    var newchoices = fieldChoice.get_choices();

}

我的问题是关于函数 addItemsToColumns() 请帮忙!提前致谢.

My problem is on the function addItemsToColumns() please help! Thanks in advance.

推荐答案

修改示例

以下修改后的示例应该可以解决问题:

The following modified example should do the trick:

SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function() {
     var fieldName = $('#dropdown').find(":selected").text();   //get selected column
     populateValues(fieldName);
});


function populateValues(fieldName)
{
    getFieldChoice(fieldName,
      function(field){
           var values = field.get_choices();             
           $("textarea#textareadisplay").val(values.join("\n"));
      },
      function(sender,args){
         console.log(args.get_message());  //handle errors..
      });
}



function addItemsToColumns() {
    var fieldName = $('#dropdown').find(":selected").text();   //get selected column
    var values = $('textarea#textareadisplay').val().split('\n');
    updateFieldChoice(fieldName,values,
       function(field){
         console.log(String.format('{0} field has been updated',fieldName)); 
      },
      function(sender,args){
         console.log(args.get_message());  //handle errors..
      });
}



//SharePoint specific function for getting choice column 
function getFieldChoice(fieldTitle,success,failure) {
    var ctx = SP.ClientContext.get_current();
    var web = ctx.get_web();
    var fieldChoice = ctx.castTo(web.get_fields().getByTitle(fieldTitle), SP.FieldChoice);
    ctx.load(fieldChoice);
    ctx.executeQueryAsync(
         function(){
            success(fieldChoice)
         },
         failure);
}

//SharePoint specific function for updating choice column
function updateFieldChoice(fieldTitle,choiceValues,success,failure) {
    var ctx = SP.ClientContext.get_current();
    var web = ctx.get_web();
    var fieldChoice = ctx.castTo(web.get_fields().getByTitle(fieldTitle), SP.FieldChoice);
    fieldChoice.set_choices(choiceValues);
    fieldChoice.update();
    ctx.executeQueryAsync(
         function(){
            success(fieldChoice)
         },
         failure);
}

一些建议

  • 更喜欢 SP.SOD.executeFuncSP.SOD.executeOrDelayUntilScriptLoaded 因为它支持加载需求脚本
  • 避免使用全局变量

这篇关于SHAREPOINT 2013:如何使用带有 javascript CSOM 的数组内容更新站点列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆