Calling Asp.NET web service from javascript (Ajax)-(Passing Parameter-GET)


Now we will modify the application that we developed in the previous post to work with parameters

1) First we will modify the default Hello World service to accept a parameter like this


public string HelloWorld(string Name)


return “Hello “+Name+”!”;


2) Add the html textbox control in the webform

<INPUT type=”text” id=”Info”>

3) Add the following in the body

<BODY onload=”getMessage()”>

4) Put the following script in our webpage.

<script language=”javascript”>

var xmlHttp;

function getMessage()


xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”);“get”, “http://localhost/WebService1/Service1.asmx/HelloWorld? name=’Nishant'”, true);


xmlHttp.send(); return false;


function doUpdate()




var xmlDoc=xmlHttp.responseXML;

var responseElement=xmlDoc.getElementsByTagName(“string”)[0];

var respText=responseElement.firstChild.nodeValue;





5) We have changed the method of passing the data to server from post to get as we are passing the value for Name by appending it in the url.

6) Right now if we run the application it will give us error as “object not found”.

7) The reason for the error is that we have to make our web service configured for get method.

8- We need to open web.config of our web service and add the following line inside system.web section



<add name=”HttpGet”/>



9) We need to build the service again

10) This time our application should work without any error.


Calling Asp.NET web service from javascript (Ajax)

Here we will be calling the default helloworld webservice that is already created for us when we open a webservice project template in Visual Studio.

We’ll just have a textbox(html control) which will display us the Hello World! response returned from the web service.

This is the step we need to follow

1) Create a new ASP.NET WebApplication

2) Put a html textbox control on the form

<input type=”text” id=”info”/>

3) Put this script code in the head section of the aspx page

<script language=”javascript”>

var xmlHttp;

function getMessage()


xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”);“post”, “http://localhost/WebService1/Service1.asmx/HelloWorld&#8221;, false);



return false;


function doUpdate()




var startTag = “<string xmlns=\”\”>”;

var endTag = “</string>”;

var exch;

var valueStart = 0;

var valueEnd = 0;

valueStart = xmlHttp.responseXML.xml.indexOf(startTag, valueEnd) + startTag.length;

valueEnd = xmlHttp.responseXml.xml.indexOf(endTag, valueEnd+1);

exch = xmlHttp.responseXML.xml.substring(valueStart, valueEnd);




4) Call the getMessage function in the body’s onLoad eventHandler

<BODY onload=”getMessage()”>

5) Run the applicaton. We’ll see the HelloWorld! text in our textbox(‘info’)

Now let us understand what is happening inside the javascript code

xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”)

This line of code creates the XMLHttpRequest object. This object sends request to the server and processes the responses from it.

The above code creates the object specific to Internet Explorer( <=6.o).

It is implemented as Active X for IE. However in IE 7 XMLHttpRequest will come as native JavaScript object.

For other browsers we can write

xmlHttp=new XMLHttpRequest();

or best we can write this



xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”);


else if (window.XmlHttpRequest)


xmlHttp=new XMLHttpRequest();

}“post”, “http://localhost/WebService1/Service1.asmx/HelloWorld&#8221;, false);

The open method initializes the connection to the server and informs the xmlHttp object how to connect to the server.

post- it indicates how we want to send the data. It can be “get” as well

url- comes the url where we are connecting

false- this means we are making a synchronous call. To make asychronous call simply set it to true


This specifies the name of the function to be called whenever the state of the xmlHttpRequest changes


Send method than makes the request to server. This method would return immediately in case of asynchronous call and it would block until the synchronous response is received from the server.


It tells about the current state of the request

0- uninitialized

1- loading

2- loaded




It returns the current response from server in XML=

<?xml version=”1.0″?>

<string xmlns=””>Hello World !</string>

Than we are using some javascript functions to get the Hello World! and remove everything else.


Finally assigning the value to our textBox.

The function doUpdate() can be written differently so that we don’t have to make use of any string functions.

function doUpdate()




// Here the server is returning us XML in response so we can make use of responseXML

// Here the browser creates a DOM tree to represent that XML and puts a reference to that DOM tree

// in the request’s (xmlHttp) object’s responseXML

var xmlDoc=xmlHttp.responseXML;

var responseElement=xmlDoc.getElementsByTagName(“string”)[0];

var respText=responseElement.firstChild.nodeValue;




However creating a ASP.NET AJAX webservice is little different than our normal ASP.NET web service, and calling them using the ASP.NET AJAX client library is also a bit different


