Sample: Associate a security role to a team


Applies To: Dynamics CRM 2016, Dynamics CRM Online

This sample code is for Microsoft Dynamics CRM Online 2016 Update and Microsoft Dynamics CRM 2016 (on-premises). Download the Microsoft Dynamics CRM SDK package. It can be found in the following location in the download package:



For more information about the requirements for running the sample code provided in this SDK, 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");

Microsoft Dynamics CRM 2016 and CRM Online

© 2016 Microsoft. All rights reserved. Copyright

Community Additions