Reputation: 4821
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);
}
}
}
}
Upvotes: 0
Views: 332
Reputation: 4821
Well, I think I answered my own question. Took me hours to figure I was missing the following 1 line of vital code:
GrantAccessResponse granted = (GrantAccessResponse)serice.Execute(grant);
Adding this worked.
Upvotes: 1
Reputation: 22836
Everything is perfect, except the below line is missing:
service.Execute(grant);
Upvotes: 1