Sunday 29 June 2014

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

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;
    }
}

1 comment:

  1. Are you looking to make money from your websites/blogs by using popunder advertisments?
    If so, have you ever consider using PopAds?

    ReplyDelete

My Name is..