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);
}
}
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?
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?
BTW I'm really liking Umbraco 7.10! Well done =)
What datatype are you using for categories?
Its called Blog Category Page (alias: USNBlogCategoryPage).
The structure is:
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?
Umbraco does't store IPublishedContent for a multinode treepicker but a comma seperated list of GuidUdi's. For example:
To get this string you can do something like:
And than this should work:
Please let me know if this works.
Ohhhh man you don't exist!
Thank you so much, it worked like a charm =)
No problem. Glad I can give back to the community ;P
is working on a reply...