Copied to clipboard

Flag this post as spam?

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


  • Bryna 32 posts 165 karma points
    Jan 24, 2019 @ 22:11
    Bryna
    0

    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:

    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?

    I updated the title to be more specific.

  • Jonas Nikolajsen 17 posts 103 karma points
    Jan 25, 2019 @ 07:37
    Jonas Nikolajsen
    0

    string keyWords = Request.Form["keyWords"];

    conten.SetValue("keyWords", alias);

  • Bryna 32 posts 165 karma points
    Jan 25, 2019 @ 15:27
    Bryna
    0

    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.

        IContent content= contentService.GetById(nodeId);
        object theTags = new object[]  { "Test1", "Tag4" };
        content.SetValue("keyWords", (object) theTags);
    

    I'm a bit concerned that passing in an object that is still using the SetValue(string,string) overload.

  • Marc Goodson 811 posts 5327 karma points MVP 3x c-trib
    Jan 26, 2019 @ 13:01
    Marc Goodson
    102

    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

    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);
    

    see signature here:

    https://our.umbraco.com/apidocs/csharp/api/Umbraco.Core.Models.ContentExtensions.html?q=settags#UmbracoCoreModelsContentExtensionsSetTagsUmbracoCoreModelsIContentBaseUmbracoCoreModelsTagCacheStorageTypeSystemStringSystemCollectionsGenericIEnumerableSystemString_SystemBooleanSystemString_

    regards

    Marc

  • Bryna 32 posts 165 karma points
    Jan 28, 2019 @ 19:21
    Bryna
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft