Muhammad Ali Khan

MS CRM 3.0, MS CRM 4.0, Sharepoint 2007 & Sharepoint 2010

Using XmlHttp or Ajax With MS CRM 4.0

Posted by Ali Khan on July 21, 2009

In this post i will show you how to use the XmlHTTP Object with MS CRM 4.0 in javascript. Thanks to my project of Zoomio. I have played a lot with XmlHttp object & Javascript .

So here is how we will start…

Add a new tool bar button to the custom entity  “contact” entity, this button once clicked will open the account lookup dialog (as shown below). Once clicked on “ok”, we will update the contact entity’s mobile no and bussiness phone no from the mobile & business phone no of the account.

2

 

So here is the javascript code to be put in the ISV.Config File.

if(crmForm.IsDirty)

{

alert(‘Please Save the Contact before updating the Phone No’);

}

else

{

var str = window.showModalDialog(‘/MicrosoftCRM/_controls/lookup/lookupsingle.aspx?class=null&objecttypes=1&browse=0&DefaultType=0′,”,’dialogHeight: 450px; dialogWidth: 600px; edge: Raised; center: Yes; resizable: Yes; status: Yes;’);

                             

      if(str!=null)

      {

            if(str.items.length>0)

            {

                  var accountID = str.items[0].id;

                  var contactID = crmForm.ObjectId;

                                         

var forwardPath = ‘http://localhost/MyAjaxApp/ajax_page.aspx?contactID=’ + contactID + ‘&accountID=‘ + accountID;

                                         

var objXMLHTTP=new ActiveXObject(‘Microsoft.XMLHTTP’);

  

                  objXMLHTTP.Open(‘POST’,forwardPath,false);

                                         

                  objXMLHTTP.Send(”);

                                         

                  var response=objXMLHTTP.ResponseText;

                  if(response==’success’)

                        window.location.reload(true);

            }

      }

}

And here is the complete xml of the contact entity.

<Entity name=contact>

<ToolBar ValidForCreate=0 ValidForUpdate=1>

            <Button Icon=/_imgs/ico_18_debug.gif

            JavaScript=

                        if(crmForm.IsDirty)

                        {

alert(‘Please Save the Contact before updating the Phone No’);

                             

                        }

                        else

                        {

var str = window.showModalDialog(‘/MicrosoftCRM/_controls/lookup/lookupsingle.aspx?class=null&amp;objecttypes=1&amp;browse=0&amp;DefaultType=0′,”,’dialogHeight: 450px; dialogWidth: 600px; edge: Raised; center: Yes; resizable: Yes; status: Yes;’);

                             

                              if(str!=null)

                              {

                                    if(str.items.length>0)

                                    {

                                          var accountID = str.items[0].id;

                                          var contactID = crmForm.ObjectId;

                                         

                                    var forwardPath = ‘http://localhost/MyAjaxApp/ajax_page.aspx?contactID=&#8217; + contactID + ‘&amp;accountID=’ + accountID;

  

var objXMLHTTP=new ActiveXObject(‘Microsoft.XMLHTTP’);

                                                                 

objXMLHTTP.Open(‘POST’,forwardPath,false);

                                         

                  objXMLHTTP.Send(”);

                                         

                  var response=objXMLHTTP.ResponseText;

                  if(response==’success’)

                        window.location.reload(true);

                                    }

                              }

                        }

                       

                        PassParams=1 WinParams=“” WinMode=1>

                                <Titles>

                                      <Title LCID=1033 Text=Update Phone No />

                                </Titles>

                          </Button>

                          <ToolBarSpacer />

                    </ToolBar>

              </Entity>

You many notice the char &amp; for the ampersond (&) character in the javascript xml, this is to avoid the parsing error in xml.

Now create an asp.net applicaton and name it as “MyAjaxApp”, create an “ajax_page.aspx”. Remove all the code from the aspx page with only this line remaining

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”ajax_page.aspx.cs” Inherits=”ajax_page” %>

Now open the “ajax_page.aspx.cs” file and write the following code in page_load method.

CrmAuthenticationToken token = new CrmAuthenticationToken();

token.AuthenticationType = intAuthenticationType;

token.OrganizationName = strOrganizationName;

  

CrmService objService = new CrmService();

objService.Url = strService;

objService.CrmAuthenticationTokenValue = token;

objService.UseDefaultCredentials = true;

objService.Credentials= System.Net.CredentialCache.DefaultNetworkCredentials;

string accountID = Request.QueryString[“accountID”];

string contactID = Request.QueryString[“contactID”];

 

// first reterive the account entity.

ColumnSet cols = new ColumnSet();

cols.Attributes = new string[] { “telephone1”, “telephone2” };

account objAccount = (account)objService.Retrieve(EntityName.account.ToString(), new Guid(accountID), cols);

 

contact objContact = new contact();

objContact.contactid = new Key();

objContact.contactid.Value = new Guid(contactID);

objContact.telephone1 = objAccount.telephone1;

objContact.telephone2 = objAccount.telephone2;

 

objService.Update(objContact);

Response.Write(“success”);

 

That’s All !!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: