1

I'm using Sitecore 9.1.1 and SXA 1.8.1, I enabled content tagging, I want to programmatically apply the content tagging to a specific item. Is there a way to programmatically trigger the Tag item tab and pass the item to it for example by calling an API (if there is an OOTB API that does so) ?

enter image description here

EDIT:

I tried the IContentTaggingRunner and this was the output:

1] the data wasn't cleared as shown in the picture and its the same IDs in every Item. 2] the data is added in the semantics not the tag field.

Does anyone know the solution? enter image description here

2 Answers 2

3

Looking at the code for that button in the TagContentCommand class, you need to use the IContentTaggingRunner to call that. Here is an example taken from the command class for that button:

public class MyTaggingClass
{
    private readonly IContentTaggingRunner _contentTaggingRunner;

    public MyTaggingClass(IContentTaggingRunner contentTaggingRunner)
    {
        _contentTaggingRunner = contentTaggingRunner;
    }

    protected void RunTagging(Item contentItem)
    {
        using (new SecurityEnabler())
        {
            if (contentItem.Security.CanWrite((Account) Context.User))
            {
                _contentTaggingRunner.Run(contentItem, messageBus);
            }
            else
            {
                Log.Warn(string.Format("Item '{0}' was not tagged. User doesn't have write access to it.", (object) contentItem.Paths.Path), (this);
            }
        }
    }
}

The IContentTaggingRunner.Run method just takes the content item. If you want to run this from the UI, there is a message bus that you can use to update the UI too. Just look at the code for TagContentCommand to see how its used.

2

There is an example in the documentation on how to invoke in your solution: https://doc.sitecore.com/developers/91/sitecore-experience-management/en/use-the-sitecore-cortex-content-tagging-framework-in-your-custom-solution.html

Get the ContentTaggingRunner and MessageBusFactory services as follows:

var ContentTaggingRunner = ServiceLocator.ServiceProvider.GetService<IContentTaggingRunner>();
var MessageBusFactory = ServiceLocator.ServiceProvider.GetService<IMessageBusFactory>();

Create a message bus. The UI can communicate with the process through the message bus. You must also implement a custom message handler.

var messageBus = MessageBusFactory.Create();
var messageHandler = new CustomMessageHandler();
messageBus.Subscribe(messageHandler);

Pass the content item and message bus instance to the runner.

ContentTaggingRunner.Run(item, messageBus);

2
  • i tried ur solution and i edited my question with the result.can you please check it and advise?
    – AME
    May 22, 2019 at 9:54
  • Hi @AmiraElEwady, if you have a new question, please post a new question. If you felt my answer answered your original question, that is great, but it is best practice not to change the goal of a question as it invalidates previously provided answers. May 22, 2019 at 16:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.