使用 Web 服务代码更新 SharePoint 中的现有列表项值
本文关键字:列表 服务 Web 代码 更新 SharePoint 使用 | 更新日期: 2023-09-27 18:35:31
我有以下代码,我正在尝试运行它来更新列表项值。"设置"是列表名称,此列表中的项目的索引为 1。
Main()
{
ListsSoapClient client = new ListsSoapClient();
client.ClientCredentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
string strListName = "Settings";
client.Open();
XElement listData = client.GetList(strListName);
string listID = listData.Attribute("ID").Value;
string version = listData.Attribute("Version").Value;
// string version = listData.Attribute("View").Value; Doesnt work
// Method 1 : Make the call to SharePoint
var listItems = client.GetListItems(strListName, null, null, null, null, null, null);
List<XElement> results = listItems.Descendants().ToList();
XElement updateItem = results[1];
updateItem.SetAttributeValue("ows_Value", "value to update");
client.UpdateListItems(strListName, updateItem); //Didnt work
// Method 2 : Make the call to SharePoint
string strBatch = "<Method ID='1' Cmd='Update'>" +
"<Field Name='ID'>1</Field>" +
"<Field Name='Title'>" + "999" + "</Field></Method>";
XmlDocument xmlDoc = new System.Xml.XmlDocument();
System.Xml.XmlElement elBatch = xmlDoc.CreateElement("Batch");
//elBatch.SetAttribute("OnError","Continue"); //Not mandatory ?
//elBatch.SetAttribute("ListVersion","1"); //Not mandatory ?
//elBatch.SetAttribute("ViewName", "00F85842-35AD-4AED-8DF7-0F903FB850BE"); is it mandatory ?
elBatch.InnerXml = strBatch;
client.UpdateListItems(strListName, XmlElementToXelement(elBatch)); //doesnt work
client.Close();
}
public static XElement XmlElementToXelement(XmlElement e)
{
return XElement.Parse(e.OuterXml);
}
我们如何使用代码获取列表的 ViewName 值? 为什么在方法 2.In Method2 中不是必需的,列表项标题被替换为值 999。而我想更新该列表项的值。
在方法1结束时,我得到以下异常。我该如何解决这个问题?
Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown.
有时,此错误的原因是 SOAP 操作中的标头。您需要设置以下内容才能进行更新。
beforeSend: function(xhr) { xhr.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/UpdateListItems");
我创建了以下函数来在 SharePoint 列表中发送更新:
var location = yoururl
var listName = yourlistname
var command = "Update" // "Update" or "New"
var fieldnames = ["ID","Title"]; //array with the fieldnames
var fieldvalues = [value1,value2]; //array with the values
function sendupdates(location,listName,command,fieldnames,fieldvalues){
var updatesvar;
for(x=0;x<fieldnames.length;x++){
updatesvar = updatesvar + '<Field Name="'+fieldnames[x]+'">'+fieldvalues[x]+'</Field>'
}
var batchvar = '<Batch OnError="Continue" ListVersion="0"><Method ID="1" Cmd="'+command+'">'+updatesvar+'</Method></Batch>';
var soapEnv =
'<?xml version="1.0" encoding="utf-8"?>'+
'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'+
'<soap:Body>'+
'<UpdateListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">'+
'<listName>'+listName+'</listName>'+
'<updates>'+batchvar+'</updates>'+
'</UpdateListItems>'+
'</soap:Body>'+
'</soap:Envelope>';
$.ajax({
url: location+"/_vti_bin/Lists.asmx",
beforeSend: function(xhr) { xhr.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/UpdateListItems");},
type: "POST",
dataType: "xml",
data: soapEnv,
complete: complete,
contentType: "text/xml; charset='"utf-8'""
});
}