Spservices Introduction: Invoking a Web Service using a Spserivces object

Source: Internet
Author: User

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
    1. $(). Spservices ({
    2. Operation: "OperationName",
    3. [WebURL: "/sitepath",]
    4. [Option1:value1,]
    5. [Option2:value2,]
    6. [Async: false,]
    7. Completefunc: function (XData, Status) {
    8. ... Do stuff ...
    9. }
    10. });

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
  1. $(). Spservices ({
  2. Operation: "GetListItems",
  3. WebURL:"/",
  4. ListName: "Tasks",
  5. Async: false,
  6. Completefunc: function (XData, Status) {
  7. if (Status = = ' success ')
  8. {
  9. alert (Xdata.responsetext);
  10. }
  11. }
  12. });
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
    1. $ (xdata.responsexml). Spfilternode (Somenode)
You can use this method to parse the XML data returned by the GetListItems method, as follows:

[JavaScript]View Plaincopy
  1. $(). Spservices ({
  2. Operation: "GetListItems",
  3. WebURL:"/",
  4. ListName: "Tasks",
  5. Async: false,
  6. Completefunc: function (XData, Status) {
  7. if (Status = = ' success ')
  8. {
  9. $ (xdata.responsexml).  Spfilternode ("Z:row"). each (function (index, item) {alert (index); alert (item);});
  10. }
  11. }
  12. });
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
    1. $ (xdata.responsexml). Spfilternode ( "Z:row"). Spxmltojson ({   
    2.   mapping: {},  
    3.   includeallattrs: false,    // Contains all the properties in the Z:row node   
    4.   removeows: true,            //need to remove ows_ prefixes    
    5.   sparse: false               //if true, Will not return an empty property value <span style= "font-family: arial, helvetica, sans-serif;" > ("") </span>  
    6. });   

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
    1. 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
  1. $(). Spservices ({
  2. Operation: "GetListItems",
  3. WebURL:"/",
  4. ListName: "Tasks",
  5. Async: false,
  6. Completefunc: function (XData, Status) {
  7. if (Status = = ' success ')
  8. {
  9. var Myjson = $ (xdata.responsexml). Spfilternode ("Z:row"). Spxmltojson ({
  10. Mapping: {
  11. ows_id: {mappedname: "ID", ObjectType: "Counter"},
  12. Ows_title: {mappedname: "Newtitle", ObjectType: "Text"},
  13. ows_created: {mappedname: "Created", ObjectType: "DateTime"},
  14. },
  15. Includeallattrs: True,
  16. Removeows: false
  17. });
  18. alert (Myjson);
  19. alert (myjson[0].id);
  20. Alert (myjson[0). Newtitle);
  21. Alert (myjson[0). Created);
  22. }
  23. }
  24. });
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
  1. $(). Spservices.spgetlistitemsjson ({
  2. WebURL: " ",
  3. ListName: " ",
  4. Camlviewname: " ",
  5. Camlquery: " ",
  6. Camlviewfields: " ",
  7. Camlrowlimit: " ",
  8. Camlqueryoptions: " ",
  9. Changetoken: " ",
  10. Contains: " ",
  11. Mapping: NULL,
  12. Mappingoverrides: null,
  13. Debug: false
  14. });

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.