Dynamics CRM Access Teams

Access Team Type is introduced in CRM 2013.Team entity has an OptionSet field Team Type (teamtype) that have two options Owner and Access. So Team with Team Type (teamtype=Access) is called Access Team.
The new “Access Teams” functionality in CRM 2013 suits scenarios where data access rules cannot be easily determined upfront but rather need to be set ‘on the fly’ on a record by record basis.  The common example is where an organization assembles a unique group on individuals for each sales pursuit or to manage each customer.


Following diagram shows data flow of access teams.

Implementing Access Teams in Dynamics CRM 2013 
Following is a detail blog on PowerObjects site, how to implement access teams in dynamics CRM 2013.
http://www.powerobjects.com/blog/2013/11/06/access-teams-in-dynamics-crm-2013/

You can control who can add members to the Access Team sub grid. Only users with share privilege for the entity on which the access team exists can add or remove members from the Access Team sub grid. This makes sense—by adding people to the Access Team, you are in effect sharing the record with the people on the team.

Some reusable code related to access teams
You can programmatically manage the membership of access teams, but the team isn’t created until the first member is added. This is for performance reasons—Microsoft doesn’t want to load the system down with record teams if no members are a member of the team.

//Adding users to access teams
AddUserToRecordTeamRequest teamAddRequest = new AddUserToRecordTeamRequest();
teamAddRequest.Record = regardingObjectid; //entity record Guid for which you are adding systemuserid to access team
teamAddRequest.SystemUserId = systemuserid;
teamAddRequest.TeamTemplateId = teamtemplateid;

AddUserToRecordTeamResponse response = (AddUserToRecordTeamResponse)service.Execute(teamAddRequest);


//Removing users from access teams                 
RemoveUserFromRecordTeamRequest teamRemoveRequest = new RemoveUserFromRecordTeamRequest();
teamRemoveRequest.Record =regardingObjectid; //entity record Guid for which you are adding systemuserid to access team
teamRemoveRequest.SystemUserId = systemuserid;
teamRemoveRequest.TeamTemplateId = teamtemplateid;

RemoveUserFromRecordTeamResponse response = (RemoveUserFromRecordTeamResponse)service.Execute(teamRemoveRequest);


//Get team template entity by team template name 
private Entity getTeamTemplateByName(string teamTemplateName, IOrganizationService service)
{
   Entity TeamTemplate = null;

   QueryExpression query = new QueryExpression("teamtemplate");
   query.ColumnSet = new ColumnSet("teamtemplatename");
   query.Criteria.AddCondition("teamtemplatename"ConditionOperator.Equal, teamTemplateName);
   EntityCollection teamtemplateColl = service.RetrieveMultiple(query);
   if (teamtemplateColl != null && teamtemplateColl.Entities.Count > 0)
   {
      TeamTemplate = teamtemplateColl.Entities[0];
   }
   return TeamTemplate;
}

//search for system user if a member of access team
private bool IsUserExistInMembership(Guid regardingId, Guid userid, string teamTemplateName, IOrganizationService service)
{
       
bool IsUserExistInMembership = false;

       
QueryExpression query = new QueryExpression("teamtemplate");
       query.ColumnSet = 
new ColumnSet("teamtemplatename");
       query.Criteria.AddCondition(
"teamtemplatename"ConditionOperator.Equal, teamTemplateName);

       
EntityCollection teamtemplateColl = service.RetrieveMultiple(query);

       
Entity teamTemplate = getTeamTemplateByName(teamTemplateName, service);

       
if (teamTemplate != null)
       {
           
QueryExpression _query = new QueryExpression("teammembership");
           _query.ColumnSet = 
new ColumnSet(new string[] { "systemuserid" });
           _query.Criteria.AddCondition(
"systemuserid"ConditionOperator.Equal, userid);         

           
LinkEntity link = new LinkEntity();
           link.LinkFromAttributeName = 
"teamid";
           link.LinkFromEntityName = 
"teammembership";
           link.LinkToAttributeName = 
"teamid";
           link.LinkToEntityName = 
"team";
           link.LinkCriteria.AddCondition(
"teamtype"ConditionOperator.Equal, 1);
           link.LinkCriteria.AddCondition(
"teamtemplateid"ConditionOperator.Equal, teamTemplate.Id);
           link.LinkCriteria.AddCondition(
"regardingobjectid"ConditionOperator.Equal, regardingId);

           _query.LinkEntities.Add(link);

           
EntityCollection TeamMembers = service.RetrieveMultiple(_query);
           
if (TeamMembers != null && TeamMembers.Entities.Count > 0)
           {
               IsUserExistInMembership = 
true;
           }
        }
        
return IsUserExistInMembership;
}