Sample: Associate a security role to a team

 

Applies To: Dynamics 365 (online), Dynamics 365 (on-premises), Dynamics CRM 2016, Dynamics CRM Online

This sample code is for Microsoft Dynamics 365 (online & on-premises). Download the Microsoft Dynamics CRM SDK package. It can be found in the following location in the download package:

SampleCode\CS\GeneralProgramming\EarlyBound\AssignSecurityRoleToTeam.cs

Requirements

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

Demonstrates

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.

Example



// 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.
    Cast<Role>().First();


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

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

See Also

AssignRequest
Assign
Privilege and role entities
Sample: Associate a Security Role to a User
User and team entities

Microsoft Dynamics 365

© 2016 Microsoft. All rights reserved. Copyright