Dynamics 365 (On-Prem):与多个团队共享记录 - C#

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

我有一种情况需要与所有团队共享记录。我所有的痕迹显示,所有的队名和记录都是应该的(我把代码里的痕迹拿出来保存space)。但是,记录不会在测试时共享。我写对了吗?我想我的逻辑是遍历所有团队并共享记录。用那个拔我的头发。以下是我的wf汇编代码:

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

}
}

嗯,我想我回答了我自己的问题。我花了几个小时才发现我遗漏了以下 1 行重要代码:

GrantAccessResponse granted = (GrantAccessResponse)serice.Execute(grant);

添加这个有效。

一切都很完美,除了缺少下面一行:

service.Execute(grant);