Skip to main content

Notifications

Announcements

No record found.

Manage Access Team Members and Template between entities using C#

Microsoft Access team is new and wonderful feature in Dynamics 365 CRM /CE. You can control the user access against one record using Access Team. if You want to give specific users specific rights against a single record you can use achieve this by using access team functionality. You can configure the access team by using the following simple steps in this docs. Access Team Configuration steps.

If you want to perform some automation and you want get the members from Access team of a record and add the same members into other record using Custom workflow and Plugins or C# code. 

For Example: Your requirement is to move the access team members from Lead record on the Opportunity during lead qualification. the below sample code will help you.  Its a Custom Workflow code activity and passing Access Team Templates for Source(Lead), Target (Opportunity) and Source Record ID in parameters. Select the respective Template records from lookup and pass the Originating lead lookup and record ID when custom work step added in workflow as in below picture. 

parameters passing in Workflow steps

Here is code activity. for any detail and help you can comments and contact me for help.

 

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Activities;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Crm.Sdk.Messages;

namespace CRM.Workflows
{
    public class MoveAccessTeamMembers : CodeActivity
    {
        #region Input Arguments
        [RequiredArgument]
        [Input("SourceTeamTemplate")]
        [ReferenceTarget("teamtemplate")]
        public InArgument SourceTeamTemplate { get; set; }

        [RequiredArgument]
        [Input("TargetTeamTemplate")]
        [ReferenceTarget("teamtemplate")]
        public InArgument TargetTeamTemplate { get; set; }

        [RequiredArgument]
        [Input("SourceEntityID")]
        [ReferenceTarget("lead")]
        public InArgument SourceEntityLookup { get; set; }
        #endregion

        #region Global Attributes
        IOrganizationService _service = null;
        ITracingService _tracingService = null;
        Guid _SourceTeamTemplateID;
        Guid _TargetTeamTemplateID;
        EntityReference _SourceER=null;
        Guid _SourceEntityId;
        Guid _targetEntityId;
        #endregion

        protected override void Execute(CodeActivityContext _executionContext)
        {
            try
            {
                _tracingService = _executionContext.GetExtension();
                if (_tracingService == null)
                {
                    throw new InvalidPluginExecutionException("Failed to retrieve tracing service.");
                }
                _tracingService.Trace("Entered Move Access Team Templates, Activity Instance Id: {0}, Work flow Instance Id: {1}",
                _executionContext.ActivityInstanceId,
                _executionContext.WorkflowInstanceId);

                //Create the context
                IWorkflowContext context = _executionContext.GetExtension();
                if (context == null)
                {
                    throw new InvalidPluginExecutionException("Failed to retrieve work flow context.");
                }

                IOrganizationServiceFactory serviceFactory = _executionContext.GetExtension();
                _service = serviceFactory.CreateOrganizationService(context.UserId);
                _targetEntityId = context.PrimaryEntityId;

                if (SourceTeamTemplate.Get(_executionContext) != null)
                {
                    _tracingService.Trace("Team Template ID from parameters");
                    _SourceTeamTemplateID = SourceTeamTemplate.Get(_executionContext).Id;
                }

                if (TargetTeamTemplate.Get(_executionContext) != null)
                {
                    _tracingService.Trace("Team Template ID from parameters");
                    _TargetTeamTemplateID = TargetTeamTemplate.Get(_executionContext).Id;
                }

                if (SourceEntityLookup.Get(_executionContext) != null)
                {
                    _tracingService.Trace("Source entity from parameters");
                    _SourceER = SourceEntityLookup.Get(_executionContext);
                    _SourceEntityId = _SourceER.Id;
                }
                if (_SourceEntityId != null && _SourceTeamTemplateID!=null)
                {
                    _tracingService.Trace("get all Team members ID");
                    throw new InvalidPluginExecutionException("Issue in Workflow RejectCASAIndividualAppforIranBorn: "   ex.Message);
                    EntityCollection _TeamMmenbers = GetUsersFromOpportunitySalesTeam(_service, _SourceEntityId, _SourceTeamTemplateID);
                    _tracingService.Trace(" Retrieve User Guids");
                    if (_TeamMmenbers != null && _TeamMmenbers.Entities.Count > 0)
                    {
                        foreach (var teamMember in _TeamMmenbers.Entities)
                        {
                            if (teamMember.Attributes.Contains("systemuser5.systemuserid") && teamMember.Attributes["systemuser5.systemuserid"] != null)
                            {
                                Guid _userId = new Guid(((AliasedValue)teamMember.Attributes["systemuser5.systemuserid"]).Value.ToString());
                                if(_userId!=null)
                                {
                                    AddUserToRecordTeamRequest _addUserRequest = new AddUserToRecordTeamRequest()
                                    {
                                        Record = new EntityReference("opportunity", _targetEntityId),
                                        SystemUserId = _userId,
                                        TeamTemplateId = _TargetTeamTemplateID
                                    };
                                    AddUserToRecordTeamResponse _addTeamResponse = (AddUserToRecordTeamResponse)_service.Execute(_addUserRequest);
                                }

                            }
                        }
                    }
                }
                }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException("Issue in Workflow MoveAccessTeamMembers: "   ex.Message);

            }
        }
        #region get all team members of access Team

        public EntityCollection GetUsersFromOpportunitySalesTeam(IOrganizationService service, Guid _SourceEntId, Guid _teamTemplateId)
        {
            try
            {
                var fetchUsersFromAccessTeam = $@"
		         
		        
		               
		        
		        
		        
		        
		        
		         
		        
		        
		        
		         
                
		        
		        
		        
		        
		        
		        ";
                var teamMembers = service.RetrieveMultiple(new FetchExpression(fetchUsersFromAccessTeam));
                return teamMembers;
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException("Issue in Workflow GetUsersFromOpportunitySalesTeam: "   ex.Message);
            }
            return null;
        }

        #endregion

    }
}

Comments

*This post is locked for comments