Hi,

Below is the code to Share/Unshare a record to Team using “GrantAccessRequest“.

//Sharing a Record to Team

using Microsoft.Crm.Sdk.Messages;

private void ShareRecordToTeam(IOrganizationService service, Guid sharingRecordId, Entity objTeam)

{

GrantAccessRequest grantRequest = new GrantAccessRequest()

{

Target = new EntityReference(“{Entity Name}”, sharingRecordId),

PrincipalAccess = new PrincipalAccess()

{

Principal = new EntityReference(objTeam.LogicalName, objTeam.Id),

AccessMask = AccessRights.WriteAccess

}

};

// Execute the request.

GrantAccessResponse granted = (GrantAccessResponse)service.Execute(grantRequest);

}

//Unshare a record From Team

private void UnShareRecordFromTeam (IOrganizationService service, Guid sharingRecordId, Entity objTeam)

{

RevokeAccessRequest revokeRequest = new RevokeAccessRequest()

{

Target = new EntityReference(“{Entity Name}”, sharingRecordId),

Revokee =new EntityReference(objTeam.LogicalName, objTeam.Id)

};

// Execute the request.

RevokeAccessResponse revoked = (RevokeAccessResponse)service.Execute(revokeRequest);

}

You can use the same code to Share/Unshare a record to User. You just need to pass User details instead of Team like below

Principal = new EntityReference(“systemuser”, userID)

Hope it helps 🙂

Advertisements
Advertisements

One response to “CRM 2011 – Team sharing in Plug in”

  1. prasun2005 Avatar

    I am writing the below code to assign a account record to a team . I am getting an error on the last line “The given key was not present in the dictionary.” .

    Could you please tell me where is the problem ?

    Guid TeamID = new Guid(“9e4c4497-2811-e111-bcdf-00155d017a04”);
    Guid accountID = new Guid(“50345443-8a70-e111-a46f-00155d017a04”);

    AssignRequest assignRequest = new AssignRequest()
    {
    Assignee = new EntityReference
    {
    LogicalName = “team”,
    Id = TeamID
    },

    Target = new EntityReference(“account”, accountID)
    };

    service.Execute(assignRequest);

Leave a comment