Properly using RuleExecutionContexts and Tokens

I'm having an issue using the RuleExecutionContext on a Rule Event with custom Tokens. Essentially, I created a rule for when a user Posts (either reply or new thread). I also created some custom tokens to display for the "When" condition. I only want my tokens to show, but when I set it up, I lose access to the User context on the Action portion of the rule. How can I only show my tokens but still be able to select the triggering user on the Event part of the rule? I've tried a few different ways, but the documentation isn't quite clear. I've tried adding the Get<users> API to my Contextual Data Type ID as well as the Get<Users> API Content Type ID but neither seem to work unless I also add the ContentTypeId for Get<Users> to the ContectualDataTypeIds Enumerable. But if I do that, then it exposes all tokens associated with that ContentTypeId to the Conditional portion of the rules.

// Code from the rule Event
public IEnumerable<Guid> ContextualDataTypeIds
		{
			get
			{
                return new[]
                {
					/*Apis.Get<Users>().ContentTypeId,
					Apis.Get<IForumThreads>().ContentTypeId,*/
                    Guid.Parse("760f7569-ddde-4ac6-8aeb-722e47eb7d83") // My custom DataTypeId from PostRuleTokens
				};
			}
		}

		public RuleTriggerExecutionContext GetExecutionContext(RuleTriggerData data)
		{
			var context = new RuleTriggerExecutionContext();
			var contentId = Guid.Parse(data["UserContentId"]);
			var threadId = int.Parse(data["ThreadId"]);
            context.Add(Apis.Get<Users>().ContentTypeId, Apis.Get<Users>().Get(new UsersGetOptions() { ContentId = contentId }));
			context.Add(Apis.Get<IForumThreads>().ContentTypeId, Apis.Get<IForumThreads>().Get(threadId));

            return context;
		}

// Custom Tokens
public void RegisterTokens(ITokenizedTemplateTokenController tokenController)
        {
            var dataTypeId = new Guid("760f7569-ddde-4ac6-8aeb-722e47eb7d83");

            tokenController.Register(new TokenizedTemplateToken(
                "PostCount",
                "PostCountDescription",
                _controller,
                Guid.Parse("e558704b-b336-4174-a149-877d8f182c14"),
                dataTypeId,
                PrimitiveType.Int,
                context => memberService.GetMemberThreadAndReplyPostsCount(context.Get<User>(dataTypeId).Id ?? 0)
            ));

            tokenController.Register(new TokenizedTemplateToken(
                "PostDate",
                "PostDateDescription",
                _controller,
                Guid.Parse("fdc87d45-4856-4fb5-bdb0-bdcd74b25e7f"),
                dataTypeId,
                PrimitiveType.DateTime,
                context => Convert.ToDateTime("2018-01-01 00:00:00.000") //Test data
                ));
        }

Parents
  • You do need the user's ContentTypeId (or DataTypeId, for users they are the same) in the ContextualDataTypeIds list - this alerts the rule engine all the relevant content or data types affected by that IRuleTrigger and allows it to wire up the correct lists for Conditions and Actions. The idea of the rule engine is that the parts are modular. You don't, as a developer, control what an admin might do with your rule trigger, beyond specifying what types of information can be checked (logically you can't check details of a forum reply if the rule trigger is about creating a blog post).

    So in short, you aren't able to specify different context for conditions and actions, because all the rule trigger can specify is what types of information the trigger concerns, and both conditions and actions should be able to operate on any part of that provided information.

  • Thanks.. that's what I figured, but, wanted to confirm before moving forward. Thanks for the quick response!

Reply Children
No Data