如何在我的插件 - CRM 2011上使用OrganizationServiceProxy?

时间:2013-01-23 20:34:48

标签: plugins dynamics-crm-2011 fetchxml

我需要在CRM插件中使用fetch xml,我在这里找到了一个如何做到这一点的例子:

string groupby1 = @" 
    <fetch distinct='false' mapping='logical' aggregate='true'> 
     <entity name='opportunity'>
      <attribute name='name' alias='opportunity_count' aggregate='countcolumn' />  
      <attribute name='ownerid' alias='ownerid' groupby='true' />
      <attribute name='createdon' alias='createdon' /> 
      <attribute name='customerid' alias='customerid' /> 
     </entity> 
    </fetch>";

    EntityCollection groupby1_result = orgProxy.RetrieveMultiple(new FetchExpression(groupby1));

但还有其他我不知道如何使用的东西,或者它在哪里使用..它的部分内容如下:

orgProxy.RetrieveMultiple(new FetchExpression(groupby1));

我知道它是 OrganizationServiceProxy 的一个对象,但它在插件类中的位置是什么?我找不到。

1 个答案:

答案 0 :(得分:6)

尽可能以最礼貌的方式,您可能需要向后退几步才能前进。

所以要编写一个插件,你需要实现IPlugin,它只有一个方法

public void Execute(IServiceProvider serviceProvider)

IServiceProvider是您进入CRM的窗口以及您正在加入的事件的上下文。

通常,您会执行以下操作:

var context = (IPluginExecutionContext) serviceProvider.GetService(typeof (IPluginExecutionContext));
var factory = (IOrganizationServiceFactory) serviceProvider.GetService(typeof (IOrganizationServiceFactory));
var service = factory.CreateOrganizationService(context.UserId);

在上面的示例中,service的类型为IOrganizationService。这为您提供了您期望的所有方法

service.Execute(foo);
service.RetrieveMultiple(bar);
service.Update(... /* etc

可能值得回顾一下这方面的一些指南 - 正如我之前的回答here

相关问题