In this article, we want to write a console app which will write the "Full Name" of the users in a specific security role in CRM 2011.
1. Generate your Organization service proxy
From the downloaded SDK, find crmsvcutil.exe, open a command window, chage your directry to the same folder where crmsvcutil.exe is located and type the following:
crmsvcutil.exe /url:"crmpath/orgname/XrmServices/2011/Organization.svc"/out:"OrganizationService.cs" /username:"crmuser" /password:"crmPass" /domain:"crmDomain" /namespace:"DynamicsCrm" /ServiceContextName:CrmServiceContext
2. Create a new console app (C#)
Add references and add the using statement on top to point to:
using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Net; using DynamicsCrm; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Client; using System.ServiceModel.Description; using Microsoft.Xrm.Sdk.Query;add the OrganizationService.CS that you have generated earlier to your app console.
3. Edit app.config
In your app.config file of your console app add the following keys and values
< configuration>
  < appsettings>
    < add key="TargetCrmService" value="http://crm/org/XrmServices/2011/Organization.svc">
    < add key="UserName" value="yourUserName">
    < add key="Password" value="CrmPass">
    < add key="Domain" value="yourDomain">
  < / appsettings="">
< startup>< supportedruntime sku=".NETFramework,Version=v4.0" version="v4.0" >< / startup>
< /configuration="">
Note1: please remove additional spaces aboveNote2: Make sure your console app project runs under .NET framework 4.0
4. Coding
Here is the code in the console app to retirve the Full Name of the users within a specific "Security role" (System administrators in this example).
namespace CRMPlayGround
{
    internal class Program
    {
        private static readonly string TargetCrmService = ConfigurationManager.AppSettings["TargetCrmService"];
        private static readonly string UserName = ConfigurationManager.AppSettings["UserName"];
        private static readonly string Domain = ConfigurationManager.AppSettings["Domain"];
        private static readonly string Password = ConfigurationManager.AppSettings["Password"];
        public static ClientCredentials ClientCredentials
        {
            get
            {
                var credentials = new ClientCredentials();
                credentials.Windows.ClientCredential = new NetworkCredential(UserName, Password, Domain);
                return credentials;
            }
        }
        private static void Main(string[] args)
        {
            Console.WriteLine("*********************************************************************");
            Console.WriteLine("*                                                                   *");
            Console.WriteLine("*               CRM play Ground                                     *");
            Console.WriteLine("*                                                                   *");
            Console.WriteLine("*********************************************************************");
            Console.WriteLine();
            Console.WriteLine("Connecting to CRM...");
            var serviceProxy = new OrganizationServiceProxy(new Uri(TargetCrmService), null, ClientCredentials, null);
            serviceProxy.EnableProxyTypes();
            var crmServiceContext = new CrmServiceContext(serviceProxy);
            Console.WriteLine("Finding the first user in CRM who is Admin and has a manager...");
            var userList = FindUsersWithSecurtyRole(crmServiceContext,"System Administrator");
            foreach (var username in userList)
            {
                Console.WriteLine(username);
            }
            Console.WriteLine();
            Console.WriteLine("Done! press any key to close ...");
            Console.ReadKey();
        }
        /// 
        /// Find Users who have a specific security role.
        ///  
        /// 
        /// Each SystemUser in CRM has N:N relationship with UserRoles
        ///  
        /// The CRM service context./// Security role name/// List of user's fullname as string 
        private static IEnumerable FindUsersWithSecurtyRole(CrmServiceContext crmServiceContext, string roleName)
        {
            var users = from systemUser in crmServiceContext.SystemUserSet
                         join systemUserRole in crmServiceContext.SystemUserRolesSet
                             on systemUser.SystemUserId equals systemUserRole.SystemUserId
                         join role in crmServiceContext.RoleSet
                             on systemUserRole.RoleId equals role.RoleId
                         where role.Name == roleName
                         select systemUser;
            foreach (var user in users)
            {
                yield return user.FullName;
            }
        }
    }
} 
 
No comments:
Post a Comment