Originally published at http://bit.ly/OPyMVn

The team in Dynamics CRM 2011 provides an ability to group users, assign record ownership to a group of users, and extend security roles to users based on their team membership (as opposed to individually assigning the role.)

Teams in Dynamics CRM 2011:

team is a group of users. This organizational structure enables groups of users across an organization to share information. Each team must be associated with only one business unit. A team can include users from any business unit, not only the business unit with which the team is associated. Users can be associated with more than one team.

More details can be found in the Dynamics CRM 2011 SDK Documentation.

Beyond allowing cross-business unit record access and assignment of security roles, the Team entity can provide a simple way to create a group of users.

There is not a simplified method to retrieve teams for a system user or validate that a system user is a member of a specific team. The code included in this blog post will allow you to achieve this functionality in your plug-ins, custom workflows, and external .NET applications that use the SDK libraries.

System Users and Teams have a many-to-many relationship named teammembership. This relationship is, in essence, another entity in CRM even though it is not displayed as an entity in CRM. What this means is that we can link to it in a QueryExpression. This pattern exists for all many-to-many relationships in CRM.

This code can be used as-is or incorporated into a Dynamics CRM Helper class with other methods to simplify your Dynamics CRM 2011 development work.

Note: This code does not include any error handling or logging both of which should be added and that will be addressed in future blog posts.

using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;

namespace Procentrix.Blog
{
    public class TeamsAndUsers
    {
        //Credit goes to http://a33ik.blogspot.com/2009/10/custom−workflow−action−team−members−and.html for getting me started with the queries.

        private IOrganizationService service;

        public TeamsAndUsers(IOrganizationService Service)
        {
            service = Service;
        }

        public bool IsTeamMember(Guid TeamID, Guid UserID)
        {
            QueryExpression query = new QueryExpression("team");
            query.ColumnSet = new ColumnSet(true);
            query.Criteria.AddCondition(new ConditionExpression("teamid", ConditionOperator.Equal, TeamID));
            LinkEntity link = query.AddLink("teammembership", "teamid", "teamid");
            link.LinkCriteria.AddCondition(new ConditionExpression("systemuserid", ConditionOperator.Equal, UserID));

            try
            {
                var results = service.RetrieveMultiple(query);
                if (results.Entities.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                // Do your Error Handling here
                throw ex;
            }
        }

        public bool IsTeamMember(String Teamname, Guid UserID)
        {
            QueryExpression query = new QueryExpression("team");
            query.ColumnSet = new ColumnSet(true);
            query.Criteria.AddCondition(new ConditionExpression("name", ConditionOperator.Equal, Teamname));
            LinkEntity link = query.AddLink("teammembership", "teamid", "teamid");
            link.LinkCriteria.AddCondition(new ConditionExpression("systemuserid", ConditionOperator.Equal, UserID));

            try
            {
                var results = service.RetrieveMultiple(query);
                if (results.Entities.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                // Do your Error Handling here
                throw ex;
            }
        }

        public Microsoft.Xrm.Sdk.EntityCollection UserTeams(Guid UserID)
        {
            QueryExpression query = new QueryExpression("team");
            query.ColumnSet = new ColumnSet(true);
            LinkEntity link = query.AddLink("teammembership", "teamid", "teamid");
            link.LinkCriteria.AddCondition(new ConditionExpression("systemuserid", ConditionOperator.Equal, UserID));

            try
            {
                return service.RetrieveMultiple(query);
            }
            catch (Exception ex)
            {
                // Do your Error Handling here
                throw ex;
            }
        }
    }
}
Categories: Dynamics CRM