AI OnAI Off
Use GetDefault to create the block. Cast it to IContent when saving.
var myBlock = repository.GetDefault<MyBlockType>(parentReference); ((IContent) myBlock).Name = "Wasabi and stuff"; repository.Save(myBlock as IContent, EPiServer.DataAccess.SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess);
If you want to get the page's folder reference as the parentRefence, you can do:
var contentAssetHelper = ServiceLocator.Current.GetInstance<contentassethelper>(); ContentAssetFolder folder = contentAssetHelper.GetOrCreateAssetFolder(myPage.ContentLink);
and then use the folder's contentLink property
If you're wondering why BlockData does not implement IContent in the first place, you should check out this blogg: http://world.episerver.com/Blogs/Johan-Bjornfot/Dates1/2012/11/Shared-blocks--IContent/
Thanks, I thought it was so simple but my friday afternoon brain did not figure it out.
I am feeling a litte stupid now, but I cannot figure out how to create a instance of a blockdata and add it to a contentarea by code.
There are no problems adding blockdata to the content area but I have problem with how to create my new instance of my blockdata and save it to the database.
When I request the once existing there are all IContent, but when trying to create a new I get this error that it is not inheriting from IContent.
I define the blockdata like this:
Then I try to create a new instance of this like this:
Propably I am not supposed to use IContentRepository but I can not figure out what to use
Please help my empty brain this late in the week