Getting Current User Security roles in Silverlight Webresource- MSCRM 2011
27 Jan 2012 Leave a Comment
in Uncategorized Tags: security roles
If you are looking to get current user security roles in silverlight webresource, you can use below function for the same. I am using SilverCrmSoap library available in codeplex and SilverlightUtility that comes with CRM SDK. It will fetch all the security role based on userid,once you have security roles you can loop thorugh it and can check for any particular security role.
private void FetchCurrentUserSecurityRoles(Guid userId)
{
try
{
this.Dispatcher.BeginInvoke(delegate()
{
// Establish a SystemUser link for a query.
// I am using silverlighcrmsoap dll from codeplex
SilverCrmSoap.CrmSdk.LinkEntity systemUserLink = new SilverCrmSoap.CrmSdk.LinkEntity()
{
LinkFromEntityName = “systemuserroles”,
LinkFromAttributeName = “systemuserid”,
LinkToEntityName = “systemuser”,
LinkToAttributeName = “systemuserid”,
LinkCriteria = new FilterExpression
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression(){
AttributeName = “systemuserid”,
Operator = ConditionOperator.Equal,
Values = { userId }
}
}
}
};
// Build the query.
QueryExpression query = new QueryExpression()
{
EntityName = “role”,
ColumnSet = new ColumnSet()
{
Columns = new System.Collections.ObjectModel.ObservableCollection<string>(new string[] { “roleid”, “name” }),
},
LinkEntities =
{
new SilverCrmSoap.CrmSdk.LinkEntity()
{
LinkFromEntityName = “role”,
LinkFromAttributeName = “roleid”,
LinkToEntityName = “systemuserroles”,
LinkToAttributeName = “roleid”,
LinkEntities = {systemUserLink}
}
},
};
OrganizationRequest request = new OrganizationRequest() { RequestName = “RetrieveMultiple” };
request["Query"] = query;
IOrganizationService service = SilverlightUtility.GetSoapService();
service.BeginExecute(request, new AsyncCallback(CheckCurrentUserSecurityRole_CallBack), service);
});
}
catch (Exception Ex)
{
throw Ex;
}
}
private void CheckCurrentUserSecurityRole_CallBack(IAsyncResult result)
{
try
{
this.Dispatcher.BeginInvoke(delegate()
{
OrganizationResponse response = ((IOrganizationService)result.AsyncState).EndExecute(result);
EntityCollection results = (EntityCollection)response["EntityCollection"];
foreach (SilverCrmSoap.CrmSdk.Entity _usersecurityrole in results.Entities)
{
string _rolename = _usersecurityrole.GetAttributeValue<String>(“name”);
}
});
}
catch (Exception Ex)
{
throw Ex;
}
}
Hope it will help somebody !!


