Share records using C# in Dynamics CRM 2011

These examples shows how to grant/modify/revoke access privileges for a record with team.


        //Share a record Read,Write and Append privileges with Team
        private void SharePrivileges(string targetEntityName, Guid targetRecordID, Guid teamID, bool read_Access, bool write_Access, bool append_Access, IOrganizationService orgService)
        {
            try
            {
                //Get User or Team reference and Target Entity and record ID that needs to be shared.      
                var recordRef = new EntityReference(targetEntityName, targetRecordID);
                var teamRef = new EntityReference("team", teamID);
                AccessRights Access_Rights = new AccessRights();
                Access_Rights = AccessRights.None;
                //Read Access           
                if (read_Access == true)
                    Access_Rights = AccessRights.ReadAccess;
                //Write Access (or) Read, Write Access        
                if (write_Access == true)
                    if (Access_Rights == AccessRights.None)
                        Access_Rights = AccessRights.WriteAccess;
                    else
                        Access_Rights = Access_Rights | AccessRights.WriteAccess;
                //Append Access or all or any two accesses         
                if (append_Access == true)
                    if (Access_Rights == AccessRights.None)
                        Access_Rights = AccessRights.AppendToAccess | AccessRights.AppendAccess;
                    else
                        Access_Rights = Access_Rights | AccessRights.AppendToAccess | AccessRights.AppendAccess;
                var grantAccess = new GrantAccessRequest
                {
                    PrincipalAccess = new PrincipalAccess
                    {
                        AccessMask = Access_Rights,
                        Principal = teamRef
                    },
                    Target = recordRef
                };
                // Execute the Request      
                orgService.Execute(grantAccess);
            }
            catch (Exception ex)
            {
                throw new Exception("An error occured while applying Sharing rules for the record." + ex.Message);
            }
        }
        //Code to modify privileges for the target record and team   
        private void ModifyAccess(string targetEntityName, Guid targetRecordID, Guid teamID, IOrganizationService orgService)
        {
            try
            {
                //Get User or Team reference and Target Entity and record ID that needs to be shared.          
                var RecordReference = new EntityReference(targetEntityName, targetRecordID);
                var teamRef = new EntityReference("team", teamID);
                AccessRights accessRights = new AccessRights();
                accessRights = AccessRights.DeleteAccess;
                var modifyAcess = new ModifyAccessRequest
                {
                    PrincipalAccess = new PrincipalAccess
                    {
                        AccessMask = accessRights,
                        Principal = teamRef
                    },
                    Target = RecordReference
                };
                // Execute the Request   
                orgService.Execute(modifyAcess);
            }
            catch (Exception ex)
            {
                throw new Exception("An error occured in Modifying access." + ex.Message);
            }
        }
        //Code to remvove the sharing privileges for the target record and team    
        private void RevokeAccess(string targetEntityName, Guid targetRecordID, Guid teamID, IOrganizationService orgService)
        {
            try
            {
                //Get User or Team reference and Target Entity and record ID that needs to be shared.        
                var recordRef = new EntityReference(targetEntityName, targetRecordID);
                var teamRef = new EntityReference("team", teamID);
                var revokeAcess = new RevokeAccessRequest
                {
                    Revokee = teamRef,
                    Target = recordRef
                };
                // Execute the Request        
                orgService.Execute(revokeAcess);
            }
            catch (Exception ex)
            {
                throw new Exception("An error occured in Revoking access." + ex.Message);
            }
        }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s