Find a CRM User’s Roles and Teams using LINQ and Late Binding

Like the example I used with QueryExpression, you’ll see the difference of using LINQ to query Dynamics CRM data.

  1. Follow the first 4 steps in the previous post on using QueryExpression to find a user’s roles and teams. The Console Application, Program.cs should look like this:
    2015-11-23_20h10_58
  2. Use the following LINQ query statement using late binding to find a CRM user by their email address (internalemailaddress).
var systemUserLinq = from u in context.CreateQuery("systemuser")
                     where (string)u["internalemailaddress"] == "homer@simpson.com"
                     select new {
                         UserId = u["systemuserid"],
                         FirstName = u["firstname"],
                         LastName = u["lastname"]
                     };

Alternatively, you can write the LINQ query statement using the GetAttributeValue method

var systemUserLinq = from u in context.CreateQuery("systemuser")
                     where ((string)u["internalemailaddress"]).Equals("homer@simpson.com")
                     select new {
                         UserId = u.GetAttributeValue<Guid>("systemuserid"),
                         FirstName = u.GetAttributeValue<string>("firstname"),
                         LastName = u.GetAttributeValue<string>("lastname")
                     };

 

  1. Use the following code to loop through the systemUserLinq query results and output the first and last name as well as the systemuserid.
foreach (var u in systemUserLinq)
{
    Console.WriteLine(u.FirstName + " " + u.LastName + ":" + u.UserId);
}

Build the solution and the output should look like this:
2015-11-23_20h52_52

  1. Within the foreach loop, add the following LINQ query statement to query the roles of the CRM user and output the roles to the console.
var rolesLinq = from r in context.CreateQuery("role")
                join sr in context.CreateQuery("systemuserroles")
                on r["roleid"] equals sr["roleid"]
                where ((Guid)sr["systemuserid"]).Equals(u.UserId)
                select new {
                    Name = r["name"]
                };
 
foreach (var r in rolesLinq)
{
    Console.WriteLine(r.Name);
}

Build the solution and the output should look like this:
2015-11-23_20h59_04

  1. The LINQ query statement isn’t much different from the roles, use the following LINQ query statement and output the teams to the console.
var teamsLinq = from t in context.CreateQuery("team")
                join tm in context.CreateQuery("teammembership")
                on t["teamid"] equals tm["teamid"]
                where ((Guid)tm["systemuserid"]).Equals(u.UserId)
                select new {
                    Name = t["name"]
                };

                foreach (var t in teamsLinq)
                {
                    Console.WriteLine(t.Name);
                }

Build the solution and the output should look like this:
2015-11-23_21h03_00

Leave a Reply

Your email address will not be published. Required fields are marked *