Please wait, loading...

 

Share A Record With Read Only Access Using Custom Workflow IN MS CRM 2011 , MS CRM 2013 , MS CRM 2015

January 5, 2015
Share a Record to a user without Assigning the record and with out giving Read access to that user , its a Custom Workflow to share a record with read only access , ( NOTE : You can not give read only access to owner of that record so , first we have to change the owner and then we can share it to created by user with read only access )


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Portal;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Workflow;
using System.Activities;
using Microsoft.Crm.Sdk.Messages;


namespace Readonlyaccess_sharerecord
{
    public class Class1 : CodeActivity
    {
        #region input parameter
        // In set property : give the user to whom record will be shared
        [ Input( “User_to_Share” )]
        // look up field of system user “Entity Name”
        [ ReferenceTarget (“systemuser” )]
        public InArgument <EntityReference > _User_to_Share { getset; }
        #endregion

        #region Output parameter
        //
        [ Output( “condition” )]
        public OutArgument <string > _condition { getset; }

        #endregion
        protected override void Execute( CodeActivityContext context)
        {

            IWorkflowContext workflowContext = context.GetExtension<IWorkflowContext >();
            IOrganizationServiceFactory serviceFactory = context.GetExtension<IOrganizationServiceFactory >();
            IOrganizationService service = serviceFactory.CreateOrganizationService(workflowContext.UserId);
            #region paramters
            EntityReference User_to_Share = _User_to_Share.Get<EntityReference >(context);

            #endregion
            try
            {
                Entity Obj_entity = (Entity )service.Retrieve(workflowContext.PrimaryEntityName, workflowContext.PrimaryEntityId, newColumnSet (true ));

                try
                {
                    //no delete access
                    GrantAccessRequest grant = new GrantAccessRequest ();
                    grant.Target = new EntityReference (workflowContext.PrimaryEntityName, workflowContext.PrimaryEntityId);

                    PrincipalAccess principal = new PrincipalAccess ();
                    principal.Principal = User_to_Share;
                    principal.AccessMask = AccessRights .ReadAccess;
                    grant.PrincipalAccess = principal;
                    GrantAccessResponse grant_response = (GrantAccessResponse )service.Execute(grant);
                    _condition.Set(context, “1” );

                }
                catch ( Exception ex)
                {
                    _condition.Set(context, “0” );
                    throw new Exception( “Enable to Grant Read Access i.e.” + ex.Message);
                }

            }
            catch ( Exception ex)
            {
                throw new Exception( “Error Due To Master User Share i.e.” + ex.Message);
            }

        }
    }
}

1. Add Step to your Workflow.

2. Set Property and provide the user whom to share the record.

3. Forms become readonly after execution of record

SOURCE : JUST2CODE.IN Subscribe to our YouTube channel : https://www.youtube.com/user/TheRussell2012

https://i0.wp.com/microsoftdynamics.in/wp-content/uploads/2020/04/Microsoftdynamics365.png?fit=640%2C651&ssl=1
Microsoft Dynamics Community Profile

Learn more