Moss2007 jQuery调用Webservice获取列表数据 更新数据

Moss2007 jQuery调用Webservice获取列表数据 更新数据

1、获取数据如下:

<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>

<script type="text/javascript">

//-------------------------异步获取数据

//---------------------------------------------------

$(document).ready(function() {

// Create the SOAP request

// NOTE: we need to be able to display list attachments to users, hence the addition of the

// <queryOptions> element, which necessitated the addition of the <query> element

var soapEnv =

"<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \

<soapenv:Body> \

<GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \

<listName>testlist</listName> \

<viewFields> \

<ViewFields> \

<FieldRef Name='Title' /> \

<FieldRef Name='Body' /> \

<FieldRef Name='ID' /> \

<FieldRef Name='Attachments' /> \

</ViewFields> \

</viewFields> \

<query> \

<Query /> \

</query> \

<queryOptions> \

<QueryOptions> \

<IncludeAttachmentUrls>TRUE</IncludeAttachmentUrls> \

</QueryOptions> \

</queryOptions> \

</GetListItems> \

</soapenv:Body> \

</soapenv:Envelope>";

// our basic SOAP code to hammer the Lists web service

$.ajax({

url: "http://yourdomain.net/_vti_bin/lists.asmx",

type: "POST",

dataType: "xml",

data: soapEnv,

// 如果要同步获取数据加上下面这句话,默认为true

// async: false,

error: printError,

complete: processResult,

contentType: "text/xml; charset=\"utf-8\""

});

});

// basic error display that will pop out SOAP errors, very useful!

function printError(XMLHttpRequest, textStatus, errorThrown)

{

alert("There was an error: " + errorThrown + " " + textStatus);

alert(XMLHttpRequest.responseText);

}

// main method that will cycle through the SoAP response nodes

function processResult(xData, status)

{

$(xData.responseXML).find("z\\:row").each(function() {

if ($(this).attr("ows_Title") == null || $(this).attr("ows_Title") == "") {

//TODO

}

else {

//TODO

}

});

}

</script>

2、更新数据如下:

<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js">

<script type="text/javascript" language="javascript">

function TestMethod(Name) {

var soap = createEnvelope(Name);

var ws = new ActiveXObject("Microsoft.XMLHTTP");

ws.open("POST", "http://yourdomain.net/_vti_bin/lists.asmx", false);

ws.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

ws.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/UpdateListItems");

ws.send(soap);

}

function createEnvelope(Name) {

var soap = '<?xml version=\"1.0\" encoding=\"utf-8\"?>';

soap += '<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 += '<soap:Body>';

soap += '<UpdateListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">';

soap += '<listName>TestList</listName>';

//如果是新建记录Cmd="New" 如果是更新记录Cmd="Update"

soap += '<updates><Batch OnError="Continue"><Method Cmd="New"><Field Name="Name">' + Name + '</Field></Method></Batch></updates>';

soap += '</UpdateListItems></soap:Body></soap:Envelope>';

return soap;

}

</script>