利用Ajax增删改Sharepoint List Item
在使用一个工具的是想要在本地的HTML文件或者JS,修改Sharepoint List中的数据。
如下是找到的方法。不知道还有其他方法没。IE中可以使用。记得加载Jquery。
如果是Chrome 浏览器需要disable-web-security
比如需要在CMD下面这麽打开浏览器 chrome.exe --disable-web-security
或者在快捷方式的Target里面加入如图(我的是QQ浏览器,Webkit内核):
增删改:
<html> <head> <script type="text/javascript" src="jquery-2.1.4.min.js"> </script> <script type="text/javascript"> function batchUpdate(siteUrl, listId, 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>' + listId + '</listName>'+ '<updates>' + batch + '</updates>'+ '</UpdateListItems>'+ '</soap:Body>'+ '</soap:Envelope>'; $.ajax({ url: siteUrl + "/_vti_bin/lists.asmx", beforeSend: function(xhr) { xhr.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/UpdateListItems"); }, type: "POST", dataType: "xml", data: soapEnv, complete: function (xData, status) { alert(status); }, contentType: "text/xml; charset=utf-8" }); } var listItemId = 2; var title = 'sss'; var listId = '{6CFA25DF-7505-4F94-A7FF-AFBD22F13E02}'; var siteUrl = 'xxxxx'; var batch = '<Batch OnError="Continue">'+ '<Method ID="1" Cmd="New">'+ '<Field Name="ID">' + 'New' + '</Field>'+ '<Field Name="Title">' + title + '</Field>'+ '</Method>'+ '</Batch>'; batchUpdate(siteUrl, listId, batch); </script> </head> </html>
Batch Method Cmd使用参考:
https://msdn.microsoft.com/en-us/library/lists.lists.updatelistitems(v=office.12).aspx
查
<html> <head> <script type="text/javascript" src="jquery-2.1.4.min.js"> </script> <script type="text/javascript"> function getItems(siteUrl, listName,query,viewFields,rowLimit,queryOptions) { 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>'+ '<GetListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">'+ '<listName>' + listName + '</listName>'+ '<query>' + query + '</query>'+ '<viewFields>' + viewFields + '</viewFields>'+ '<rowLimit>' + rowLimit + '</rowLimit>'+ '<queryOptions>' + queryOptions + '</queryOptions>'+ //'<webID>' + webID + '</webID>'+ '</GetListItems>'+ '</soap:Body>'+ '</soap:Envelope>'; $.ajax({ url: siteUrl + "/_vti_bin/lists.asmx", type: "POST", dataType: "xml", data: soapEnv, error: printError, complete: processResult, contentType: "text/xml; charset=\"utf-8\"" }); } function printError(XMLHttpRequest, textStatus, errorThrown) { alert("There was an error: " + errorThrown + " " + textStatus); alert(XMLHttpRequest.responseText); } function processResult(xData, status) { var liHtml =''; $(xData.responseXML).find("z\\:row,row").each(function() { liHtml += "<h3>" + $(this).attr("ows_Title") + "</h3>"; }); $("#Title").html(liHtml); } var siteUrl = 'http://xxxx'; var query='<Query>'+ //' <Where>'+ // '<Eq>'+ // '<FieldRef Name="ID" />'+ // ' <Value Type="Counter">26</Value>'+ // '</Eq>'+ // '</Where>'+ '</Query>'; var viewFields='<ViewFields />'; var queryOptions='<QueryOptions />'; var rowLimit=10; //var viewName="AllItems"; var listName='xxxx'; getItems(siteUrl, listName,query,viewFields,rowLimit,queryOptions); </script> </head> <div id='Title'></div> </html>
MSDN参考:
https://msdn.microsoft.com/en-us/library/lists.lists.getlistitems(v=office.12).aspx