Plugin: Revoke sharing permissions to all USERS/TEAMS associated to a record
public void Execute(IServiceProvider serviceProvider)
{
try
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
service = factory.CreateOrganizationService(context.UserId);
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
string _contact = entity.LogicalName;
Guid _contactid = entity.Id;
if (entity.LogicalName == "contact")
{
RetrieveSharedPrincipalsAndAccessRequest retrieveSharedRequest = new RetrieveSharedPrincipalsAndAccessRequest()
{
Target = new EntityReference(context.PrimaryEntityName, context.PrimaryEntityId)
};
RetrieveSharedPrincipalsAndAccessResponse retrieveSharedResponse = (RetrieveSharedPrincipalsAndAccessResponse)service.Execute(retrieveSharedRequest);
RevokeAccessRequest revokeRequest = new RevokeAccessRequest();
revokeRequest.Target = new EntityReference(_contact, _contactid);
foreach (PrincipalAccess principal in retrieveSharedResponse.PrincipalAccesses)
{
revokeRequest.Revokee = principal.Principal;
service.Execute(revokeRequest);
}
}
}
}
Note: Please test before using this code
Note: Please test before using this code
No comments:
Post a Comment