Sample: Associate a security role to a team

 

Applies To: Dynamics CRM 2013

This sample code is for Microsoft Dynamics CRM 2013 and Microsoft Dynamics CRM Online. 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

SampleCode\VB\GeneralProgramming\EarlyBound\AssignSecurityRoleToTeam.vb

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");


' Retrieve a role from CRM.
Dim query As QueryExpression = New QueryExpression With { _
    .EntityName = Role.EntityLogicalName, .ColumnSet = New ColumnSet("roleid")}
query.Criteria = New FilterExpression()
query.Criteria.AddCondition("name", ConditionOperator.Equal, {_roleName})
        ' You would replace the condition below with an actual role
        ' name, or skip this query if you had a role id.

Dim role_renamed As Role = _service.RetrieveMultiple(query).Entities.Cast(Of Role)().First()


' Add the role to the team.
_service.Associate(Team.EntityLogicalName, _teamId, New Relationship("teamroles_association"), _
                   New EntityReferenceCollection() From {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