Try our conversational search powered by Generative AI!

Category and blocks

Vote:
 

I am creating blocks programmatically. But block contents getting created without category. How to set category to a block by code ? 


Below is how my code look like

var sharedBlock = contentrepository.GetDefault(new ContentReference(FolderId));
sharedBlock.Name ="Name";
sharedBlock.Title ="Title";

var contentCat = sharedBlock as ICategorizable;
contentCat.Category = new CategoryList(categoryIds.AsEnumerable());

//Since this is a shared block instance I can cast it to IContent
var content = contentCat as IContent;
content.Name ="BlockName";

var savedReference = contentrepository.Save(content, EPiServer.DataAccess.SaveAction.Publish);

#143969
Feb 04, 2016 0:45
Vote:
 

Hi,

Intead of asigning Category property -> contentCat.Category = new CategoryList(categoryIds.AsEnumerable());

try to use Add method to add categories:

var contentCat = sharedBlock as ICategorizable;

// remove this line:
// contentCat.Category = new CategoryList(categoryIds.AsEnumerable());

foreach(var id in categoryIds)
{
  contentCat.Category.Add(id);
}
#143971
Feb 04, 2016 8:03
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.