Use JavaScript to call CRM web service on the client

Source: Internet
Author: User

In CRM, JavaScript is used to call web service on the client to facilitate deployment and improve user experience. in fact, the development of plugin on the server side can still achieve similar results, and we can easily process the results returned through the Web service, but the main advantage of using JavaScript is that it is easy to deploy. As we all know, most of the secondary development in CRM focuses on customization. We can easily export the written JavaScript and the Javascript methods called in the client events to the customization file, in this way, we do not need to do anything else when deploying to production machines.

Like a common JavaScript client side that calls a web service, the process constructs a request SOAP message, and then sends the request to the Web service by creating an XMLHTTP object, then, the XML is parsed based on the result returned by XMLHTTP to refresh the interface. In fact, it is the same as the Web service call process on the server side. We may wish to make a comparison to help you better understand the client-side calling process:

1) construct a queryexpression object to prepare the requested object and the Object Attributes returned by the request.

Queryexpression query = new queryexpression ();
Query. columnset = new allcolumns ();
Query. entityname = entityname. Account. tostring ();


2) Add filter conditions to the constructed request object
Filterexpression filter = new filterexpression ();
Query. Criteria = filter;

Conditionexpression hasemailaddress = new conditionexpression ();
Filter. Conditions = new conditionexpression [] {hasemailaddress };

Hasemailaddress. attributename = "emailaddress1 ";
Hasemailaddress. Operator = conditionoperator. notnull;

3) Call the retrievemultiple or retrievesingle method of crmservice to send a request.

Crmlogservice service = new crmlogservice ();
Service. url = "http: // stunnwarecrm/mscrmservices/2006/crmservice. asmx ";
Service. Credentials = credentialcache. defaultcredentials;
Service. soapmessageserializationfinished + = new soapmessageeventhandler (service_soapmessageserializationfinished );
Service. retrievemultiple (query );
The three steps for the server to call the Web service are actually no different from all the methods for calling the web service. In addition to declaring columnset as the returned object attribute group according to the CRM method, using conditionoperator as the relational symbol of the conditional expression and a slightly tedious conditionexpression is nothing more than declaring a parameter and calling the service. retrievemultiple. After understanding this process, we can easily host the aboveCodeTranslation into JavaScript code. What's different is that we need to splice it into an evelop message in JavaScript (because the proxy class helps us process this process on the server side, but we cannot get it in Javascript, so it must be created manually ). The following code demonstrates how to obtain a group of account objects:

// Call web service to retrieve the account

VaR xml = "" +

"<? XML version = \ "1.0 \" encoding = \ "UTF-8 \"?> "+

"<Soap: envelope xmlns: Soap = \" http://schemas.xmlsoap.org/soap/envelope// "xmlns: xsi = \" http://www.w3.org/2001/XMLSchema-instance/ "xmlns: XSD = \" http://www.w3.org/2001/XMLSchema/ ">" +

Generateauthenticationheader () +

"<Soap: Body>" +

"<Retrievemultiple xmlns = \" http://schemas.microsoft.com/crm/2007/WebServices/ ">" +

"<Query xmlns: Q1 = \" http://schemas.microsoft.com/crm/2006/Query/ "xsi: TYPE = \" Q1: queryexpression \ ">" +

"<Q1: entityname> account </Q1: entityname>" +

"<Q1: columnset xsi: TYPE = \" Q1: columnset \ ">" +

"<Q1: Attributes>" +

"<Q1: attribute> name </Q1: attribute>" +

"<Q1: attribute> accountid </Q1: attribute>" +

"</Q1: Attributes>" +

"</Q1: columnset>" +

"<Q1: distinct> false </Q1: distinct>" +

"<Q1: Criteria>" +

"<Q1: filteroperator> and </Q1: filteroperator>" +

"<Q1: conditions>" +

"<Q1: Condition>" +

"<Q1: attributename> firstname </Q1: attributename>" +

"<Q1: Operator> like </Q1: Operator>" +

