Using Ajax in ASP.NET application -2

In part 1 of this post we saw our response returning us a simple text which was holding a single value.

But what if it is returing multiple values and it using some characters like | (pipe) or say semicolon for separating one value from another.

In this case, we need to make use of javascript string functions to get each and every value.

Here then our application should understand what ; and | or say , mean and what if their order changes.

So now we can have our responses send as XML so that we can make use of XMLDOM to parse through the response. Which infact would be quite easy and efficient.

Let’s update our application

https://nishantrana.wordpress.com/2007/10/25/using-ajax-in-aspnet-application/

Things we need to change are

1)  Change doUpdate function

function doUpdate()

{

if(xmlHttp.readyState==4)

{ // replacing reponseText with responseXML to get the xml DOM tree reference 

var xmlDoc=xmlHttp.responseXML; // parsing through the response using dom methods

var firstName=xmlDoc.getElementsByTagName(“first”)[0].firstChild.nodeValue;

var lastName=xmlDoc.getElementsByTagName(“last”)[0].firstChild.nodeValue;

document.getElementById(“lblInfo”).firstChild.nodeValue=firstName+” “+lastName; }

 }

In the above case the response we are recieving from server is this, which is in xml

<?xml version=”1.0″?>
<name>
<first>Mickey</first><last>Mouse</last>
</name>

2) Now we will change Default2.aspx to send us response back in xml.

protected void Page_Load(object sender, EventArgs e){ // this will inform our webpage about responses being sent as XML and not text

 Response.AddHeader(“Content-Type”, “text/xml;charset=UTF-8”);

Response.Write(“<?xml version=\”1.0\” encoding=\”utf-8\”?><name><first>Mickey</first><last>Mouse</last> </name>”);  

}

Now we can run our application and see our label’s text changing to Mickey Mouse. 

Bye

Using Ajax in ASP.NET application -1

Hi,

Here we’ll change the small application that we had written to get reponse from ASP.NET webpage rather than a webservice.

https://nishantrana.wordpress.com/2007/10/18/calling-aspnet-webservice-from-javascript-ajax/

Here we are using a label server control whose value we will be replacing with response from our asp.net web page through ajax call

1)  Create a new ASP.NET WebApplication  

2)  Put a label control on the form (Default.aspx)

<asp:Label ID=”lblInfo” runat=”server” Height=”49px” Text=”Label” Width=”209px”>ReplaceIt</asp:Label>

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

var xmlHttp;

function getMessage()

{

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

xmlHttp.open(“post”, “Default2.aspx”, true);

xmlHttp.onreadystatechange=doUpdate;

xmlHttp.send();  return false; }

function doUpdate() { if(xmlHttp.readyState==4)

{

var exch; exch=xmlHttp.responseText;document.getElementById(“lblInfo”).firstChild.nodeValue=exch;

}

}

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

 <BODY onload=”getMessage()”>

5) Create another page Default2.aspx which will return us  the “Hello World” repsonse.

6) Write this in our Default2.aspx’s page load event

protected void Page_Load(object sender, EventArgs e)

{

Response.Write(“Hello World”);

}

7) Now comes the most imp part i.e. Deleting all the html tags  from our Default2.aspx leaving only this, otherwise the html will also come as a part of response. But we need only the Hello World text.

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

8- Now run the application, we should see our label’s text changing to “Hello World”

Bye

Using ALLColumns() in Dynamics CRM

Hi,

To retireve the values for all the attributes of an entity we can make use of ALLColumns ,

CrmService service=new CrmService();

service.Credentials = System.Net.CredentialCache.DefaultCredentials;

QueryExpression query = new QueryExpression();

query.EntityName = EntityName.new_entityname.ToString();

query.ColumnSet=new AllColumns();

BusinessEntityCollection entities= service.RetrieveMultiple(query);

for(int i=0;i<entities.BusinessEntities.Length;i++)

{

new_entityname nEntity=(new_entityname) entities.BusinessEntities[i];

Response.Write(“<b>”+nEntity.new_id+” -></b>”);

Response.Write(“<i>” + nEntity.createdby.name + ” –></i>”);

}

}

Or by using QueryByAttribute  

It can be used when our query is very simple i.e. multiple equals conditions that are combined using AND.

But remember it doesn’t support OR
QueryByAttribute myQuery=new QueryByAttribute();
myQuery.ColumnSet=new AllColumns();
myQuery.EntityName=EntityName.opportunity.ToString();

    // Find all the opportunities with topic name test1 and city as Redmond
myQuery.Attributes=new string[]{“name”,”address1_city”};
myQuery.Values=new string[]{“Test1″,”Redmond”};
BusinessEntityCollection retrieved=service.RetrieveMultiple(myQuery);

Bye

Query for finding Team and it’s member in MS DYNAMICS CRM

Hi,

We can use this query to retrieve different teams and the users belonging to that team within Microsoft Dyanmics CRM

Select FT.name TeamName,FSU.Fullname UserName, FSU.Systemuserid from
dbo.FilteredSystemUser FSU
inner Join
dbo.FilteredTeamMembership FTM  
on FSU.Systemuserid = FTM.Systemuserid
Inner join  dbo.FilteredTeam FT
on FT.TeamID = FTM.TeamID
ORDER BY FT.NAME

Bye

Configuring ASP.NET Web Service

Hi,

Well today i created a webservice which would fetch me some data from oracle database. The method signature was something like this

public DataSet getEmpInfo

{,,,,,,,,,,,,,}

I was calling this webservice within form’s onLoad inside the a custom entity of Microsoft Dyanmics CRM3,0. 

I deployed it in my machine and when i was opening the my custom entity form inside CRM in my machine, i was able to see the values fetched using ajax calls to webservice.

But for other machines i was getting an internal server error for my ajax call.

Finally the error got resolved when i included this inside the web.config for my webservice

<webServices>
    <protocols>     
        <add name=”HttpPost”/> 
        <add name=”HttpGet”/>  

 </protocols>
</webServices>

By the way, I was using POST in my xmlHttpRequest object Open method.

Plzz check out these links to better understand web services.

Configuration Options for XML Web Services Created Using ASP.NET 

http://msdn2.microsoft.com/en-us/library/b2c0ew36(vs.80).aspx

How to: Disable Protocol Support for Web Services 

http://msdn2.microsoft.com/en-us/library/9hdd3w8c(VS.80).aspx

Top Five ASP.NET Web Services Tips

http://www.ondotnet.com/pub/a/dotnet/2002/10/07/webservices.html

Bye

Hiding or disabling elements in Microsoft Dynamics CRM 3.0

Aim : To disable the close opportunity menu item from the Actions Menu for Opportunity form.

Put the following code in the Form_Load script of the Opportunity form

document.getElementById(‘_MIcomplete’).Style=”display:none” ;


Put the id of the menu item there
To get the id name for any particular menu item

  • step 1- open the opportunity form in crm.

  • step 2- press ctrl+N to open the same form in a new window.

  • step 3- right click go to view source and find the id of the required element.

Same thing we can do for buttons added through isv.config as well as other elements

ISV_New_1_199_Billing.style.display = “none” ;

Bye