- September 22, 2016
- Posted by: Scaleable Solutions
- Category: SDK.Soap
In this blog series, we are going to demonstrate how to use SDK.SOAP.js with Microsoft Dynamics CRM. The purpose of this blog series is to ease out the barriers and difficulties developers face while using SDK.SOAP.js. As the SOAP endpoint is still a first-class citizen in CRM 2016, however, the 2011 REST endpoint has officially been marked as deprecated. SDK.Soap.js makes writing JavaScript code for data operations much like using C#. We can use this library to write JavaScript code that can perform actions using the Microsoft Dynamics CRM Modern Soap endpoint.
In this post, we would like to share details about the Sdk.Soap.js library and sample code for CRUD Operations.
Add Required Libraries to Solution:
- You can download SDK.SOAP from here
- Create a new web resource in the desired solution. Name the web resource and specify its type as “JavaScript”. Then add “Sdk.Soap.vsdoc.js” or “Sdk.Soap.min.js” file.
- Create a new web resource in the desired solution. Name the web resource and specify its type as “JavaScript”. Then add “Sdk.Create.js” from “messages” folder.
- Create a new web resource in the desired solution. Name the web resource and specify its type as “JavaScript”. Then add “Sdk.Update.js” from “messages” folder.
- Create a new web resource in the desired solution. Name the web resource and specify its type as “JavaScript”. Then add “Sdk.Delete.js” from “messages” folder.
- Create a new web resource in the desired solution. Name the web resource and specify its type as “JavaScript”. Then add “Sdk.Retrieve.js” from “messages” folder.
- Include the “Sdk.soap.js” and required messages to your Form or Html Web-resource.
1. Create:
- In the case of create entity we first create the entity type Sdk.Entity (). This function will receive a function parameter which will be the entity logical name to create. In our case it will be “account”.
var testAccount = new Sdk.Entity("account");
- Now we will add the attribute to the newly instantiated entity using Entity.addAttribute(). In our case we will only add the name to newly instantiated entity.
- Now instantiate a Service Request to create an account record and pass the instantiated account entity to the CreateRequest as a parameter.
new Sdk.CreateRequest(account)
- Use the Sdk.Async() method to execute the Service Request in Async Mode.
- We can also execute the request in sync mode. But it is not the recommended way.
Complete function call to create entity
var response = Sdk.Sync. execute(createReq);
function CreateAccount() { //Instantiate Entity to Create var testAccount = new Sdk.Entity("account"); //Add attributes to the entity testAccount.addAttribute(new Sdk.String("name", "Account Test 1")); //Create Organization Request var createReq = new Sdk.CreateRequest(testAccount); //Execute using Async Mode Sdk.Async.execute(createReq, function (res) { //Get Organization Respose console.log(res.getId()); alert("Record Created"); }, function (e) { //Call back function for Error Handling console.log(e.message); }); }
2. Retrieve Record:
- To retrieve entity record we first create the entity Reference using newEntityReference (). This function will receive a function parameter which will be the entity logical name and second parameter will be Record GUID which we want to retrieve. In our case logical name will be “account”.
- Now we will specify the fields of record to retrieve. It will be comma separated values mentioning the Logical Name of the field. Column Set will be instantiated using “new Sdk.ColumnSet()”
- Use the Sdk.Async() method to execute the retrieve Request in Async Mode.
Complete function call to retrieve entity data
function RetrieveSingleRecord() { //Create Entity Refrence var target = new Sdk.EntityReference("account", "475b158c-541c-e511-80d3-3863bb347ba8"); //Specify Column Set var columnSet = new Sdk.ColumnSet("name", "emailaddress1", "address1_postalcode"); //Execute Request in Async Mode Sdk.Async.retrieve("account", "475b158c-541c-e511-80d3-3863bb347ba8", columnSet, function (account) { //Get Responce console.log(account.getValue("name")); console.log(account.getValue("emailaddress1")); console.log(account.getValue("address1_postalcode")); }); }
3. Update:
- To update an existing record we have to create an entity using Sdk.Entity(). The function will receive the logical name of entity we want to update. In our case it will be “account”. “new Sdk.Entity(“account”);”
- Add attributes to the instantiated entity. We will need to specify the account GUID of type Sdk.Guid() to find the record.
account1.addAttribute(new Sdk.Guid("accountid", "fa4ae639-fa5e-e611-80ec-5065f38915a1")); account1.addAttribute(new Sdk.String("name", "Account Test Updated"));
- Instantiate a Service Request to update the record. And pass the instantiated entity to the Service Request.
new Sdk.UpdateRequest(account1);
- Use the Sdk.Async() method to execute the Service Request in Async Mode.
Complete function call to update entity record
function update() { //Instantiate account entity var account1 = new Sdk.Entity("account"); //add the required attributes account1.addAttribute(new Sdk.Guid("accountid", "fa4ae639-fa5e-e611-80ec-5065f38915a1")); account1.addAttribute(new Sdk.String("name", "Account Test Updated")); //Create Organization Service Request var updateReq = new Sdk.UpdateRequest(account1); //Execute the request Sdk.Async.execute(updateReq, function (res) { if (res.type == "Sdk.OrganizationResponse") { alert("Successfully Updated"); } }, //Call back function for Error Handling function (error) { console.log(e.message); }); }
4. Delete:
- To delete a record create an entity refrence using “new Sdk.EntityReference()” Pass enitity logical name as the first parameter and record GUID as the second parameter.
- Create a Service Request to delete a record. “new Sdk.DeleteRequest(account1);”
- Use the Sdk.Async() method to execute the Service Request in Async Mode.
Complete function call to delete entity record
function deleteRec() { //Instantiate Entity Reference of entity to be deleted var account1 = new Sdk.EntityReference("account", "fa4ae639-fa5e-e611-80ec-5065f38915a1"); //Create Organization Request var deleteReq = new Sdk.DeleteRequest(account1); //Execute using Async Mode Sdk.Async.execute(deleteReq, function (res) { //Get Organization Respose if (res.type == "Sdk.OrganizationResponse") { alert("Successfully Deleted"); } }, function (error) { //Call back function for Error Handling console.log(e.message); }); }
In this blog post we have performed CRUD operations for single record. SDK.Soap.js also gives us privilege to perform bulk operations. So, in our next post of this series we will perform bulk operations for CRUD operations.
[…] is a second post of this series of blog posts, Previously we have described CRUD operations using SDK.SOAP.Js. In this blog post we would like to share details of Bulk Operations(Bulk Update, Bulk Delete) […]
[…] is a 3rd post of this series of blog posts, Previously we have described CRUD operations using SDK.SOAP.Js and Bulk Operations using SDK.SOAP in Javascript. In this blog post we would like to share details […]
[…] is a 3rd post of this series of blog posts, Previously we have described CRUD operations using SDK.SOAP.Js and Bulk Operations using SDK.SOAP in Javascript. In this blog post we would like to share details […]