首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过CRM 365插件在线连接CRM

通过CRM 365插件在线连接CRM
EN

Stack Overflow用户
提问于 2017-02-20 01:55:59
回答 4查看 1.1K关注 0票数 1

我需要连接和检索记录在CRM在线通过CRM 365插件。我尝试过使用xrm.tooling.dll简化连接,但不幸的是,它表示Could not load file or assembly 'microsoft.xrm.tooling.connector,而当我使用ClientCredential时,错误表示Metadata contain refereces that cannot be resolved

奇怪的是,我尝试了这两种方法和控制台鼓掌,这是非常出色的工作。只想知道我在这件案子里错过了什么?当我想通过插件连接到CRM时,我需要特殊的要求吗?请大家分享你的知识。

编辑

这只是一个示例代码,可以从CRM获取帐户名并使用InvalidPluginExecutionException显示它:

代码语言:javascript
复制
IOrganizationService _service;

public void Execute(IServiceProvider serviceprovider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceprovider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory servicefactory = (IOrganizationServiceFactory)serviceprovider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = servicefactory.CreateOrganizationService(context.UserId);

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
           {

                Entity ent = (Entity)context.InputParameters["Target"];

                if (ent.LogicalName != "opportunity")
                    return;

                string connstring = @"Url=https://office.crm5.dynamics.com; Username=username@office.onmicrosoft.com; Password=crmoffice; authtype=Office365";
                CrmServiceClient conn = new Microsoft.Xrm.Tooling.Connector.CrmServiceClient(connstring);
                service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : 

(IOrganizationService)conn.OrganizationServiceProxy;


                try
                {
                    Guid fabercastel = new Guid("efd566dc-10ff-e511-80df-c4346bdcddc1");
                    Entity _account = new Entity("account");
                    _account = service.Retrieve(_account.LogicalName, fabercastel, new ColumnSet("name"));

                    string x = _account["name"].ToString();


                    throw new InvalidPluginExecutionException("Result of Query : " + x);
                }
                catch (Exception ex)
                {
                    throw new InvalidPluginExecutionException(ex.Message);
                }
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-03-18 11:18:01

您应该能够连接到另一个客户关系管理实例,而无需使用在线沙箱之外的任何程序集( Microsoft.Xrm.Sdk和相关程序集除外)。只需使用来自"SDK\SampleCode\CS\GeneralProgramming\Authentication\AuthenticateWithNoHelp\AuthenticateWithNoHelp.cs".的SDK示例连接到Office365的简化版本如下所示:

代码语言:javascript
复制
class AuthenticateWithNoHelp
{
    private String _discoveryServiceAddress = "https://disco.crm.dynamics.com/XRMServices/2011/Discovery.svc";
    private String _organizationUniqueName = "orgname";
    private String _userName = "admin@orgname.onmicrosoft.com";
    private String _password = "password";
    private String _domain = "domain";

    public void Run()
    {
        IServiceManagement<IDiscoveryService> serviceManagement =
                    ServiceConfigurationFactory.CreateManagement<IDiscoveryService>(
                    new Uri(_discoveryServiceAddress));
        AuthenticationProviderType endpointType = serviceManagement.AuthenticationType;

        AuthenticationCredentials authCredentials = GetCredentials(serviceManagement, endpointType);


        String organizationUri = String.Empty;
        using (DiscoveryServiceProxy discoveryProxy =
            GetProxy<IDiscoveryService, DiscoveryServiceProxy>(serviceManagement, authCredentials))
        {
            if (discoveryProxy != null)
            {
                OrganizationDetailCollection orgs = DiscoverOrganizations(discoveryProxy);
                organizationUri = FindOrganization(_organizationUniqueName,
                    orgs.ToArray()).Endpoints[EndpointType.OrganizationService];

            }
        }

        if (!String.IsNullOrWhiteSpace(organizationUri))
        {
            IServiceManagement<IOrganizationService> orgServiceManagement =
                ServiceConfigurationFactory.CreateManagement<IOrganizationService>(
                new Uri(organizationUri));

            AuthenticationCredentials credentials = GetCredentials(orgServiceManagement, endpointType);

            using (OrganizationServiceProxy organizationProxy =
                GetProxy<IOrganizationService, OrganizationServiceProxy>(orgServiceManagement, credentials))
            {
                organizationProxy.EnableProxyTypes();
                Guid userid = ((WhoAmIResponse)organizationProxy.Execute(
                    new WhoAmIRequest())).UserId;
            }
        }
    }

    private AuthenticationCredentials GetCredentials<TService>(IServiceManagement<TService> service, AuthenticationProviderType endpointType)
    {
        AuthenticationCredentials authCredentials = new AuthenticationCredentials();

        authCredentials.ClientCredentials.UserName.UserName = _userName;
        authCredentials.ClientCredentials.UserName.Password = _password;

        return authCredentials;
    }

    public OrganizationDetailCollection DiscoverOrganizations(
        IDiscoveryService service)
    {
        if (service == null) throw new ArgumentNullException("service");
        RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
        RetrieveOrganizationsResponse orgResponse =
            (RetrieveOrganizationsResponse)service.Execute(orgRequest);

        return orgResponse.Details;
    }

    public OrganizationDetail FindOrganization(string orgUniqueName,
        OrganizationDetail[] orgDetails)
    {
        if (String.IsNullOrWhiteSpace(orgUniqueName))
            throw new ArgumentNullException("orgUniqueName");
        if (orgDetails == null)
            throw new ArgumentNullException("orgDetails");
        OrganizationDetail orgDetail = null;

        foreach (OrganizationDetail detail in orgDetails)
        {
            if (String.Compare(detail.UrlName, orgUniqueName,
                StringComparison.InvariantCultureIgnoreCase) == 0)
            {
                orgDetail = detail;
                break;
            }
        }
        return orgDetail;
    }

    private TProxy GetProxy<TService, TProxy>(
        IServiceManagement<TService> serviceManagement,
        AuthenticationCredentials authCredentials)
        where TService : class
        where TProxy : ServiceProxy<TService>
    {
        Type classType = typeof(TProxy);

        if (serviceManagement.AuthenticationType !=
            AuthenticationProviderType.ActiveDirectory)
        {
            AuthenticationCredentials tokenCredentials =
                serviceManagement.Authenticate(authCredentials);
            return (TProxy)classType
                .GetConstructor(new Type[] { typeof(IServiceManagement<TService>), typeof(SecurityTokenResponse) })
                .Invoke(new object[] { serviceManagement, tokenCredentials.SecurityTokenResponse });
        }

        return (TProxy)classType
            .GetConstructor(new Type[] { typeof(IServiceManagement<TService>), typeof(ClientCredentials) })
            .Invoke(new object[] { serviceManagement, authCredentials.ClientCredentials });
    }

    static public void Main(string[] args)
    {
        AuthenticateWithNoHelp app = new AuthenticateWithNoHelp();
        app.Run();
    }
}

您可以通过使用DiscoveryService删除部件并直接调用:

代码语言:javascript
复制
https://orgname.api.crm.dynamics.com/XRMServices/2011/Organization.svc

这应该适用于沙箱插件,因为它只使用Sdk程序集。

票数 2
EN

Stack Overflow用户

发布于 2017-02-20 11:25:04

您已经使用在插件的第三行上定义的IOrganizationService与客户关系管理系统建立了连接。除非您需要连接到其他组织中的另一个CRM实例,否则不需要或不需要登录。

基本上,只要删除以上的4行你的尝试,你应该是好的。

编辑:

代码语言:javascript
复制
public void Execute(IServiceProvider serviceprovider)
{
    IPluginExecutionContext context = (IPluginExecutionContext)serviceprovider.GetService(typeof(IPluginExecutionContext));
    IOrganizationServiceFactory servicefactory = (IOrganizationServiceFactory)serviceprovider.GetService(typeof(IOrganizationServiceFactory));
    IOrganizationService service = servicefactory.CreateOrganizationService(context.UserId);

    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
    {

         Entity ent = (Entity)context.InputParameters["Target"];

         if (ent.LogicalName != "opportunity")
             return;

         Guid fabercastel = new Guid("efd566dc-10ff-e511-80df-c4346bdcddc1");
         Entity _account = new Entity("account");
         _account = service.Retrieve(_account.LogicalName, fabercastel, new ColumnSet("name"));

         string x = _account["name"].ToString();


         throw new InvalidPluginExecutionException("Result of Query : " + x);
    }
}
票数 2
EN

Stack Overflow用户

发布于 2017-02-21 07:05:40

您不需要任何额外的库(如Microsoft.Xrm.Tooling.Connector或SDK中的其他库)来使用CRM服务。与SOAP / REST协议相关的标准.NET机制就足够了(当然,这种方法可能稍微困难一些)。

编辑:我已经做了一些额外的调查,在不使用SDK库的情况下,为Office365身份验证配置自动生成的Office365可能是非常痛苦的。我并不是说这是不可能的,但是微软并没有记录它。要添加更多详细信息,Visual生成的代理类不支持OAuth身份验证。

正因为如此,我的第二个建议是使用与CRM OnLine通信的facade web服务。您可以在Windows或互联网上的任何其他云/托管位置上承载此web服务。从您的CRM 365插件,您可以使用您的自定义web服务方法,并与您的CRM在线实例使用此服务。我想这将是一个更好的方法,尝试运行无文档的方法连接到CRM在线。**

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42335295

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档