Search code examples
asp.net.netumbraco

Is there a way to programmatically add authors/categories to a blog post in Umbraco 8?


Basically I am importing blogs from previous site which have authors/categories for each blog post. I am able to programmatically import the blogs with their titles/post dates, and used a BlockList for the body content. We are using a USN theme.

I am now trying to use the documentation in Umbraco 8 to add authors and categories to the posts.

When trying to add authors to a blog post I am using the Member Picker documentation which looks like this -

var guid = Guid.Parse("1l09dfbd-d391-4f07-bfd4-284fcefc99d2"); // Create a variable for the GUID of the page you want to update
var content = contentService.GetById(guid); // ID of your page - Get the page using the GUID you've defined
var authorId = Guid.Parse("a284efc4-e08f-4f7b-a83b-0a4b7m80b45b");
content.SetValue("postAuthor", authorId); 

I am able to build without any errors. When going to the blog post with the author added, I noticed it does not add the author. If I then try to manually add the author I get an exception -

"An error occurred Multiple actions were found that match the request: GetByIds on type Umbraco.Web.Editors.EntityController GetByIds on type Umbraco.Web.Editors.EntityController GetByIds on type Umbraco.Web.Editors.EntityController

Exception Details System.InvalidOperationException: Multiple actions were found that match the request: GetByIds on type Umbraco.Web.Editors.EntityController GetByIds on type Umbraco.Web.Editors.EntityController GetByIds on type Umbraco.Web.Editors.EntityController"

Am I going about adding the authors the right way? I have not been able to find anything about this error when using the member picker property editor.

I have also tried adding categories to the posts and I am assuming I need to use Multinode-Treepicker to add them. Am I on the right path?

My file structure looks like this.

Home
+Blog
++Categories
++Authors
++Posts

Thanks for any advice/tips in advance.


Solution

  • Was able to get authors as such

    string somestring = Convert.ToString(contentService.GetByLevel(4).Where(x => x.Name == authorUsername).FirstOrDefault().Key).Replace("-", String.Empty);
    content.SetValue("postAuthor", $"umb://document/{somestring}");