1

I want to stop publishing a structure groups for certain user groups. I'm able to catch the structure group and filter per the desired groups, but when I throw the exception, nothing happens and the structure group is sent to the publish queue.

    private void SubScribe()
    {
        EventSystem.Subscribe<StructureGroup, PublishOrUnPublishEventArgs>(OnPublishTransactionPublish, EventPhases.Initiated);
    }

    private void OnPublishTransactionPublish(StructureGroup structureGroup, PublishOrUnPublishEventArgs e, EventPhases phase)
    {
        if (structureGroup == null)
            return;

        // get the person who created the publish transaction (= the one who published the item)
        var creator = new User(structureGroup.Session.AccessToken.Id, structureGroup.Session);
        var groups = creator.GroupMemberships;
        var structureGroupId = structureGroup.Id.ItemId.ToString();

        if (structureGroupId == tcmHomeStructureGroup)
        {
            if (!groups.Where(x => x.Group.Title == homeAllowedGroup).Any())
            {
                ResourceManager rm = new ResourceManager("MijnPostNL.Tridion.Addons", typeof(Resources).Assembly);
                throw new PublisherException(new LocalizableMessage(rm, "cannotPublish", new object[] { structureGroup.Id }));
            }
        }
        if (structureGroupId == tcmPersoneelsinfoStructureGroup)
        {
            if (groups.Where(x => personeelsinfoNotAllowedGroups.Contains(x.Group.Title)).Any())
            {
                throw new PublisherException(new LocalizableMessage(Resources.ResourceManager, "cannotPublish", new object[] { structureGroup.Id }));
            }
        }
    }
4
  • 1
    Did you try just "Exception" instead of "PublisherException"?
    – Shiva
    Commented Jun 15, 2020 at 14:01
  • You ought to be able to see the exception in the Windows Event Viewer. Can you confirm this? Commented Jun 15, 2020 at 15:01
  • I cannot imagine that nothing happens when you throw an exception. You might get fooled because your event handler isn’t loaded in the apropriate hosts yet (in case of CME, you should restart the TCM Service Host). Commented Jun 16, 2020 at 16:46
  • BTW: you should use Session.AccessToken directly rather than loading a new User object by ID (in that way you loose the expanded group memberships). Commented Jun 16, 2020 at 16:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.