2011-06-22 48 views

回答

0

這是使用連接到網絡提供商

   var serverConfig = GetServerConfig(sessionKey); 
      // Connect to the Organization service. 
      // The using statement ensures that the service proxy will be properly disposed. 

      using (var serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri, serverConfig.Credentials, serverConfig.DeviceCredentials)) 
      { 
       // This statement is required to enable early-bound type support. 
       serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior()); 

       using (var orgContext = new CrmServiceContext(serviceProxy)) 
       { 
        return orgContext.AccountSet.Where(item => item.Id == id).Select().Single(); 
       } 
      } 

這裏還有SDK中的一個很好的例子ODATA提供的粗略例如:

CRM2011Sdk \ SDK \ samplecode \ CS \ wsdlbasedproxies \在線

3

我使用「早期綁定」方法,使用CrmSvcUtil.exe工具生成C#實體類,但請確保使用在各種示例中可找到的/codecustomization開關。您需要最新版本的CRM 2011 SDK,並且必須從\bin文件夾中運行CrmSvcUtil.exe(不要使用與CRM安裝的版本)。

你的項目需要引用Microsoft.Xrm.ClientMicrosoft.Xrm.SdkMicrosoft.Crm.Sdk.Proxy加上.NET Framework中的幾個人(看生成錯誤,看看你錯過了什麼,然後將它們添加到它生成)。

這是一個基本的代碼片段檢索聯繫人實體,更新自己的領域之一,然後將其保存回CRM:

CrmDataContext dc = new CrmDataContext("Xrm"); 

Contact contact = (from c in dc.ContactSet 
        where ...whatever... 
        select c).FirstOrDefault(); 

contact.FirstName = "Jo"; 

dc.SaveChanges(); 

(注意CrmDataContext是我的數據上下文的名稱可以使用CrmSvcUtil命令行開關之一設置該名稱)。

你還需要一些東西添加到您的web.config:

<configSections> 
    <section name="microsoft.xrm.client" type="Microsoft.Xrm.Client.Configuration.CrmSection, Microsoft.Xrm.Client" /> 
</configSections> 

<connectionStrings> 
    <add name="Xrm" connectionString="Server=http://<your crm url>; Domain=<your domain>; Username=<a crm user id>; Password=<their password>" /> 
</connectionStrings> 

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

這是假設你是公司網絡上運行CRM,所以帳戶和域的連接字符串中指定將成爲一個AD賬戶,該賬戶被設置爲具有相關權限的CRM用戶來檢索和更新實體。