与Microsoft Dynamic CRM 2011集成

时间:2014-04-22 07:05:42

标签: c# dynamics-crm-2011 dynamics-crm

如何使用CRM 2011 SDK和C#将您的应用程序集成到Microsoft CRM 2011?

编辑: 我把问题转到答案,按照问答格式。据Guido Preite说。

1 个答案:

答案 0 :(得分:1)

因为我现在习惯于分享我每天都学到的新东西,所以我将在这里展示如何使用CRM 2011 SDK和C#连接到Microsoft CRM 2011。这将帮助你不要像我刚才那样把头撞在墙上。

首先将对项目的引用添加到Microsoft.Xrm.Sdk.dll。您可以从CRM 2011 sdk获取(在此处下载:http://www.microsoft.com/en-us/download/details.aspx?id=24004)。

这是关于如何连接到CRM服务的代码:

    using Microsoft.Xrm.Sdk.Client;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Xrm.Sdk;

    //This is your Organization Service which you can find from the actual CRM UI. go to Settings>Customizations>Developer Resources.
    Uri organizationUri = new Uri("http://crmservername/organizationname/XRMServices/2011/Organization.svc"); 
    Uri homeRealmUri = null;
    ClientCredentials credentials = new ClientCredentials();
    //Instantiate your network credential that will access the CRM Server
    credentials.Windows.ClientCredential = new System.Net.NetworkCredential("username", "password", "domain");

    OrganizationServiceProxy orgProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null);
    //Instantiate IOrganizationService so you can call the CRM service methods.
     IOrganizationService _service = (IOrganizationService)orgProxy

//from this you can now perform CRUD to your CRM. 
//I'm just going to provide some example on how to query your entities in CRM like so:

    QueryExpression query = new QueryExpression() { };

    query.EntityName = "country";
    query.ColumnSet = new ColumnSet("name", "2digitiso", "3digitiso");

    EntityCollection retrieved = _service.RetrieveMultiple(query);

    foreach (var item in retrieved.Entities)
    {
         MessageBox.Show(item["name"].ToString() + " " + item["2digitiso"].ToString() + " " + item["3digitiso"].ToString());
    }

参考: http://nishantrana.wordpress.com/2010/11/03/sample-code-for-using-iorganizationservice-in-crm-2011/
http://msdn.microsoft.com/en-us/library/gg334708.aspx
http://msdn.microsoft.com/en-us/library/gg328149.aspx
http://www.codeproject.com/Articles/559599/Integrating-your-applications-with-MS-CRM-Online

另外如果碰巧遇到这样的例外:

CRM Service Exception: Could not load file or assembly 'Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies

安装:Windows Identity Foundation(http://www.microsoft.com/en-us/download/details.aspx?id=17331

我希望我帮助你们中的一些人。

相关问题