"<Q1: values>" +

"<Q1: Value xsi: TYPE = \" XSD: String \ "> % A % </Q1: value>" +

"</Q1: values>" +

"</Q1: Condition>" +

"</Q1: conditions>" +

"</Q1: Criteria>" +

"</Query>" +

"</Retrievemultiple>" +

"</Soap: Body>" +

"</Soap: envelope>" +

"";

YesNote that you must add a reference to the global methode generateauthenticationheader () provided by CRM. It will add the authentication code to this request code. We still only need to generate the query and declare the columns to be returned (if you want to return all columns, you can select <Q1: columnset xsi: TYPE = \ "Q1: allcolumn \"> "), then add filter conditions to the request. Next, you can send the email to the web service and wait for the notification:

VaR XMLHttpRequest = new activexobject ("msxml2.xmlhttp ");

XMLHttpRequest. Open ("Post", "/mscrmservices/2007/crmservice. asmx", false );
XMLHttpRequest. setRequestHeader ("soapaction ","Http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
XMLHttpRequest. setRequestHeader ("Content-Type", "text/XML; charset = UTF-8 ");
XMLHttpRequest. setRequestHeader ("Content-Length", XML. Length );
XMLHttpRequest. Send (XML );

VaR resultxml = XMLHttpRequest. responsexml;
Alert (resultxml. XML );

The returned results are also encapsulated in XML format. The next step is to parse the returned resultxml.

VaREntitynodes = resultxml. selectnodes ("// retrievemultipleresult/businessentities/businessentitity ");

For(VaRI = 0; I <entitynodes. length; I ++ ){

// Access the current array element
VaREntitynode = entitynodes [I];

VaRAccountidnode = entitynode. selectsinglenode ("Q1: accountid ");
VaRNamenode = entitynode. selectsinglenode ("Q1: Name ");


VaRAccountid = (accountidnode =Null)?Null: Accountidnode. text;
VaRName = (namenode =Null)?Null: Namenode. text;

// Finally display the values.
Alert(Name + "," + accountid );
}

Some of the inconvenience is that we usually need to use multiple objects (linkentity), multiple filtering conditions, and so on. It is difficult to write JavaScript that is case sensitive and not automatically aware. Now that we have learned how to use the client to call CRM web service, we will introduce a tool to simplify the above problems:FetchxmlwizardTool to generate the request XML (the syntax used to retrieve objects is called fetchxml), and send the verified fetchxmlJavascript Web Service CILSTool to generate standard client-side JavaScript code. You can download these two tools at http://www.stunnware.com. The following describes how to use these two tools:

1. Open the fetchxmlwizard.exe file first, enter the relevant information in the pop-up window to connect to the CRM server, and then confirm.

2. In the blank area, right-click and select Add main entity (only choice now). Then, select an entity in the select object box that appears. Here, select account.

3. Right-click the account and select attributes. In the displayed dialog box, select the attributes you want to select.

4. Next we can right-click to add anything we want, such as link entity and add filter, which are relatively simple. We will not outline them one by one. One thing to mention is Add filter. After adding filter, We need to right-click to add condition to the filter, because a filter actually contains multiple condition and there is a relationship between each condition. Select the corresponding fields and relationships in the specify condition form and enter your condition values. here we can see many relationships (the relationships will change depending on the type)

5. after everything is done, go to the fetch XML tab page and we will see the generated fetchxml. Here we have already succeeded more than half (you can still choose (query-> execute) or F5 to verify your results .)

6. finally, we handed the generated fetchxml to the Javascript web service call tool to generate JavaScript code. this tool is. net project, you first need to open the solution and change its correct reference to the Web service, and in the app. modify the address of the CRM server correctly in config.




......




serializeas = "string">
http: // SERVER_NAME /mscrmservices/2007/crmservice. asmx



Then runProgramCopy the fetchxml generated above to the request column and click Start.

OK, I'm done. Check your JavaScript code :)

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.