Using AJAX and SOAP to create a CRM 2011 activity

I have posted a bunch of PowerShell scripts to interact with CRM 2011, now I am going to put up similar javascript versions. I feel that there is a lot of javascript content out there for CRM 2011, but most of it is javascript inside the actual CRM interface. I don’t see a lot of content about using javascript from a different webpage/site with ajax, so I thought I would post some of the code I put together.

This is a function that I put together to build a SOAP envelope for creating an activity in CRM 2011. Credit for the template goes to Jamie Miley and this post. I took his template a little further and created a function for either email,Phone call or appointment. In addition this function allows for multiple contacts in an phone call’s to and multiple required contacts in a meeting.

function BuildSOAPEnvelope(EntityType, strSubject, strDescription, strLocation,stringRegardingGUID, strOwnerGUID, strScheduledStart, strScheduledEnd, otherAttendees) {
        var requestMain = ""
        requestMain += "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
        requestMain += "  <s:Body>";
        requestMain += "    <Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
        requestMain += "      <request i:type=\"a:CreateRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\">";
        requestMain += "        <a:Parameters xmlns:b=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
        requestMain += "          <a:KeyValuePairOfstringanyType>";
        requestMain += "            <b:key>Target</b:key>";
        requestMain += "            <b:value i:type=\"a:Entity\">";
        requestMain += "              <a:Attributes>";
        // subject
        requestMain += "                <a:KeyValuePairOfstringanyType>";
        requestMain += "                  <b:key>subject</b:key>";
        requestMain += "                  <b:value i:type=\"c:string\" xmlns:c=\"http://www.w3.org/2001/XMLSchema\">"+strSubject+"</b:value>";
        requestMain += "                </a:KeyValuePairOfstringanyType>";
        // description
        requestMain += "                <a:KeyValuePairOfstringanyType>";
        requestMain += "                  <b:key>description</b:key>";
        requestMain += "                  <b:value i:type=\"c:string\" xmlns:c=\"http://www.w3.org/2001/XMLSchema\">"+strDescription+"</b:value>";
        requestMain += "                </a:KeyValuePairOfstringanyType>";
        if (EntityType == "phonecall") {
            //recipient
            requestMain += "                <a:KeyValuePairOfstringanyType>";
            requestMain += "                  <b:key>to</b:key>";
            requestMain += "                  <b:value i:type=\"a:ArrayOfEntity\">";
            $.each(otherAttendees, function () {
                requestMain += "                    <a:Entity>";
                requestMain += "                      <a:Attributes>";
                requestMain += "                        <a:KeyValuePairOfstringanyType>";
                requestMain += "                          <b:key>partyid</b:key>";
                requestMain += "                          <b:value i:type=\"a:EntityReference\">";
                requestMain += "                            <a:Id>" + stringRegardingGUID + "</a:Id>";
                requestMain += "                            <a:LogicalName>contact</a:LogicalName>";
                requestMain += "                            <a:Name i:nil=\"true\" />";
                requestMain += "                          </b:value>";
                requestMain += "                        </a:KeyValuePairOfstringanyType>";
                requestMain += "                      </a:Attributes>";
                requestMain += "                      <a:EntityState i:nil=\"true\" />";
                requestMain += "                      <a:FormattedValues />";
                requestMain += "                      <a:Id>00000000-0000-0000-0000-000000000000</a:Id>";
                requestMain += "                      <a:LogicalName>activityparty</a:LogicalName>";
                requestMain += "                      <a:RelatedEntities />";
                requestMain += "                    </a:Entity>";
            });
            requestMain += "                  </b:value>";
            requestMain += "                </a:KeyValuePairOfstringanyType>";
            //SS
            requestMain += "                    <a:KeyValuePairOfstringanyType>";
            requestMain += "                      <b:key>scheduledstart</b:key>";
            requestMain += "                      <b:value i:type=\"d:dateTime\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">"+strScheduledStart+"</b:value>";
            requestMain += "                    </a:KeyValuePairOfstringanyType>";
            //SE
            requestMain += "                    <a:KeyValuePairOfstringanyType>";
            requestMain += "                      <b:key>scheduledend</b:key>";
            requestMain += "                      <b:value i:type=\"d:dateTime\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">"+strScheduledEnd+"</b:value>";
            requestMain += "                    </a:KeyValuePairOfstringanyType>";
        }
        if (EntityType == "email") {
            //to
            requestMain += "                <a:KeyValuePairOfstringanyType>";
            requestMain += "                  <b:key>to</b:key>";
            requestMain += "                  <b:value i:type=\"a:ArrayOfEntity\">";
            requestMain += "                    <a:Entity>";
            requestMain += "                      <a:Attributes>";
            requestMain += "                        <a:KeyValuePairOfstringanyType>";
            requestMain += "                          <b:key>partyid</b:key>";
            requestMain += "                          <b:value i:type=\"a:EntityReference\">";
            requestMain += "                            <a:Id>" + stringRegardingGUID + "</a:Id>";
            requestMain += "                            <a:LogicalName>contact</a:LogicalName>";
            requestMain += "                            <a:Name i:nil=\"true\" />";
            requestMain += "                          </b:value>";
            requestMain += "                        </a:KeyValuePairOfstringanyType>";
            requestMain += "                      </a:Attributes>";
            requestMain += "                      <a:EntityState i:nil=\"true\" />";
            requestMain += "                      <a:FormattedValues />";
            requestMain += "                      <a:Id>00000000-0000-0000-0000-000000000000</a:Id>";
            requestMain += "                      <a:LogicalName>activityparty</a:LogicalName>";
            requestMain += "                      <a:RelatedEntities />";
            requestMain += "                    </a:Entity>";
            requestMain += "                  </b:value>";
            requestMain += "                </a:KeyValuePairOfstringanyType>";
            // from 
            requestMain += "                <a:KeyValuePairOfstringanyType>";
            requestMain += "                  <b:key>from</b:key>";
            requestMain += "                  <b:value i:type=\"a:ArrayOfEntity\">";
            requestMain += "                    <a:Entity>";
            requestMain += "                      <a:Attributes>";
            requestMain += "                        <a:KeyValuePairOfstringanyType>";
            requestMain += "                          <b:key>partyid</b:key>";
            requestMain += "                          <b:value i:type=\"a:EntityReference\">";
            requestMain += "                            <a:Id>" + strOwnerGUID + "</a:Id>";
            requestMain += "                            <a:LogicalName>systemuser</a:LogicalName>";
            requestMain += "                            <a:Name i:nil=\"true\" />";
            requestMain += "                          </b:value>";
            requestMain += "                        </a:KeyValuePairOfstringanyType>";
            requestMain += "                      </a:Attributes>";
            requestMain += "                      <a:EntityState i:nil=\"true\" />";
            requestMain += "                      <a:FormattedValues />";
            requestMain += "                      <a:Id>00000000-0000-0000-0000-000000000000</a:Id>";
            requestMain += "                      <a:LogicalName>activityparty</a:LogicalName>";
            requestMain += "                      <a:RelatedEntities />";
            requestMain += "                    </a:Entity>";
            requestMain += "                  </b:value>";
            requestMain += "                </a:KeyValuePairOfstringanyType>";
            // cc
            if (otherAttendees != '') {
            requestMain += "                <a:KeyValuePairOfstringanyType>";
            requestMain += "                  <b:key>cc</b:key>";
            requestMain += "                  <b:value i:type=\"a:ArrayOfEntity\">";
                $.each(otherAttendees, function () {
                    requestMain += "                    <a:Entity>";
                    requestMain += "                      <a:Attributes>";
                    requestMain += "                        <a:KeyValuePairOfstringanyType>";
                    requestMain += "                          <b:key>partyid</b:key>";
                    requestMain += "                          <b:value i:type=\"a:EntityReference\">";
                    requestMain += "                            <a:Id>" + this + "</a:Id>";
                    requestMain += "                            <a:LogicalName>contact</a:LogicalName>";
                    requestMain += "                            <a:Name i:nil=\"true\" />";
                    requestMain += "                          </b:value>";
                    requestMain += "                        </a:KeyValuePairOfstringanyType>";
                    requestMain += "                      </a:Attributes>";
                    requestMain += "                      <a:EntityState i:nil=\"true\" />";
                    requestMain += "                      <a:FormattedValues />";
                    requestMain += "                      <a:Id>00000000-0000-0000-0000-000000000000</a:Id>";
                    requestMain += "                      <a:LogicalName>activityparty</a:LogicalName>";
                    requestMain += "                      <a:RelatedEntities />";
                    requestMain += "                    </a:Entity>";
                });
            requestMain += "                  </b:value>";
            requestMain += "                </a:KeyValuePairOfstringanyType>";
            }
            //SS
            requestMain += "                    <a:KeyValuePairOfstringanyType>";
            requestMain += "                      <b:key>scheduledstart</b:key>";
            requestMain += "                      <b:value i:type=\"d:dateTime\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">"+strScheduledStart+"</b:value>";
            requestMain += "                    </a:KeyValuePairOfstringanyType>";
            //SE
            requestMain += "                    <a:KeyValuePairOfstringanyType>";
            requestMain += "                      <b:key>scheduledend</b:key>";
            requestMain += "                      <b:value i:type=\"d:dateTime\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">" + strScheduledEnd + "</b:value>";
            requestMain += "                    </a:KeyValuePairOfstringanyType>";
        }
        if (EntityType == "appointment"){
            //SS
            requestMain += "                    <a:KeyValuePairOfstringanyType>";
            requestMain += "                      <b:key>scheduledstart</b:key>";
            requestMain += "                      <b:value i:type=\"d:dateTime\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">"+strScheduledStart+"</b:value>";
            requestMain += "                    </a:KeyValuePairOfstringanyType>";
            //SE
            requestMain += "                    <a:KeyValuePairOfstringanyType>";
            requestMain += "                      <b:key>scheduledend</b:key>";
            requestMain += "                      <b:value i:type=\"d:dateTime\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">"+strScheduledEnd+"</b:value>";
            requestMain += "                    </a:KeyValuePairOfstringanyType>";

            // Organizer
            requestMain += "                <a:KeyValuePairOfstringanyType>";
            requestMain += "                  <b:key>organizer</b:key>";
            requestMain += "                  <b:value i:type=\"a:ArrayOfEntity\">";
            requestMain += "                    <a:Entity>";
            requestMain += "                      <a:Attributes>";
            requestMain += "                        <a:KeyValuePairOfstringanyType>";
            requestMain += "                          <b:key>partyid</b:key>";
            requestMain += "                          <b:value i:type=\"a:EntityReference\">";
            requestMain += "                            <a:Id>"+strOwnerGUID+"</a:Id>";
            requestMain += "                            <a:LogicalName>systemuser</a:LogicalName>";
            requestMain += "                            <a:Name i:nil=\"true\" />";
            requestMain += "                          </b:value>";
            requestMain += "                        </a:KeyValuePairOfstringanyType>";
            requestMain += "                      </a:Attributes>";
            requestMain += "                      <a:EntityState i:nil=\"true\" />";
            requestMain += "                      <a:FormattedValues />";
            requestMain += "                      <a:Id>00000000-0000-0000-0000-000000000000</a:Id>";
            requestMain += "                      <a:LogicalName>activityparty</a:LogicalName>";
            requestMain += "                      <a:RelatedEntities />";
            requestMain += "                    </a:Entity>";
            requestMain += "                  </b:value>";
            requestMain += "                </a:KeyValuePairOfstringanyType>";

            // location
            requestMain += "                <a:KeyValuePairOfstringanyType>";
            requestMain += "                  <b:key>location</b:key>";
            requestMain += "                  <b:value i:type=\"c:string\" xmlns:c=\"http://www.w3.org/2001/XMLSchema\">"+strLocation+"</b:value>";
            requestMain += "                </a:KeyValuePairOfstringanyType>";

            // Required 
            requestMain += "                <a:KeyValuePairOfstringanyType>";
            requestMain += "                  <b:key>requiredattendees</b:key>";
            requestMain += "                  <b:value i:type=\"a:ArrayOfEntity\">";
            if (otherAttendees != '') {
                $.each(otherAttendees, function () {
                    requestMain += "                    <a:Entity>";
                    requestMain += "                      <a:Attributes>";
                    requestMain += "                        <a:KeyValuePairOfstringanyType>";
                    requestMain += "                          <b:key>partyid</b:key>";
                    requestMain += "                          <b:value i:type=\"a:EntityReference\">";
                    requestMain += "                            <a:Id>" + this + "</a:Id>";
                    requestMain += "                            <a:LogicalName>contact</a:LogicalName>";
                    requestMain += "                            <a:Name i:nil=\"true\" />";
                    requestMain += "                          </b:value>";
                    requestMain += "                        </a:KeyValuePairOfstringanyType>";
                    requestMain += "                      </a:Attributes>";
                    requestMain += "                      <a:EntityState i:nil=\"true\" />";
                    requestMain += "                      <a:FormattedValues />";
                    requestMain += "                      <a:Id>00000000-0000-0000-0000-000000000000</a:Id>";
                    requestMain += "                      <a:LogicalName>activityparty</a:LogicalName>";
                    requestMain += "                      <a:RelatedEntities />";
                    requestMain += "                    </a:Entity>";
                });
            }
            requestMain += "                  </b:value>";
            requestMain += "                </a:KeyValuePairOfstringanyType>";
        }
        // Regarding
        requestMain += "                <a:KeyValuePairOfstringanyType>";
        requestMain += "                  <b:key>regardingobjectid</b:key>";
        requestMain += "                  <b:value i:type=\"a:EntityReference\">";
        requestMain += "                    <a:Id>"+stringRegardingGUID+"</a:Id>";
        requestMain += "                    <a:LogicalName>contact</a:LogicalName>";
        requestMain += "                    <a:Name i:nil=\"true\" />";
        requestMain += "                  </b:value>";
        requestMain += "                </a:KeyValuePairOfstringanyType>";
        // Owner
        requestMain += "                <a:KeyValuePairOfstringanyType>";
        requestMain += "                  <b:key>ownerid</b:key>";
        requestMain += "                  <b:value i:type=\"a:EntityReference\">";
        requestMain += "                    <a:Id>"+strOwnerGUID+"</a:Id>";
        requestMain += "                    <a:LogicalName>systemuser</a:LogicalName>";
        requestMain += "                    <a:Name i:nil=\"true\" />";
        requestMain += "                  </b:value>";
        requestMain += "                </a:KeyValuePairOfstringanyType>";
        // EntityType
        requestMain += "              </a:Attributes>";
        requestMain += "              <a:EntityState i:nil=\"true\" />";
        requestMain += "              <a:FormattedValues />";
        requestMain += "              <a:Id>00000000-0000-0000-0000-000000000000</a:Id>";
        requestMain += "              <a:LogicalName>"+EntityType+"</a:LogicalName>";
        requestMain += "              <a:RelatedEntities />";
        requestMain += "            </b:value>";
        requestMain += "          </a:KeyValuePairOfstringanyType>";
        requestMain += "        </a:Parameters>";
        requestMain += "        <a:RequestId i:nil=\"true\" />";
        requestMain += "        <a:RequestName>Create</a:RequestName>";
        requestMain += "      </request>";
        requestMain += "    </Execute>";
        requestMain += "  </s:Body>";
        requestMain += "</s:Envelope>";
        return requestMain;
    }

,

Trackbacks/Pingbacks

  1. A simple javascript/AJAX function to post a SOAP request to CRM 2011 | jbmurphy.com - February 19, 2013

    […] I use to post a SOAP envelope to CRM 2011. Just pass the URL and the xml (you can create that with this function) and you should be good to […]

Powered by WordPress. Designed by WooThemes