Sample code to find all Security Roles for a System User:
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Tooling.Connector;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using Xrm;
private List<Role> GetAllSecurityRolesForSystemUser(Guid systemUserId)
{
// Initiate CRM-connection
var conn = new CrmServiceClient(ConfigurationManager.ConnectionStrings["CONN"].ConnectionString);
var service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;
var context = new OrganizationServiceContext(service);
var roles = (from ur in context.CreateQuery<SystemUserRoles>()
join r in context.CreateQuery<Role>() on ur.RoleId equals r.RoleId
where ur.SystemUserId == systemUserId
select r).ToList();
return roles;
}
Observe: The above code requires a generated XRM early bound entity file for the Role and SystemUserRole entities.