简体   繁体   English

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

[英]Dynamics 365 (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: 以下是我的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);
}
}

}
}

Well, I think I answered my own question. 好吧,我想我回答了我自己的问题。 Took me hours to figure I was missing the following 1 line of vital code: 花了我几个小时才弄清楚我缺少以下1条重要代码:

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

Adding this worked. 添加此工作。

一切都很完美,但缺少以下行:

service.Execute(grant);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM