web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :

CRM 2011/2013 Share and Un-Share Records Programmatically C#

Aileen Gusni Profile Picture Aileen Gusni 44,524
Please refer to these codes, to Share Records, Share Record only for Read, and Unshare Records.
public void ShareRecord(IOrganizationService service, Entity TargetEntity, Entity TargetShare)
{
//no delete access
GrantAccessRequest grant = new GrantAccessRequest();
grant.Target = new EntityReference(TargetEntity.LogicalName, TargetEntity.Id);

PrincipalAccess principal = new PrincipalAccess();
principal.Principal = new EntityReference(TargetShare.LogicalName, TargetShare.Id);
principal.AccessMask = AccessRights.ReadAccess | AccessRights.AppendAccess | AccessRights.WriteAccess | AccessRights.AppendToAccess | AccessRights.ShareAccess | AccessRights.AssignAccess;
grant.PrincipalAccess = principal;

try
{
GrantAccessResponse grant_response = (GrantAccessResponse)service.Execute(grant);
}
catch (Exception ex)
{
throw ex;
}
}
public void ShareRecordReadOnly(IOrganizationService service, Entity TargetEntity, Entity TargetShare)
{
//no delete access
GrantAccessRequest grant = new GrantAccessRequest();
grant.Target = new EntityReference(TargetEntity.LogicalName, TargetEntity.Id);

PrincipalAccess principal = new PrincipalAccess();
principal.Principal = new EntityReference(TargetShare.LogicalName, TargetShare.Id);
principal.AccessMask = AccessRights.ReadAccess;
grant.PrincipalAccess = principal;

try
{
GrantAccessResponse grant_response = (GrantAccessResponse)service.Execute(grant);
}
catch (Exception ex)
{
throw ex;
}
}
public void UnShareRecord(IOrganizationService service, Entity TargetEntity, Entity TargetShare)
{
//no delete access
ModifyAccessRequest modif = new ModifyAccessRequest(); ;
modif.Target = new EntityReference(TargetEntity.LogicalName, TargetEntity.Id);

PrincipalAccess principal = new PrincipalAccess();
principal.Principal = new EntityReference(TargetShare.LogicalName, TargetShare.Id);
principal.AccessMask = AccessRights.None;
modif.PrincipalAccess = principal;

try
{
ModifyAccessResponse modif_response = (ModifyAccessResponse)service.Execute(modif); ;
}
catch (Exception ex)
{
throw ex;
}
}

This was originally posted here.

Comments

*This post is locked for comments