Monday 22 May 2017

DYNAMICS 365 INTEGRATION WITH WEB APPLICATION (ASP.NET) AND RETRIEVING DATA AND SHOWING IN GRID

Follow the steps as below:

First Create an ASP.NET WEBSITE from Visual Studio
Goto File> New > Website > Add a webform > 
then it will create an aspx page file then add a "GridView" to the aspx page. Inside the ASPX.CS class add the below code 

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Net;
using System.ServiceModel.Description;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;


public partial class Accounts : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        IOrganizationService service = GetCRMService();
        QueryExpression query = new QueryExpression("account");
        query.ColumnSet.AllColumns = true;
        query.Criteria.AddCondition("name", ConditionOperator.NotEqual, "ksllls");
        EntityCollection collection = service.RetrieveMultiple(query);

        DataTable dt = new DataTable();
        dt.Columns.Add("name");
        dt.Columns.Add("accountnumber");
        foreach(Entity entity in collection.Entities)
        {
            DataRow dr = dt.NewRow();
            dr["name"] = entity.Attributes["name"].ToString();
            if(entity.Contains("accountnumber"))
            { 
            dr["accountnumber"] = entity.Attributes["accountnumber"].ToString();
            }
            dt.Rows.Add(dr);
        }
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }


    public IOrganizationService GetCRMService()
    {
        string UserName = "mam@santechnology.onmicrosoft.com";
        string Password = "devisri123";
        ClientCredentials Credentials = new ClientCredentials();
        IOrganizationService Service;
        Credentials.UserName.UserName = UserName;
        Credentials.UserName.Password = Password;

        Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
        //This URI need to be updated to match the servername and organisation for the environment

        string CRMServer = ConfigurationManager.AppSettings["crmserverurl"].ToString();
        Uri OrganizationUri = new Uri(CRMServer);
        Uri HomeRealmUri = null;

        // OrgaizationServiceProxy  ServiceProxy

        using(OrganizationServiceProxy ServiceProxy = new OrganizationServiceProxy(OrganizationUri,HomeRealmUri,Credentials,null))
        {
             Service = (IOrganizationService)ServiceProxy;
        }
        return Service;
    }
}

=======================================================================
 Screen shot of the Visual studio website application..


Account.aspx code:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>

<body>
    <form id="form1" runat="server">
    <div>
    <asp:gridview ID="GridView1" runat="server"></asp:gridview>
    </div>
    </form>
</body>
</html>

=======================================================================
WEB.CONFIG CODE



<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
<appSettings>
  <add key ="crmserverurl" value="https://santechnology.api.crm11.dynamics.com/XRMServices/2011/Organization.svc"/>
</appSettings>
    <system.web>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>


</configuration>

=======================================================================

The Output will be as shown on the screen shot:



=======================================================================

Here we go, then we will be able to see the accounts based on the queryexpression condition:

I hope this code might helps someone out there..

Happy CRM:-)
🔄


🔵🌹💮












No comments:

Post a Comment

Note: only a member of this blog may post a comment.