SHAREPOINT 2013:如何使用javascript CSOM将网站列更新为数组内容?

3

我是一个相对新手的Sharepoint 2013用户,我正在尝试使用数组来更新站点列的内容。我可以检索和可视化我的站点列的内容,并且用户可以更改并保存必要的部分,这些更改保存到了一个数组中。现在我需要使用数组的内容来更新站点列的内容,但由于某些原因我无法完成这个任务,请问您有什么建议或示例吗?以下是我目前用来检索、可视化站点列并将修改存储到数组中的代码。

<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

$(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()函数上,请帮忙!谢谢!

1个回答

1

修改后的示例

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

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.executeFunc而不是SP.SOD.executeOrDelayUntilScriptLoaded,因为它支持按需加载脚本。
  • 避免使用全局变量。

好的,我已经复制了你的代码,并将函数“addItemsToColumns()”放在onClick事件中,但我的网站列仍然没有使用新值更新..当我刷新页面时,网站列仍然具有旧值..这是我的工作,正在让我发疯..控制台日志显示:此功能不适用于未与列表相关联的字段。 - FabioEnne
1
啊,请将函数get_availableFields()替换为get_fields(),我相信错误会消失(请参见更新的答案)。 - Vadim Gremyachev
已经完成了...但现在它不再显示我的站点列了... :( ...我开始对此失去所有希望了..... - FabioEnne
1
好的,现在它可以工作了。我将Web部件添加到了子网站中的一个页面中,因此无法更新上一级中的列。非常感谢! - FabioEnne

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接