Copied to clipboard

Flag this post as spam?

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


  • Elton Xavier 3 posts 83 karma points
    Jun 25, 2018 @ 10:41
    Elton Xavier
    0

    Is there a way to programmatically add categories to a blog post?

    Hi guys

    I've managed to create blog posts programmatically, I just can't find a way to set its categories. Any ideas how to go about this?

        var articlesPostsNodeId = 1550;
        var blogPostNodeId = 1559; // existing blog post
        var blogPostNode = Umbraco.TypedContent(blogPostNodeId);
        var categories = blogPostNode.GetPropertyValue("postCategories") as IEnumerable<IPublishedContent>; // just to see how existing categories look like
        foreach (var post in blogPosts)
        {
            var newBlog = contentService.CreateContent(post.Title, articlesPostsNodeId, blogPostNode.DocumentTypeAlias, 0);
    
            if (newBlog != null)
            {
                // Set values for the document
                newBlog.SetValue("pageSummary", post.Title.ToNullSafeString());
                newBlog.SetValue("bodyText", post.BodyText.ToNullSafeString());
                newBlog.SetValue("postDate", post.PublishDate.ToNullSafeString());
                newBlog.SetValue("postCategories", categories); // ### This line fails ###
            }
    
            if (contentService != null)
            {
                var result = contentService.SaveAndPublishWithStatus(newBlog, 0, true);
            }
        }
    

    BTW I'm really liking Umbraco 7.10! Well done =)

  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    Jun 25, 2018 @ 14:00
    Frans de Jong
    0

    What datatype are you using for categories?

  • Elton Xavier 3 posts 83 karma points
    Jun 25, 2018 @ 14:22
    Elton Xavier
    0

    Its called Blog Category Page (alias: USNBlogCategoryPage).

    The structure is:

    > Articles (blog landing page)
      > Categories (folder)
        - Accountancy (blog category page)
        - Business (blog category page)
        - etc.
    > Posts (folder)
      > 2018 (folder)
        > June (folder)
          - My post (Standard blog post page)
    

    When I create a blog post, I can add multiple categories to it using an Umbraco.MultiNodeTreePicker2 property that lists the pages in the Categories folder. Does that make any sense?

  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    Jun 25, 2018 @ 14:49
    Frans de Jong
    101

    Umbraco does't store IPublishedContent for a multinode treepicker but a comma seperated list of GuidUdi's. For example:

    umb://document/9c4dffe2201541998576fdf7120c861d,umb://document/cb88aaa910a942fbac7be3e993d493f5,umb://document/e09253c015204aac802390742b6180dc
    

    To get this string you can do something like:

    var categories = blogPostNode.GetPropertyValue("postCategories") as IEnumerable<IPublishedContent>;
    IEnumerable<GuidUdi> udiList = categories.Select(x => new GuidUdi("document", x.GetKey()));
        string udiString = string.Join(",", udiList);
    

    And than this should work:

    newBlog.SetValue("postCategories", udiString);
    

    Please let me know if this works.

  • Elton Xavier 3 posts 83 karma points
    Jun 25, 2018 @ 14:59
    Elton Xavier
    0

    Ohhhh man you don't exist!

    Thank you so much, it worked like a charm =)

  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    Jun 25, 2018 @ 18:20
    Frans de Jong
    2

    No problem. Glad I can give back to the community ;P

Please Sign in or register to post replies

Write your reply to:

Draft