Sample: Associate a security role to a team

Applies to Dynamics 365 for Customer Engagement apps version 9.x

This sample code is for Dynamics 365 for Customer Engagement. Download the Sample: Work with early bound entity classes in code.


Internet connection is required to download the sample project and to restore the NuGet packages used in the sample project.


For more information about the requirements for running the sample code provided here, see Use the sample and helper code.


This sample shows how to assign a security role to a team by using the AssignRequest message. Note that this example does not take into consideration that a team or user can only be assigned a role from its business unit. The role to be assigned is the first from the collection that is returned by the RetrieveMultiple method. If that record is from a business unit that is different from the requesting team, the assignment fails.


// Retrieve a role from CRM.
QueryExpression query = new QueryExpression
    EntityName = Role.EntityLogicalName,
    ColumnSet = new ColumnSet("roleid"),
    Criteria = new FilterExpression
        Conditions =
        // You would replace the condition below with an actual role
        // name, or skip this query if you had a role id.
        new ConditionExpression
            AttributeName = "name",
            Operator = ConditionOperator.Equal,
            Values = {_roleName}

Role role = _service.RetrieveMultiple(query).Entities.

// Add the role to the team.
       new Relationship("teamroles_association"),
       new EntityReferenceCollection() { new EntityReference(Role.EntityLogicalName, _roleId) });

Console.WriteLine("Assigned role to team");

See also

Privilege and Role Entities
Sample: Associate a Security Role to a User
User and Team Entities