簡體   English   中英

SharePoint 2010 REST jQuery更新

[英]SharePoint 2010 REST jQuery Update

我正在嘗試使用REST和jQuery更新SharePoint 2010中列表項的單個值。 到目前為止,我還沒有任何工作。 我看了很多帖子和MSDN文章。 這是聲稱可以正常工作的帖子的鏈接 ,但失敗並顯示“ 500:Internal Server Error”。 下面是我的代碼版本。為清楚起見,我需要從客戶端執行所有操作,並且希望使用REST over SPServices方法。

<html>
<head>
<script language="javascript" type="text/javascript" 
src="https://http://[path removed]/jquery.json-2.4.min.js"></script>
<script language="javascript" type="text/javascript">

//update
           function updateProject(id) {
               var projectUrl = "http://[path removed]/_vti_bin/listdata.svc/ProjectManagement";
               projectUrl = projectUrl + "(" + id+ ")";
               var beforeSendFunction;  
               var projectModifications = {};
               projectModifications.Title = "Test Update Performed";
               //var updatedProjectData = JSON.stringify(projectModifications);
               var updatedProjectData = $.toJSON(projectModifications); 
               //update exsiting project
               beforeSendFunction = function (xhr) {
                   xhr.setRequestHeader("If-Match", "*");
                   // Using MERGE so that the entire entity doesn't need to be sent over the wire.
                   xhr.setRequestHeader("X-HTTP-Method", 'MERGE');
               };

               $.ajax({
                   type: "POST",
                   contentType: "application/json; charset=utf-8",
                   processData: false,
                   beforeSend: beforeSendFunction,
                   url: projectUrl,
                   data: updatedProjectData,
                   dataType: "json",
                   error: function (xhr) {
                       alert(xhr.status + ": " + xhr.statusText);
                   },

                   success: function () {
                       alert("Updated");
                       getAll();
                       }
               });
           }    
</script>
<title>Test Page</title>
</head>
<body>
<div id="divarea">This is a test page</div>
<button onclick="updateProject(2523);">Update!</button>
</body>
</html>

我在您的示例中發現的唯一可能問題與List Item對象projectModifications ,當List Item實體不包含指定的屬性(在您的情況下為Title )時,將發生錯誤。

例如,如果是OOTB任務列表(實體Microsoft.SharePoint.DataService.TasksItem ),則應指定TaskName屬性:

var taskProperties = {
        'TaskName': 'Approval'
    };

因此,在解決方案中,請確保指定正確的屬性。

此外,您可以打印詳細的錯誤消息,如下所示:

error: function (xhr) {
   console.log(JSON.stringify(xhr.responseJSON.error));
}

下面提供了執行Update操作的常用功能

function updateListItem(webUrl,listName,itemId,itemProperties,success, failure)
{ 
      var itemUrl = webUrl + "_vti_bin/listdata.svc/" + listName + "(" + itemId + ")"; 
      $.ajax({
         type: 'POST',
         url: itemUrl,
         contentType: 'application/json',
         processData: false,
         headers: {
                "Accept": "application/json;odata=verbose",
                "X-HTTP-Method": "MERGE",
                "If-Match": "*" 
         },
         data: JSON.stringify(itemProperties),
         success: function () {
                success();
         },
         error: function (data) {
                failure(data.responseJSON.error);
         }
      });

}


//Usage: how to update Task Name (Title) in Tasks list  
var taskProperties = {
    'TaskName': 'Approval'
};


updateListItem('https://contoso.sharepoint.com/project/','Tasks',17,taskProperties,function(){
    console.log('Task has been updated'); 
  },
  function(error){
    console.log(JSON.stringify(error));
  }
); 

參考

請遵循文章“ 通過SharePoint 2010中的REST API進行項目操作” ,其中包含對SharePoint 2010 REST API的CRUD操作的描述。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM