Joseph Anderson
Joseph Anderson

Reputation: 4144

The value 'Xrm.XrmServiceContext, Xrm' is not recognized as a valid type or is not of the type 'Microsoft.Xrm.Sdk.Client.OrganizationServiceContext'

I am trying to connect to MS Dynamics 2011 Online. On the using xrm part of my code, I receive this error:

The value 'Xrm.XrmServiceContext, Xrm' is not recognized as a valid type or is not of the type 'Microsoft.Xrm.Sdk.Client.OrganizationServiceContext'.

Here is my code:

        var contextName = "Xrm";
        using (var xrm = CrmConfigurationManager.CreateContext(contextName) as XrmServiceContext)
        {


            WriteExampleContacts(xrm);

            //Create a new contact called Allison Brown.
            var allisonBrown = new Xrm.Contact
            {
                FirstName = "Allison",
                LastName = "Brown",
                Address1_Line1 = "23 Market St.",
                Address1_City = "Sammamish",
                Address1_StateOrProvince = "MT",
                Address1_PostalCode = "99999",
                Telephone1 = "12345678",
                EMailAddress1 = "[email protected]"
            };

            xrm.AddObject(allisonBrown);
            xrm.SaveChanges();

            WriteExampleContacts(xrm);
        }

App.config:

  <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
  <section name="microsoft.xrm.client" type="Microsoft.Xrm.Client.Configuration.CrmSection, Microsoft.Xrm.Client"/>
  </configSections>

  <connectionStrings>
    <add name="Xrm"     
connectionString="Url=https://MYORG.crm.dynamics.com/XRMServices"/>
  </connectionStrings>

<microsoft.xrm.client>
<contexts>
  <add name="Xrm" type="Xrm.XrmServiceContext, Xrm"/>
</contexts>
</microsoft.xrm.client>
</configuration>

Xrm.XrmServiceContext is my actual XrmServiceContext name. It is held in Xrm.cs file in my console application.

Upvotes: 1

Views: 5534

Answers (1)

Guido Preite
Guido Preite

Reputation: 15128

Just use the simplified connection.

You need to refer Microsoft.Xrm.Sdk.Client and Microsoft.Xrm.Client.Services

CrmConnection crmConnection = CrmConnection.Parse("Url=https://XXX.crm4.dynamics.com; [email protected]; Password=passwordhere; DeviceID=contoso-ba9f6b7b2e6d; DevicePassword=passcode;");
OrganizationService service = new OrganizationService(crmConnection);

XrmServiceContext context = new XrmServiceContext(service);

var allisonBrown = new Xrm.Contact
            {
                FirstName = "Allison",
                LastName = "Brown",
                Address1_Line1 = "23 Market St.",
                Address1_City = "Sammamish",
                Address1_StateOrProvince = "MT",
                Address1_PostalCode = "99999",
                Telephone1 = "12345678",
                EMailAddress1 = "[email protected]"
            };

context.AddObject(allisonBrown);
context.SaveChanges();

Upvotes: 2

Related Questions