Export (0) Print
Expand All

Sample: Associate a security role to a team

Applies To: CRM 2015 on-prem, CRM Online

This sample code is for Microsoft Dynamics CRM 2015 and Microsoft Dynamics CRM Online 2015 Update. 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");

See Also

Microsoft Dynamics CRM 2015 and Microsoft Dynamics CRM Online
Send comments about this topic to Microsoft.
© 2015 Microsoft. All rights reserved.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2015 Microsoft