You can use the code below to retrieve the user assigned security roles in C# plugin and JScript.
[Plugin: C# Code]using System; |
using System.Collections.Generic; |
using System.Linq; |
using System.Text; |
using Microsoft.Xrm.Sdk; |
using Microsoft.Xrm.Sdk.Metadata; |
using Microsoft.Crm.Sdk; |
using Microsoft.Crm.Sdk.Messages; |
using Microsoft.Xrm.Sdk.Query; |
using Microsoft.Xrm.Sdk.Messages; |
namespace wod.Crm.UserSecurityRoles |
{ |
public class wod_Plugin : IPlugin |
{ |
public void Execute(IServiceProvider serviceProvider) |
{ |
// Obtain the execution context from the service provider. |
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); |
IOrganizationServiceFactory jj_serviceFactory = null; |
IOrganizationService jj_CrmService = null; |
Guid jj_UserGuid = Guid.Empty; |
EntityCollection jj_UserRoles = null; |
string jj_SecurityRole = string.Empty; |
Try |
{ |
// Obtain the service factory to get the service object |
jj_serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); |
// Obtain service object Imporsonated as plugin calling user |
jj_CrmService = jj_serviceFactory.CreateOrganizationService(context.InitiatingUserId); |
// Setting UserId to plugin initiating User Id |
jj_UserGuid = context.InitiatingUserId; |
//Security Role to Search |
jj_SecurityRole = "Customer Service Representative"; |
#region Retrieve records from an intersect table via QueryExpression |
//Create Query Expression to fetch Role Entity |
QueryExpression jj_Query = new QueryExpression() |
{ |
//Setting the link entity condition and filter condition criteria/ |
LinkEntities = |
{ |
new LinkEntity |
{ |
LinkFromEntityName = "role", |
LinkFromAttributeName = "roleid", |
LinkToEntityName = "systemuserroles", |
LinkToAttributeName = "roleid", |
LinkCriteria = new FilterExpression |
{ |
FilterOperator = LogicalOperator.And, |
Conditions = |
{ |
new ConditionExpression |
{ |
AttributeName = "systemuserid", |
Operator = ConditionOperator.Equal, |
Values = { jj_UserGuid } |
} |
} |
} |
} |
} |
}; |
jj_Query.ColumnSet = new ColumnSet(true); |
// Obtain results from the query expression. |
jj_UserRoles = jj_CrmService.RetrieveMultiple(jj_Query); |
// Searching for a specified Security Role into the list |
Entity jj_UserSecurityRole = jj_UserRoles.Entities.ToList().ToList<Entity>().Find(delegate(Entity jj_RoleEntity) |
{ |
return (string)jj_RoleEntity.Attributes["name"] == jj_SecurityRole; |
}); |
if (jj_UserSecurityRole == null) |
{ |
throw new InvalidPluginExecutionException("Security role not assigned to User."); |
} |
#endregion |
} |
catch (System.Web.Services.Protocols.SoapException ex) |
{ |
throw new InvalidPluginExecutionException(ex.Detail.InnerText); |
} |
catch (Exception ex) |
{ |
throw new InvalidPluginExecutionException(ex.Message); |
} |
} |
} |
} |
// Retrieving current login user security roles by using CRM client API method Xrm.Page.context.getUserRoles(); |
var jj_UserSecurityRoles = Xrm.Page.context.getUserRoles(); |
// Security role GUID (You can get it by opening Security Role record copy web address and go to the website |
// http://meyerweb.com/eric/tools/dencoder , paste the URL and click on Decode button twice then copy Id value |
var jj_CustomerServicesRoleId = “{E324246D-B8AF-E011-AA34-C95682B33448}”; |
var jj_UserHaveSecurityRole = false; |
// Checking if Security Role Id found in User Security Roles |
for (var x = 0; x < UserSecurityRoles.length; x++) { |
if (UserSecurityRoles[x] == jj_CustomerServicesRoleId) { |
jj_UserHaveSecurityRole = ture; |
} |
} |
if (jj_UserHaveSecurityRole == false) { |
throw new exception (“User has not been assigned Customer Service security role.”) |
} |
i got this exception
ReplyDeleteThe 'RetrieveMultiple' method does not support entities of type 'none'.
I got the same error msg..
ReplyDeleteI am trying to use this code in Delete operation pre step and getting the error "The 'RetrieveMultiple' method does not support entities of type 'none'".
ReplyDeleteIt is throwing this exception on this line: "jj_UserRoles = service.RetrieveMultiple(jj_Query);". Do you have any solution or suggestions? Please Help, thanks.
Hi,
ReplyDeletePlease add this code line jj_Query.EntityName = "role"; after the query expression i.e.
//Create Query Expression to fetch Role Entity
QueryExpression jj_Query = new QueryExpression()
{
//Setting the link entity condition and filter condition criteria/
LinkEntities =
{
new LinkEntity
{
LinkFromEntityName = "role",
LinkFromAttributeName = "roleid",
LinkToEntityName = "systemuserroles",
LinkToAttributeName = "roleid",
LinkCriteria = new FilterExpression
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression
{
AttributeName = "systemuserid",
Operator = ConditionOperator.Equal,
Values = { jj_UserGuid }
}
}
}
}
}
};
jj_Query.EntityName = "role";
This still does not work "The key is not present in the dictionary"
ReplyDeleteIt worked. Thanks
ReplyDeleteIt worked..Thanks! :)
ReplyDeletelancel, jimmy choo shoes, chi flat iron, new balance, vans, celine handbags, asics running shoes, gucci, converse, soccer jerseys, vans shoes, north face outlet, ralph lauren, reebok shoes, herve leger, nike roshe, timberland boots, ferragamo shoes, nfl jerseys, beats by dre, abercrombie and fitch, birkin bag, hollister, mac cosmetics, soccer shoes, lululemon, ghd, iphone cases, hollister, valentino shoes, longchamp, instyler, p90x workout, oakley, mcm handbags, ray ban, nike air max, babyliss, hollister, north face outlet, bottega veneta, nike trainers, insanity workout, baseball bats, louboutin, nike huarache, wedding dresses, nike air max, mont blanc, converse outlet
ReplyDeletecanada goose outlet, canada goose, replica watches, louis vuitton, montre pas cher, canada goose, doudoune canada goose, juicy couture outlet, swarovski crystal, moncler, bottes ugg, louis vuitton, canada goose, pandora jewelry, toms shoes, louis vuitton, ugg pas cher, swarovski, karen millen, juicy couture outlet, moncler, pandora charms, canada goose uk, links of london, canada goose outlet, wedding dresses, marc jacobs, pandora charms, hollister, ugg boots uk, moncler, moncler, moncler, moncler outlet, pandora jewelry, ugg,ugg australia,ugg italia, louis vuitton, supra shoes, sac louis vuitton pas cher, moncler, moncler, canada goose, coach outlet, ugg,uggs,uggs canada, thomas sabo
ReplyDeletekaraman
ReplyDeleteordu
urfa
kilis
kütahya
YKVW8