Thursday 20 July 2017

Sharing and unSharing records using custom workflow

Sharing and Revoke Records to Team using Custom Workflow


Hi all,

Recently I got a requirement for Sharing records using Workflow. As of now I never tried sharing records. By default, we don't have any OOB functionality for sharing records. We have the only option for Assigning records. 

So I decided to create a custom workflow. Here I am pasting my code.

using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Workflow;
using System;
using System.Activities;
using System.Collections.Generic;
using System.Linq;

namespace SharingAccountRecordstoTeam
{
    public class Class1 : CodeActivity
    {
        protected override void Execute(CodeActivityContext executionContext)
        {
            //Create the context
            IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
            Entity account = (Entity)context.InputParameters["Target"];

            // Create the request object and set the target and principal access
            GrantAccessRequest grantRequest = new GrantAccessRequest()
            {
                Target = new EntityReference("account", account.Id),
                PrincipalAccess = new PrincipalAccess()
                {
                    Principal = new EntityReference("team", Team.Get<EntityReference>(executionContext).Id),
                    AccessMask = AccessRights.WriteAccess | AccessRights.ReadAccess | AccessRights.ShareAccess                                     // Mention here what Access you want to provide.
                }
            };

            // Execute the request.
            GrantAccessResponse granted = (GrantAccessResponse)service.Execute(grantRequest);

// Revoke The shared recorde from Team
 RevokeAccessRequest revokeRequest = new RevokeAccessRequest()
            {
                Target = new EntityReference("account", account.Id),
                Revokee = new EntityReference("team", Team.Get<EntityReference>(executionContext).Id),
            };

            // Execute the request.
            RevokeAccessResponse revoked = (RevokeAccessResponse)service.Execute(revokeRequest);
      }
        [Input("Team")]
        [ReferenceTarget("team")]
        public InArgument<EntityReference> Team { get; set; }
    }
}


This is the code for Sharing and revoking records to Team. Once Completed the code, then register your workflow to Plugin Registration Tool. then your workflow will be available in processes. Just add your condition and after that call this Workflow.

Note:   Pass Team Id as a parameter.

This is for the new users who want to create a custom workflow.

Hope it will help you.

Thanks,
Shivaram.


No comments:

Post a Comment