Dynamics crm 2011 通过CRM 365插件连接到CRM Online

Dynamics crm 2011 通过CRM 365插件连接到CRM Online,dynamics-crm-2011,dynamics-crm,crm,dynamics-crm-online,dynamics-crm-365,Dynamics Crm 2011,Dynamics Crm,Crm,Dynamics Crm Online,Dynamics Crm 365,我需要通过CRM 365插件连接和检索CRM Online中的记录。我曾尝试使用xrm.tooling.dll简化连接,但不幸的是,它说无法加载文件或程序集microsoft.xrm.tooling.connector,当我使用ClientCredential时,错误显示元数据包含无法解析的引用 奇怪的是,我在控制台应用程序中尝试了这两种方法,效果都很好。只是想知道我错过了什么?当我想通过插件连接到CRM时,是否需要特殊要求?请任何人分享你的知识 编辑 这只是从CRM Online获取帐户名并使

我需要通过CRM 365插件连接和检索CRM Online中的记录。我曾尝试使用
xrm.tooling.dll
简化连接,但不幸的是,它说
无法加载文件或程序集microsoft.xrm.tooling.connector
,当我使用
ClientCredential
时,错误显示
元数据包含无法解析的引用

奇怪的是,我在控制台应用程序中尝试了这两种方法,效果都很好。只是想知道我错过了什么?当我想通过插件连接到CRM时,是否需要特殊要求?请任何人分享你的知识

编辑

这只是从CRM Online获取帐户名并使用InvalidPlugineExecutionException显示帐户名的示例代码:

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);
                }

您已经使用您在插件的第三行中定义的IOrganizationService连接到CRM。除非您需要连接到不同组织中的另一个CRM实例,否则不需要或不需要登录

基本上只要删除上面的4行就可以了,你应该很好

编辑:

public void Execute(IServiceProvider服务提供者)
{
IPluginExecutionContext上下文=(IPluginExecutionContext)serviceprovider.GetService(类型为(IPluginExecutionContext));
IOOrganizationServiceFactory servicefactory=(IOOrganizationServiceFactory)serviceprovider.GetService(类型为(IOOrganizationServiceFactory));
IOOrganizationService=servicefactory.CreateOrganizationService(context.UserId);
if(context.InputParameters.Contains(“目标”)&&context.InputParameters[“目标”]是实体)
{
实体ent=(实体)上下文.InputParameters[“目标”];
if(ent.LogicalName!=“商机”)
返回;
Guid fabercastel=新Guid(“efd566dc-10ff-e511-80df-C4346BDCDCC1”);
实体账户=新实体(“账户”);
_account=service.Retrieve(_account.LogicalName,fabercastel,new ColumnSet(“name”);
字符串x=_帐户[“名称”]。ToString();
抛出新的InvalidPlugineExecutionException(“查询结果:+x”);
}
}

您已经使用您在插件第三行定义的IOrganizationService连接到CRM。除非您需要连接到不同组织中的另一个CRM实例,否则不需要或不需要登录

基本上只要删除上面的4行就可以了,你应该很好

编辑:

public void Execute(IServiceProvider服务提供者)
{
IPluginExecutionContext上下文=(IPluginExecutionContext)serviceprovider.GetService(类型为(IPluginExecutionContext));
IOOrganizationServiceFactory servicefactory=(IOOrganizationServiceFactory)serviceprovider.GetService(类型为(IOOrganizationServiceFactory));
IOOrganizationService=servicefactory.CreateOrganizationService(context.UserId);
if(context.InputParameters.Contains(“目标”)&&context.InputParameters[“目标”]是实体)
{
实体ent=(实体)上下文.InputParameters[“目标”];
if(ent.LogicalName!=“商机”)
返回;
Guid fabercastel=新Guid(“efd566dc-10ff-e511-80df-C4346BDCDCC1”);
实体账户=新实体(“账户”);
_account=service.Retrieve(_account.LogicalName,fabercastel,new ColumnSet(“name”);
字符串x=_帐户[“名称”]。ToString();
抛出新的InvalidPlugineExecutionException(“查询结果:+x”);
}
}
您不需要任何附加库,如Microsoft.Xrm.Tooling.Connector或SDK中的其他库来使用CRM web服务。与SOAP/REST协议相关的标准.NET机制就足够了(当然,这种方法可能稍微困难一些)

编辑:我做了一些额外的调查,发现在不使用SDK库的情况下为Office365身份验证配置自动生成的OrganizationServiceClient可能是一件非常麻烦的事情。我并不是说这是不可能的,但是Microsoft没有对此进行记录。要添加更多详细信息,Visual Studio生成的代理类不支持OAuth身份验证

因此,我的第二个建议是使用FacadeWeb服务与CRM在线通信。您可以在Windows Azure或internet上的任何其他云/托管位置上托管此web服务。通过CRM 365插件,您可以使用自定义web服务方法,并使用此服务与CRM Online实例通信。我认为,尝试运行未记录的连接到CRM Online的方法会更好。**

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

编辑:我做了一些额外的调查,发现在不使用SDK库的情况下为Office365身份验证配置自动生成的OrganizationServiceClient可能是一件非常麻烦的事情。我并不是说这是不可能的,但是Microsoft没有对此进行记录。要添加更多详细信息,Visual Studio生成的代理类不支持OAuth身份验证


因此,我的第二个建议是使用FacadeWeb服务与CRM在线通信。您可以在Windows Azure或internet上的任何其他云/托管位置上托管此web服务。通过CRM 365插件,您可以使用自定义web服务方法,并使用此服务与CRM Online实例通信。我认为,尝试运行未记录的连接到CRM Online的方法会更好。**

您应该能够连接到另一个CRM实例,而无需使用在线沙盒之外的任何程序集(因此不是Microsoft.Xrm.Sdk和相关程序集)。只需使用示例f
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();
    }
}
https://orgname.api.crm.dynamics.com/XRMServices/2011/Organization.svc