Wednesday 2 December 2015

Show MS CRM Account details in grid view of Asp.net

Hi,

Here is a code to show account details from Dynamics CRM online 2015 to gridview of ASP.Net through late binding.

Add new web form and add one gridview to design form.

 Now add code to code view.

protected void Page_Load(object sender, EventArgs e)
        {
            IOrganizationService service = GetCRMService();
            QueryExpression query = new QueryExpression("account");
            query.ColumnSet.AllColumns = true;
            EntityCollection ecollection = service.RetrieveMultiple(query);

            DataTable dt = new DataTable();
            dt.Columns.Add("name");
            dt.Columns.Add("accountid");

            foreach (Entity entity in ecollection.Entities)
            {
                DataRow dr = dt.NewRow();
                dr["Name"] = entity.Attributes["name"].ToString();
                dr["accountid"] = entity.Attributes["accountid"].ToString();
                dt.Rows.Add(dr);
            }
       
            gridViewAccountName.DataSource = dt;
            gridViewAccountName.DataBind();
        }


        public IOrganizationService GetCRMService()
        {
            string orgUrl = "https://XXXX.api.crm5.dynamics.com/XRMServices/2011/Organization.svc";
            Uri OrganizationUri = new Uri(orgUrl);
            IOrganizationService service;
            ClientCredentials credentials = new ClientCredentials();

            credentials.UserName.UserName = "XXX@XXXXXXXX.onmicrosoft.com";
            credentials.UserName.Password = "XXXXXXXXX";
            try
            {
                using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, null, credentials, null))
                {
                    serviceProxy.EnableProxyTypes();
                    service = (IOrganizationService)serviceProxy;
                                   }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return service;
        }

The result is like below:

nameaccountid
vdvzsdfae87e930-d691-e511-80fa-3863bb345bf8
Fourth Coffee (sample)1af02229-5582-e511-80fe-3863bb347758
Litware, Inc. (sample)1cf02229-5582-e511-80fe-3863bb347758
Adventure Works (sample)1ef02229-5582-e511-80fe-3863bb347758
Fabrikam, Inc. (sample)20f02229-5582-e511-80fe-3863bb347758
Blue Yonder Airlines (sample)22f02229-5582-e511-80fe-3863bb347758
City Power & Light (sample)24f02229-5582-e511-80fe-3863bb347758
Contoso Pharmaceuticals (sample)26f02229-5582-e511-80fe-3863bb347758
Alpine Ski House (sample)28f02229-5582-e511-80fe-3863bb347758
A. Datum Corporation (sample)2af02229-5582-e511-80fe-3863bb347758
Coho Winery (sample)2cf02229-5582-e511-80fe-3863bb347758
Adventure test cycle7668e02f-238d-e511-80f4-3863bb36ef98


No comments:

Post a Comment