I have been looking around to try and figure out how to add tags to my content utilizing the content service. I started off at the following forum post and that didn't work. I couldn't Find any Documentation on the SetTags method anywhere, so I backed down a bit and tried the following code:
content.SetValue("keyWords", "test");
All of my simple properties are updating correctly, but my tag properties has 4 one-letter tags. I've tried passing in an array of string only to be told the best matching overload has invalid arguments. What am I missing?
That didn't work. I tried passing in alias as a System.Collections.Specialized.NameValueCollection
but it gave me an error saying "The best overloaded method match for 'Umbraco.Core.Models.ContentBase.SetPropertyValue(string, string)' has some invalid arguments". You did get me to thinking though. I tried passing the second argument in as an object and it still gives me the same error. If you look at the documentation, there are more than 5 overloads for the setValue method.
So it should be available for you to use, if you add
using Umbraco.Core.Models
at the top of your code file.
Then you should be able to use it to add your tags like so:
//tag storage type, this can be either Csv or Json
TagCacheStorageType storageType = TagCacheStorageType.Csv;
//whether to add/merge to existing set tags or replace existing tags with this set of tags
bool replaceTags = true;
//optional tagGroup
string tagGroup = "default";
IContent content= contentService.GetById(nodeId);
string[] theTags = new string[] { "Test1", "Tag4" };
content.SetTags(storageType,"keyWords", theTags, replaceTags,tagGroup);
It appears that the setTags function is treating the tags as csv by default.
The default setting when adding an editor for the document property indicates that json is the default and csv is legacy, so that kind of caught me off guard.
Setting Content Tags via C# API
I have been looking around to try and figure out how to add tags to my content utilizing the content service. I started off at the following forum post and that didn't work. I couldn't Find any Documentation on the SetTags method anywhere, so I backed down a bit and tried the following code:
All of my simple properties are updating correctly, but my tag properties has 4 one-letter tags. I've tried passing in an array of string only to be told the best matching overload has invalid arguments. What am I missing?
I updated the title to be more specific.
string keyWords = Request.Form["keyWords"];
conten.SetValue("keyWords", alias);
Umbraco version 7.13.1 assembly: 1.0.6949.28284
That didn't work. I tried passing in alias as a
System.Collections.Specialized.NameValueCollection
but it gave me an error saying "The best overloaded method match for 'Umbraco.Core.Models.ContentBase.SetPropertyValue(string, string)' has some invalid arguments". You did get me to thinking though. I tried passing the second argument in as an object and it still gives me the same error. If you look at the documentation, there are more than 5 overloads for the setValue method.I'm a bit concerned that passing in an object that is still using the SetValue(string,string) overload.
Hi Bryna
SetTags is an extension method on IContentBase
https://github.com/umbraco/Umbraco-CMS/blob/dd6e764588d22ef2b7bce01fd504ece89834f181/src/Umbraco.Core/Models/ContentExtensions.cs#L641
So it should be available for you to use, if you add
at the top of your code file.
Then you should be able to use it to add your tags like so:
see signature here:
https://our.umbraco.com/apidocs/csharp/api/Umbraco.Core.Models.ContentExtensions.html?q=settags#UmbracoCoreModelsContentExtensionsSetTagsUmbracoCoreModelsIContentBaseUmbracoCoreModelsTagCacheStorageTypeSystemStringSystemCollectionsGenericIEnumerableSystemString_SystemBooleanSystemString_
regards
Marc
Thanks. That did the trick nicely.
It appears that the setTags function is treating the tags as csv by default.
The default setting when adding an editor for the document property indicates that json is the default and csv is legacy, so that kind of caught me off guard.
Here's the current documentation link for anyone looking:
https://our.umbraco.com/apidocs/v8/csharp/api/Umbraco.Core.Models.ContentTagsExtensions.html?q=tags
is working on a reply...