spservices Introduction: Invoking a Web Service using a Spserivces objectCategory: Spserivces2014-09-21 04:23 304 People read comments (0) favorite reports In the previous article, Spservices introduced the basic methods of some spservices, in addition to these methods, the main use of spservices is to use Ajax methods to invoke the Web Service provided by SharePoint.
First look at the syntax:
[JavaScript]View Plaincopy
- $(). Spservices ({
- Operation: "OperationName",
- [WebURL: "/sitepath",]
- [Option1:value1,]
- [Option2:value2,]
- [Async: false,]
- Completefunc: function (XData, Status) {
- ... Do stuff ...
- }
- });
which
Operation is used to specify the name of the Web Service method, such as the GetListItems method in the most commonly used lists Web services, to get items in the list.
The Weburl is used to specify the URL that the AJAX request uses, by default the URL of the current site.
Options (OPTIONS1,OPTIONS2, etc.) are used to specify the parameters required for a Web Servcie call, and different Web services have different parameters, such as GetListItems service, need to specify Listname,viewname, and so on.
Async is used to specify whether the invocation of a Web service is asynchronous or synchronous, and the default Ajax call is asynchronous, and if you need to use Async:false to force a synchronous call to a Web service
The completefunc is used to specify a callback function that the Web service invocation succeeds.
To give a simple example, use Spservices to call the GetListItems method:
[JavaScript]View Plaincopy
- $(). Spservices ({
- Operation: "GetListItems",
- WebURL:"/",
- ListName: "Tasks",
- Async: false,
- Completefunc: function (XData, Status) {
- if (Status = = ' success ')
- {
- alert (Xdata.responsetext);
- }
- }
- });
In my environment, the result of running the above code is as shown. (Only one piece of data in the Tasks list)
You can see that we have used very little code to complete the call to the GetListItems method, and we can handle the return result in the Completefunc method. The XML data returned by the call to the Web service is saved in the Xdata.responsexml property, and in the Spserives library, two very useful methods are available to process the XML data.
One is the Spfilternode used to parse XML data.
The syntax is:
[JavaScript]View Plaincopy
- $ (xdata.responsexml). Spfilternode (Somenode)
You can use this method to parse the XML data returned by the GetListItems method, as follows:
[JavaScript]View Plaincopy
- $(). Spservices ({
- Operation: "GetListItems",
- WebURL:"/",
- ListName: "Tasks",
- Async: false,
- Completefunc: function (XData, Status) {
- if (Status = = ' success ')
- {
- $ (xdata.responsexml). Spfilternode ("Z:row"). each (function (index, item) {alert (index); alert (item);});
- }
- }
- });
This uses the Spfilternode method to traverse each Z:row node.
Another method is to convert the XML data to a JSON object's Spxmltojson
The syntax is:
[JavaScript]View Plaincopy
- $ (xdata.responsexml). Spfilternode ( "Z:row"). Spxmltojson ({
- mapping: {},
- includeallattrs: false, // Contains all the properties in the Z:row node
- removeows: true, //need to remove ows_ prefixes
- sparse: false //if true, Will not return an empty property value <span style= "font-family: arial, helvetica, sans-serif;" > ("") </span>
- });
What you need to illustrate here is the mapping parameter, which allows you to specify the matching rules between the attributes of the XML node and the properties of the JSON object, such as the properties of the Z:row node you want to Ows_title, the name in the JSON object is "Newtitle", Type is of type string, you can add such a mapping:
[JavaScript]View Plaincopy
- Ows_title: {mappedname: "Newtitle", ObjectType: "Text"}
Where mappedname specifies the name in the JSON object, objecttype specifies the JS object after the conversion. The corresponding conversion relationships for SharePoint Field objects and JS objects are as follows:
spfieldtype (field type) |
JavaScript Object type (JS type) |
Counter |
Int |
Integer |
Int |
Datetime |
Date () |
User |
User = {userId, userName} |
Usermulti |
User array |
Lookup |
Lookup = {lookupid, lookupvalue} |
Lookupmulti |
Lookup array |
Boolean |
True/false |
Multichoice |
String number |
Currency |
Float |
Text |
String |
To illustrate:
[JavaScript]View Plaincopy
- $(). Spservices ({
- Operation: "GetListItems",
- WebURL:"/",
- ListName: "Tasks",
- Async: false,
- Completefunc: function (XData, Status) {
- if (Status = = ' success ')
- {
- var Myjson = $ (xdata.responsexml). Spfilternode ("Z:row"). Spxmltojson ({
- Mapping: {
- ows_id: {mappedname: "ID", ObjectType: "Counter"},
- Ows_title: {mappedname: "Newtitle", ObjectType: "Text"},
- ows_created: {mappedname: "Created", ObjectType: "DateTime"},
- },
- Includeallattrs: True,
- Removeows: false
- });
- alert (Myjson);
- alert (myjson[0].id);
- Alert (myjson[0). Newtitle);
- Alert (myjson[0). Created);
- }
- }
- });
The structure of the Myjson object after running is this:
You can see that ows_id has map to the id attribute, and the same ows_title map to the Newtitle attribute, as well as ows_created,map to the Created attribute.
In addition to the above two methods, Spservices also provides a more methodological approach: Spgetlistitemsjson
This method calls GetListItems the Web service method, directly returns a JSON object, very convenient to use, but also eliminates the mapping trouble.
Grammar:
[JavaScript]View Plaincopy
- $(). Spservices.spgetlistitemsjson ({
- WebURL: " ",
- ListName: " ",
- Camlviewname: " ",
- Camlquery: " ",
- Camlviewfields: " ",
- Camlrowlimit: " ",
- Camlqueryoptions: " ",
- Changetoken: " ",
- Contains: " ",
- Mapping: NULL,
- Mappingoverrides: null,
- Debug: false
- });
This will not give an example.
In addition to the above example of the GetListItems method, the Spservice library can also support a lot of Web service calls, specifically, please refer to: Click to open the link
Spservices Introduction: Invoking a Web Service using a Spserivces object