值“Xrm.XrmServiceContext,Xrm”未被识别为有效类型或不属于“Microsoft.Xrm.Sdk.Client.OrganizationServiceContext”类型

时间:2013-05-10 20:05:39

标签: dynamics-crm-2011 microsoft-dynamics

我正在尝试连接到MS Dynamics 2011 Online。在我的代码的使用xrm部分,我收到此错误:

值'Xrm.XrmServiceContext,Xrm'未被识别为有效类型或不属于'Microsoft.Xrm.Sdk.Client.OrganizationServiceContext'类型。

这是我的代码:

        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 = "allison.brown@example.com"
            };

            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是我的实际XrmServiceContext名称。它保存在我的控制台应用程序的Xrm.cs文件中。

1 个答案:

答案 0 :(得分:2)

只需使用简化连接即可。

您需要推荐Microsoft.Xrm.Sdk.ClientMicrosoft.Xrm.Client.Services

CrmConnection crmConnection = CrmConnection.Parse("Url=https://XXX.crm4.dynamics.com; Username=user@domain.onmicrosoft.com; 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 = "allison.brown@example.com"
            };

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