简体   繁体   English

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

[英]CRM 2011 + Custom Workflow to fetch user with Specifed role

Hi i am totally new to crm2011. 嗨,我对crm2011完全陌生。 i need to write a custom workflow. 我需要编写一个自定义工作流程。

To do the following things - 做以下事情-

 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.

From what i have understood is i need to join the below 3 tables - 据我了解,我需要加入以下3个表-

System user
System user roles
Roleset

After searching a lot on how to create a workflow i have landed with this - 在搜索了很多有关如何创建工作流的信息后,我找到了这个-

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

What i need is similar to below fetchxml query but i am not understanding how should i use it in my code since i am not very familiar with the custom workflow syntax 我需要的内容类似于以下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>

Or i found this code also but could not make it useful in my case 或者我也找到了此代码,但无法在我的情况下使它有用

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

Please if anybody could guide me with this. 请有人指导我。 i am totally lost. 我完全迷路了。

Here is how to do the search, if you don't care about Business Unit (ie, you only are using one Business Unit.) You'll need to change the return statement to set the Output parameter CodeActivity.Execute is a void method. 如果您不关心业务单位(即,您仅使用一个业务单位),请按照以下方法进行搜索。您需要更改return语句以设置Output参数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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM