Search code examples
service-referencedynamics-crm-365

Unable to add a service reference to Dynamics 365 crm in visual studio 2012


I am trying to add service reference to Dynamics 365 CRM using the following API https://[Organization].api.crm8.dynamics.com/api/data/v8.2/ but each time I am getting this window that asks me for credentials.... enter image description here

I tried using the credentials that I use to login to the crm...but they donot work...can someone tell me which credential I should use?..


Solution

  • Why exactly are you trying to add a reference to the CRM web services? Assuming you want to access CRM from server side code, what you need to do is:

    1. Add references to the core CRM SDK assemblies (Microsoft.Crm.Sdk.Proxy.dll and Microsoft.Xrm.Sdk.dll). You get can them from the downloadable SDK or just add the "Microsoft.CrmSdk.CoreAssemblies" NuGet package.
    2. After doing this you'll be able to write code "talking" with CRM. But what you are missing is the actual "connection". There are several ways of obtaining it, but the easiest one is to use the Xrm Tooling helper class, described here - https://msdn.microsoft.com/en-us/library/mt608573.aspx. You'll need to reference the required assemblies or use the "Microsoft.CrmSdk.XrmTooling.CoreAssembly" NuGet package.

    After doing all this, you'll be able to successfully code against Dynamics CRM.

    CrmServiceClient crmSvc = new CrmServiceClient(ConfigurationManager.ConnectionStrings["MyCRMServer"].ConnectionString);
    
    IOrganizationService orgService = crmSvc.OrganizationServiceProxy;
    
    // Who am I?
    WhoAmIResponse whoAmIResp = orgService.Execute(new WhoAmIRequest()) as WhoAmIResponse;
    Guid myUserId = whoAmIResp.UserId;
    
    // Get all accounts starting with 'A'
    QueryExpression query = new QueryExpression("account");
    query.ColumnSet = new ColumnSet("accountid", "name");
    query.Criteria.AddCondition("name", ConditionOperator.BeginsWith, "a");
    
    EntityCollection ecoll = orgService.RetrieveMultiple(query);
    
    foreach(Entity account in ecoll.Entities)
    {
        if(account.Attributes.Contains("name"))
        {
            Console.WriteLine((string)account["name"]);
        }
    }
    
    // Update some account
    Entity accountToUpdate = new Entity("account");
    accountToUpdate["accountid"] = new Guid("_some_guid_here");
    accountToUpdate["name"] = "new name";
    
    orgService.Update(accountToUpdate);
    

    If you want to use the type safe approach, you'll need to generate a proxy class - like described here: https://msdn.microsoft.com/en-us/library/gg327844.aspx

    Afterwards you'll be able to write code like this:

    DataContext data = new DataContext(orgService);
    // DataContext is the name of the service context, as defined in the CrmScv tool
    
    var myAccountData = (from a in data.AccountSet
                            where a.Address1_Telephone1 == "12312313"
                            select new
                            {
                                a.AccountId,
                                a.Name,
                                a.EMailAddress1,
                                a.PrimaryContactId
                            }).First();
    
    Contact contactToUpdate = new Contact()
    {
        ContactId = myAccountData.PrimaryContactId.Id,
        EMailAddress1 = myAccountData.EMailAddress1
    };
    
    orgService.Update(contactToUpdate);
    

    ... which is much nicer and less error prone.