SharePoint 2010 REST jQuery更新

SharePoint 2010 REST jQuery更新,jquery,rest,sharepoint-2010,Jquery,Rest,Sharepoint 2010,我正在尝试使用REST和jQuery更新SharePoint 2010中列表项的单个值。到目前为止,我还没有得到任何工作。我看过很多帖子和MSDN文章。这里有一个帖子声称它可以工作,但是它失败了,出现了“500:内部服务器错误”。下面是我的代码版本 要明确的是,我需要从客户端做所有事情,并且更喜欢使用REST而不是SPServices方法 <html> <head> <script language="javascript" type="text/javascrip

我正在尝试使用REST和jQuery更新SharePoint 2010中列表项的单个值。到目前为止,我还没有得到任何工作。我看过很多帖子和MSDN文章。这里有一个帖子声称它可以工作,但是它失败了,出现了“500:内部服务器错误”。下面是我的代码版本 要明确的是,我需要从客户端做所有事情,并且更喜欢使用REST而不是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>

//更新
函数更新项目(id){
var projectUrl=“http://[path removed]/\u vti\u bin/listdata.svc/ProjectManagement”;
projectUrl=projectUrl+“(“+id+”)”;
var-beforeSendFunction;
var projectModifications={};
projectModifications.Title=“已执行测试更新”;
//var updatedProjectData=JSON.stringify(projectModifications);
var updatedProjectData=$.toJSON(projectModifications);
//更新现有项目
beforeSendFunction=函数(xhr){
setRequestHeader(“如果匹配”,“*”);
//使用合并,这样整个实体就不需要通过线路发送。
setRequestHeader(“X-HTTP-Method”,“MERGE”);
};
$.ajax({
类型:“POST”,
contentType:“应用程序/json;字符集=utf-8”,
processData:false,
beforeSend:beforeSendFunction,
url:projectUrl,
数据:更新的项目数据,
数据类型:“json”,
错误:函数(xhr){
警报(xhr.status+”:“+xhr.statusText);
},
成功:函数(){
警报(“更新”);
getAll();
}
});
}    
测试页
这是一个测试页面
更新!

我在您的示例中发现的唯一可能的问题与列表项对象
projectModifications
有关,当列表项实体不包含指定的属性(在您的示例中是
Title
)时,会发生错误

例如,对于OOTB任务列表(实体
Microsoft.SharePoint.DataService.TasksItem
),您应该指定
TaskName
属性:

var taskProperties = {
        'TaskName': 'Approval'
    };
因此,对于解决方案,请确保指定了正确的属性

此外,您可以按如下方式打印详细的错误消息:

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

下面提供了执行
更新操作的常用功能

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 CRUD操作说明的文章。

Wow!成功了!我一直在尝试对我发布的代码进行各种调整,但当我使用您的示例时,它第一次起作用。非常感谢!我希望我知道旧代码的问题是什么。太好了,很高兴听到:)我仍在尝试通过多个选择查找找出添加/更新项。有什么例子吗?