CRM 2011 +自定义工作流程以获取具有特定角色的用户

时间:2013-11-23 19:52:27

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

嗨,我对crm2011全新。 我需要编写自定义工作流程。

要做以下事情 -

 1)I want to pass Role Name as "Manager" or something
 2)and on the basis of Role Name a user should be returned who is assigned that role.

据我所知,我需要加入以下3个表格 -

System user
System user roles
Roleset

在搜索了很多关于如何创建工作流程之后,我已经找到了这个 -

class GetUserRole:CodeActivity
{
     [Output("Current User")]
     [ReferenceTarget("systemuser")]
     public OutArgument<EntityReference> CurrentUser { get; set; }
     string securityrole ="Manager";

     protected override void Execute(CodeActivityContext Execution)
     {

       IWorkflowContext context = Execution.GetExtension<IWorkflowContext>();
       IOrganizationServiceFactory serviceFactory =
                        Execution.GetExtension<IOrganizationServiceFactory>();

       IOrganizationServiceservice=
             serviceFactory.CreateOrganizationService(context.InitiatingUserId);

       var entity = organizationService.Retrieve("systemuser",context.PrimaryEntityId,new ColumnSet(new String [] {"systemuserid", "firstname", "lastname"}));

      //Here i need to add the logic to fetch the user from the system with the mentioned role

     //set Current user to the returned value (User)
     }

我需要的是类似于下面的fetchxml查询,但我不明白我应该如何在我的代码中使用它,因为我不熟悉自定义工作流语法

 <fetch mapping="logical" count="50" version="1.0">
 <entity name="systemuser">
 <attribute name="fullname" />
 <link-entity name="systemuserroles" from="systemuserid" to="systemuserid">
  <link-entity name="role" from="roleid" to="roleid">
    <filter>
      <condition attribute="name" operator="eq" value="salesperson" />
    </filter>
  </link-entity>
</link-entity>

或者我也发现了这段代码,但在我的情况下无法使它有用

QueryExpression query = new QueryExpression("systemuser");

query.ColumnSet = new ColumnSet(new string[] { "systemuserid" });
query.Distinct = true;
query.Criteria = new FilterExpression();
query.Criteria.AddCondition("businessunitid", ConditionOperator.Equal,  ((EntityReference)entity.Attributes["new_unit"]).Id);
query.AddLink("systemuserroles", "systemuserid", "systemuserid").
AddLink("role","roleid", "roleid").
    LinkCriteria.AddCondition("name", ConditionOperator.Equal, "MyRoleName");

var users = organizationService.RetrieveMultiple(query);

如果有人可以指导我的话。我完全迷失了。

1 个答案:

答案 0 :(得分:2)

以下是如何进行搜索,如果您不关心业务单位(即,您只使用一个业务单位。)您需要更改return语句以设置输出参数CodeActivity.Execute是一种void方法。

string roleName = "System Administrator";

QueryExpression query = new QueryExpression("systemuser");

query.ColumnSet = new ColumnSet(new string[] { "systemuserid" });
query.Distinct = true;
query.Criteria = new FilterExpression();
query.AddLink("systemuserroles", "systemuserid", "systemuserid").
AddLink("role","roleid", "roleid").LinkCriteria.AddCondition("name", ConditionOperator.Equal, roleName);

var users = organizationService.RetrieveMultiple(query);

if (users.Entities.Count() > 0) return ((Entity)users[0]).ToEntityReference();