D365 (On-Prem): Share record with multiple teams - C#

Multi tool use


D365 (On-Prem): Share record with multiple teams - C#
I have a situation where I will need to share records with all teams. All my traces show that all the team names and record are what they should be (I took out the traces in the code to save space). However, the record isn't sharing upon testing. Do I have this written correctly? I guess my logic was to loop through all teams and share the record. Pulling my hair out with the one. The following is my wf assembly code:
using Microsoft.Xrm.Sdk.Workflow;
using System;
using System.Activities;
using System.Diagnostics;
using System.ServiceModel;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
namespace workfow_ShareWithAllTeams
{
public class workfow_ShareWithAllTeams : CodeActivity
{
protected override void Execute(CodeActivityContext executionContext)
{
ITracingService tracer = executionContext.GetExtension<ITracingService>();
IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
try
{
Entity entity = (Entity) context.InputParameters["Target"];
//TODO: Do stuff
QueryExpression qe = new QueryExpression();
qe.EntityName = "team";
qe.ColumnSet = new ColumnSet();
qe.ColumnSet.Columns.Add("teamid");
qe.ColumnSet.Columns.Add("name");
var teams = service.RetrieveMultiple(qe);
Guid Id = context.PrimaryEntityId;
QueryExpression query = new QueryExpression("item");
query.ColumnSet = new ColumnSet();
query.ColumnSet.Columns.Add("itemid");
query.ColumnSet.Columns.Add("name");
query.Criteria.AddCondition(new ConditionExpression("itemid", ConditionOperator.Equal, Id));
var recordToShare = service.RetrieveMultiple(query);
foreach (Entity team in teams.Entities) //looping through all teams to share
{
foreach (Entity record in recordToShare.Entities)//only one record in entity collection
{
GrantAccessRequest grant = new GrantAccessRequest();
grant.Target = new EntityReference(entity.LogicalName, entity.Id);
PrincipalAccess principal = new PrincipalAccess();
principal.Principal = new EntityReference(team.LogicalName, team.Id);
principal.AccessMask = AccessRights.ReadAccess | AccessRights.AppendAccess |
AccessRights.WriteAccess | AccessRights.AppendToAccess |
AccessRights.ShareAccess | AccessRights.AssignAccess;
grant.PrincipalAccess = principal;
}
}
}
catch (Exception e)
{
throw new InvalidPluginExecutionException(e.Message);
}
}
}
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.