Thursday, November 1, 2012

Connecting to CRM Online from an outside caller


Open Visual Studio
Add the following references from the SDK\bin folder.
Microsoft.Xrm.Client.dll
Microsoft.Xrm.Sdk.dll

Add the following references from .NET.
System.Data.Services.dll
System.Data.Services.Client.dll
System.Runtime.Serialization.dll

DEVICE REGISTRATION
You can skip this next step and jump to CREATE EARLY BOUND TYPES if you follow the enhancement in the Eliminating manual device registration post.

There’s a section titled “To Generate your individual device ID and password” in the Create Early Bound Entity Classes with the Code Generation Tool (CrmSvcUtil.exe), but it’s easy to miss.  Follow the instructions from that section:

Open and build the DeviceRegistration project: SDK\Tools\DeviceRegistration\DeviceRegistration.csproj.
Run the executable file from the command line. To register your device, set the /operation parameter to Register.
C:\deviceregistration.exe /operation:Register
Copy the displayed device ID and password values and use them as the deviceid and devicepassword parameter values when you run the CrmSvcUtil tool.

Generate the Class by using below command
crmsvcutil.exe /url:https://demo1.api.crm5.dynamics.com/XRMServices/2011/Organization.svc /o:demo1.cs /n:demo1namespace /u:"emailid" /p:"password" /di: DeviceID /dp: DevicePassword /serviceContextName:demo1context

Add the webform
Add the GridView
goto--> Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using demo1namespace;
using System.Linq;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Client;
using Microsoft.Crm.Sdk;
using System.ServiceModel;
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Metadata;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ClientCredentials cc = new ClientCredentials();
        cc.UserName.UserName = "";
        cc.UserName.Password = "";
        ClientCredentials dc = new ClientCredentials();
        dc.UserName.UserName = "DeviceID";
        dc.UserName.Password = "DevicePassword";
        Uri url = new Uri("https://demo.crm5.dynamics.com/XRMServices/2011/Organization.svc");
        using (OrganizationServiceProxy proxy = new OrganizationServiceProxy(url, null, cc, dc))
        {
            proxy.EnableProxyTypes();
            IOrganizationService orgserivce = (IOrganizationService)proxy;
           demo1context context = new demo1context(proxy);
            var query = from con in context.ContactSet
                        where con.LastName.Contains("sample")
                        select new
                        {
                            con.MiddleName,
                            con.LastName,
                            con.FirstName,
                            con.CreatedOn,
                            con.CreatedBy,
                        };
            GridView1.DataSource = query;
            GridView1.DataBind();
        }
    }
}


No comments:

Post a Comment