Friday, 13 April 2012

Assign a Security Role to Team in CRM 2011

In this sample code , I am explaining how to assign a security role to a team using the AssignRequest message

public void Run(ServerConnection.Configuration serverConfig, bool promptForDelete)
{
    using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri,
             serverConfig.HomeRealmUri,
             serverConfig.Credentials,
             serverConfig.DeviceCredentials))
   {
       _serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
       _service = (IOrganizationService)_serviceProxy;

       QueryExpression query = new QueryExpression
       {
           EntityName = Role.EntityLogicalName,
           ColumnSet = new ColumnSet("roleid"),
           Criteria = new FilterExpression
           {
             Conditions =
             {
               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) });

    }

}

No comments:

Post a Comment