Copied to clipboard

Flag this post as spam?

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


  • sandeep 12 posts 112 karma points
    Sep 02, 2016 @ 01:10
    sandeep
    0

    How to add tags value in tags datatype programmatically

    I want to add tags in tags data type using code. I can add tags manually. Can you help me with addtags section of code?

    1. I have a checkbox(true/false) if checkbox is true then add tag

      if (CurrentPage.GetPropertyValue("stickyPost") == true)// this is a check box
      {
          string[] tags = CurrentPage.articleCategory.ToString().Split(',');
          if (!tags.Contains("StickyPost"))// this is checking if StickyPost tag is already present in articleCategory section 
          {
      
      
      
         //addtags in articleCategory
      }
      
      }
  • sandeep 12 posts 112 karma points
    Sep 06, 2016 @ 20:59
    sandeep
    0

    can anyone give some kind of hint so I can proceed with something?

  • Ian 178 posts 752 karma points
    Sep 06, 2016 @ 21:39
    Ian
    0

    I havent done it but you could have a look at the umbraco source https://github.com/umbraco/Umbraco-CMS/blob/dev-v7/src/umbraco.editorControls/tags/DataEditor.cs the method contents of interest to you would be within updateoraddtags

  • sandeep 12 posts 112 karma points
    Sep 07, 2016 @ 05:15
    sandeep
    0

    Thanks Ian for the hint but seems these methods are deprecated already. I have found one method though https://github.com/umbraco/Umbraco-CMS/blob/75c2b07ad3a093b5b65b6ebd45697687c062f62a/src/Umbraco.Core/Models/ContentExtensions.cs the method here is SetTags but I am wondering what is the first argument ?

    public static void SetTags(this IContentBase content, string propertyTypeAlias, IEnumerable

    What is the meaning of this IcontentBase Content ?

  • Ian 178 posts 752 karma points
    Sep 07, 2016 @ 07:12
    Ian
    0

    It might be the database representation of your node rather than the ipublished content from the cache. Am icontent item probably also implememts icontentbase, you might try using the content service to get the icontent instance of your node by using its id and using that as your argument. One thing i dont know if in using the method you found whether you have to save the content yourself again using the contenntservice to get your tag to persist. I dont know the context in which you are doing this but for on node it could be ok but slow for lots of content or if you are doing it often.

  • sandeep 12 posts 112 karma points
    Sep 07, 2016 @ 18:48
    sandeep
    100

    Thanks Ian. Finally It's working.

    Purpose: I am trying to implement Stickypost functionality available in wordpress. In backoffice I am providing a checkbox to users if check box is checked then post becomes sticky post. I am adding a tag called StickyPost in a tags field once checkbox is checked.

    Final code:

    ` @using ContentModels = Umbraco.Web.PublishedContentModels; @using Umbraco.Core.Models; @using Umbraco.Core.Services; @{ Layout = null; } @{

    try
    {
        if (CurrentPage.GetPropertyValue("stickyPost") == true)// this is a check box
        {
            string[] tags = CurrentPage.articleCategory.ToString().Split(',');
            IEnumerable<string> a = new List<string>() { "StickyPost" };
            if (!tags.Contains("StickyPost"))// this is checking if StickyPost tag is already present in articleCategory section
            {
                IContent content = ApplicationContext.Current.Services.ContentService.GetById(1126);
                content.SetTags("articleCategory", a, false, "default");
                ApplicationContext.Current.Services.ContentService.Save(content);
    
            }
    
        }
    }
    catch (Exception)
    {
    
        throw;
    }
        }
    

    `

  • Ian 178 posts 752 karma points
    Sep 07, 2016 @ 19:53
    Ian
    0

    I m curious does the checkbox property on its own not suffice?

  • sandeep 12 posts 112 karma points
    Sep 07, 2016 @ 20:18
    sandeep
    1

    only checkbox will be sufficient. Let me tell you what I am thinking. we have

    News page (top 10 articles will be displayed at a time with pagination for next 10..)
    post 1
    post 2
    post 3
    . . . post 10

    but we have 100s of postsif say article 50 is a sticky post then it should display always on a first page.

    News page
    post 50
    post 1 ...so on

    To achieve this when news page load I can check foreach(post check if check box is checked and show that post first for checkbox is checked and then rest of post) but I am wondering that will slow news page loading.

    so my solution is if checkbox is checked then I am adding a tag "StickyPost" and when news page loads then I am using Tag service to get all the post which has "Sticky post" tag. then rest of the posts which does not have that tag.

    Do you think my logic is correct ?

  • Ian 178 posts 752 karma points
    Sep 07, 2016 @ 21:10
    Ian
    0

    Some wiser people may be able to advise differently but i would have said that if you were intending to display items only with the tag attached then it would be worth taking the approach you have amd using the tag service to reduce you queries. But in your case as you are instead just using the tag as an ordering mechanism it would be worth looking at linq and orderby see this post https://our.umbraco.org/forum/developers/razor/56041-OrderBy-multiple-parameters for some idea. I forget myself what the exact query would be off the top pf my head without doing a test but it would look very similar in structure to the solution put forward there. You might work with descendants not children (but avoid that if you can) and might have a hasvalue check in there somewhere. If you use descendants just be careful about how many nodes you might be recurring through.

  • sandeep 12 posts 112 karma points
    Sep 07, 2016 @ 23:47
    sandeep
    0

    Thanks Ian you really helped me here.

    I tried following linq query and it worked very well.

    @foreach (var item2 in Model.Content.Children.Where("Visible").OrderBy(x => x.GetPropertyValue<bool>("stickyPost") ? 0:1))
    
Please Sign in or register to post replies

Write your reply to:

Draft