Home > CRM 2011, Plug-Ins > Check user security role in Plug-ins CRM 2011

Check user security role in Plug-ins CRM 2011


Below is the code snippet to check current user security role in Plug-in.

private void CheckUserRole(IOrganizationService service, Guid userID)


QueryExpression query = new QueryExpression();

query.EntityName = “role”; //role entity name

ColumnSet cols = new ColumnSet();

cols.AddColumn(“name”); //We only need role name

query.ColumnSet = cols;

ConditionExpression ce = new ConditionExpression();

ce.AttributeName = “systemuserid”;

ce.Operator = ConditionOperator.Equal;


//system roles

LinkEntity linkRole = new LinkEntity();

linkRole.LinkFromAttributeName = “roleid”;

linkRole.LinkFromEntityName = “role”; //FROM

linkRole.LinkToEntityName = “systemuserroles”;

linkRole.LinkToAttributeName = “roleid”;

//system users

LinkEntity linkSystemusers = new LinkEntity();

linkSystemusers.LinkFromEntityName = “systemuserroles”;

linkSystemusers.LinkFromAttributeName = “systemuserid”;

linkSystemusers.LinkToEntityName = “systemuser”;

linkSystemusers.LinkToAttributeName = “systemuserid”;

linkSystemusers.LinkCriteria =new FilterExpression();




EntityCollection collRoles = service.RetrieveMultiple(query);

if (collRoles != null && collRoles.Entities.Count > 0)            {

foreach (Entity _entity in collRoles.Entities)                {

if (_entity.Attributes[“name”].ToString().ToLower() == “{Your rolename}”                    {






How do I call this method :-

public void Execute(IServiceProvider serviceProvider)


IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

IOrganizationService service = factory.CreateOrganizationService(context.UserId);

          CheckUserRoles(service, context.UserId);


Hope it helps 🙂

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: