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?
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.
Add tags programatically
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:
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?
Hey Tito,
This assumes you're using a fairly recent version of v7 and also you have access to the ContentService.
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 theServiceContext
and therefore theContentService
.You can read up on it in the API documentation. I hope this helps :)
Thanks,
Jamie
Thanks Jamie, thats what i was looking for! i didnt know about that SetTags method...
is working on a reply...