Refresh CRM form from third-party applications

Code to put in the javascript of our custom page

function window.onunload()

But the thing with the code is that it only works when we deploy the page in the same server where our Crm is deployed.

When i had deployed the page in my machine, the page wasn’t refreshing but when i deployed the same page where the crm is deployed everything was working perfectly fine.

I think in the previous case because of the cross-domain issue i guess there were problems.

One more thing,  to assign values to attributes in our Crm Form from our isv page we can make use of familiar syntax

window.opener.document.crmForm.all.new_totalbilling.DataValue=parseInt(//value );

parseInt – if the crm attribute is of numeric type



Finding shared records in CRM

To find all the records(say lead)  shared with any particular user we can make use of following query

It will return us all records(lead) shared with the user directly( i.e. through Action–>Sharing and user)

select  fl.subject

PrincipalObjectAccess poa , FilteredLead fl, FilteredSystemUser fsu
poa.ObjectTypeCode = 4
and poa.ObjectId = fl.leadid
and poa.PrincipalId = fsu.systemuserid
and fsu.domainname=SYSTEM_USER

// fsu.fullname=’name of user’

And to get the records shared with the user indirectly (i.e. through Action–>Sharing and Team (user belongs to that team)

select  fl.subject
FilteredLead fl,  PrincipalObjectAccess poa,  FilteredTeam ft
poa.ObjectTypeCode = 4 

and poa.ObjectId =fl.leadid
and poa.PrincipalId = ft.teamid and 

fl.owneridname not in (select fullname from filteredsystemuser
where domainname=SYSTEM_USER)
ft.teamid  in (select ft.teamid from filteredteammembership fm
, filteredsystemuser fsu, filteredteam ft
where fm.systemuserid=fsu.systemuserid and
and fsu.domainname=SYSTEM_USER


Making value in one lookup depended on value of another lookup.

Making value in one lookup depended on value of another lookup in Microsoft CRM. Say we have created 2 custom entities Categories and Skill. Skill entity has category lookup field which relates it to that category.(Added a relationship between them)Than created few records for both the entities. After that we have added that entities to the third entity where they’ll appear as LookUp.

Now we want the value of Skill lookup to be dependent value selected on Resource(Category) LookUp.


Resource can have following values


Now if Datawarehousing is selected than

we want to show Analysis and SSAS in the Skill lookup


To accomplish this we will write the following code in the OnChange event of Resource Lookup

// Making the skill id lookup null whenver a new values is selected for reesource


crmForm.all.new_skillid.disabled = false;

// getting the value of selected resource

var ar=new Array();


var rID = ar[0].id;

// Changing the display of skillid lookup so that only skill belonging to particular rID should appear


// in the additionalparams for the skill id attribute we will specify the fetch xml query // e.g. select * from new_categoryskill (name of our custom skill entity) where new_categoryofskillid = rid

crmForm.all.new_skillid.additionalparams = “fetchXml=<fetch mapping=’logical’ ><entity name=’new_categoryskill’><all-attributes/><filter type=’and’><condition attribute=’new_categoryofskillid’ operator=’eq’ value='” + rID + “‘/></filter></entity></fetch>”;

To get the fetchXml query what we can do over here is

We will go to our skill entity form and run an advancedfind query against it (giving the appropriate condition for which we want the fetch xml)

Now pressing CTRL+N open it in a new window Now in URl replace everything with the following line


Press Ok to the dialog that appears and it opens a prompt window from where we can copy our fetch xml and modify it accordingly.

Using Left and CharIndex in Oracle

Well i was given the task to get the email id’s of the user which was stored in one of our oracle db table.

But the problem was that we wanted that part of emailid which  appears before ‘@’ .

Well coming from SQL Server background i thought it could be acheived using CharIndex and Left Function.

Let’s see what they do

Select Left(‘abcdef’,3)

-> abc


select charindex(‘c’,’abcde’)

-> 3

But than as expected there were no functions like charindex and left in Oracle.

After searching i finally managed to found the solution

Inplace of CharIndex we have instr function

select instr(‘ab’,’b’) from dual;


and for left and also right we have

SUBSTR (`ABCDEF’,-5); //Right(..)

SUBSTR (`ABCDEF’,1,5); // Left(…

So finally the query was


-1 is used otherwise @ will also come along

And one more thing, to extract username portion from login name i.e  nishantr1 from

abccompany\nishantr1 we could write something as following

SUBSTRING(loginname ,charindex(‘\’,loginname)+1, len(loginname)) for sql server.