2015-04-30 81 views
3

我對Sharepoint 2013相對比較陌生,我試圖用數組內容更新網站列的內容,我可以檢索並可視化我的網站列的內容,用戶可以更改並保存必要的部分和更改被保存到一個數組中,現在我必須用數組的內容更新網站列的內容,但出於某種原因,我無法完成這些任何建議/示例?這是我的代碼到目前爲止檢索,可視化站點列並將mofication存儲到我的數組。SHAREPOINT 2013:如何使用javascript CSOM更新一個數組內容的網站列?

<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

變形例

以下變形例應達到目的:因爲它支持負載

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在 需求腳本
  • 避免全局變量
+0

好,我貼你的代碼,我把函數「addItemsToColumns()」的onClick事件,但我的網站列仍然不能用新值更新..當我刷新頁面時,網站欄目仍然存在舊的值..我正在努力工作,並讓我瘋狂。CONSOLE LOG SAYS:此功能對於與列表不相關的字段不可用。 – FabioEnne

+1

啊,請用get_fields()替換函數get_availableFields(),我相信錯誤會消失(請參閱更新後的答案) –

+0

完成它..但現在它不再顯示我的站點列了...... :(...我開始失去對此的所有希望..... – FabioEnne

相關問題