Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tito 314 posts 623 karma points
    Mar 28, 2017 @ 12:14
    Tito
    0

    I need to add tags programatically to content. I have set a tag property type to a content type. For this project, I have made an angular app to manage the content, so i have made a custom form where the editor can set tags comma separated.

    Using Umbraco backoffice I can see Umbraco set the comma separated values as the value stored in the content's property but manage tags in this tables:

    • cmsTags
    • cmsTagRelationship

    I cant see any method in the tag service api to create or save tags to a content so I have created a POCO object to manage this, but there are weird behaviours, it seems that when i save the comma separated values to the content item Umbraco save values in the cmsTagRelationship table automatically. How does it work? do i have to manage the db tables or can Umbraco do it for me?

  • Jamie Pollock 174 posts 853 karma points c-trib
    Mar 28, 2017 @ 13:24
    Jamie Pollock
    101

    Hey Tito,
    This assumes you're using a fairly recent version of v7 and also you have access to the ContentService.

            var idOfYourContent = -1;
            var content = Services.ContentService.GetById(idOfYourContent);
            var optionalTagGroup = "";
            content.SetTags("yourContentProperty", new[] { "tag1", "tag2" }, optionalTagGroup);
    
            Services.ContentService.SaveAndPublishWithStatus(content);
    

    I wrote this in a controller but anywhere you have access to ContentService will do. The SetTags method requires the Umbraco.Core.Models namespace.

    Reading your application, you could pass the ID and the tags from your Angular app through to an UmbracoAuthorizedApiController which will have access to the ServiceContext and therefore the ContentService.

    You can read up on it in the API documentation. I hope this helps :)

    Thanks,
    Jamie

  • Tito 314 posts 623 karma points
    Mar 29, 2017 @ 08:33
    Tito
    0

    Thanks Jamie, thats what i was looking for! i didnt know about that SetTags method...

Please Sign in or register to post replies

Write your reply to:

Draft