Share via


Populate a DataGrid with the Results of a Query

This sample shows how to populate an ASP.NET DataGrid Web Control with results from a call to CRMQuery.ExecuteQuery. You can use the XML string returned to create a dataset that is used to create a datasource to populate a datagrid.

Within an ASP.NET Web Application that has a Button (button1), and a Datagrid Web Control on the Web Form, add the following C# code to the _button1 click event().

You must add the following references to your project:

Microosft.CRM.Proxy.dll

System.Web.Services.dll

Class Reference

Schema Reference

Example

[C#]

private void button1_Click(object sender, System.EventArgs e)

{

     string strServer = <crmservername>;
     // virtualDirectory should be set with the name of the Microsoft CRM
     // virtual directory on the platform Web server
     string virtualDirectory = "mscrmservices";

    string strDir = "https://" + strServer + "/" + virtualDirectory + "/";

     // BizUser proxy object
     Microsoft.CRM.Proxy.BizUser bizUser = new Microsoft.CRM.Proxy.BizUser ();
     bizUser.Credentials = System.Net.CredentialCache.DefaultCredentials;
     bizUser.Url = strDir + "BizUser.srf";

     // CRMQuery proxy object
     Microsoft.CRM.Proxy.CRMQuery myQuery = new Microsoft.CRM.Proxy.CRMQuery();
     myQuery.Credentials = System.Net.CredentialCache.DefaultCredentials;
     myQuery.Url = strDir + "CRMQuery.srf";
     Microsoft.CRM.Proxy.CUserAuth userAuth = bizUser.WhoAmI();

     string lstrQueryActivityXml= "<fetch mapping='logical'>"
             + "<entity name='activity'>"
             + "<attribute name = 'activityid'/>"
             + "<attribute name = 'activitytypecode'/>"
             + "<attribute name = 'objectid'/>"
             + "<attribute name = 'objectname'/>"
             + "<attribute name = 'objecttypecode'/>"
             + "<attribute name = 'subject'/>"
             + "<attribute name = 'category'/>"
             + "<attribute name = 'misc1'/>"
             + "<attribute name = 'scheduledstart'/>"
             + "<attribute name = 'ownerid'/>"
             + "<attribute name = 'description'/>"
             + "<filter type='and'>"
             + "<condition attribute = 'subject' operator='like' value='%" + "i" + "%'/>"
             + "</filter>"
             + "</entity>"
             + "</fetch>"; 
       string myquery=myQuery.ExecuteQuery(userAuth,lstrQueryActivityXml);

       System.IO.StringReader lxmlStringReader = new  System.IO.StringReader(myquery);
       DataSet pdsActivities=new DataSet();
       pdsActivities.ReadXml(lxmlStringReader, XmlReadMode.Auto);
       XmlDocument lXmlDocActivities = new XmlDocument();
       lXmlDocActivities.LoadXml(myquery);

       DataGrid1.DataSource = pdsActivities; 

       DataGrid1.DataBind();

}

© 2005 Microsoft Corporation. All rights reserved.