AI OnAI Off
ok this thread helped me also... i needed to initialize the contentarea first... strange if you ask me...
http://world.episerver.com/Forum/Developer-forum/-EPiServer-75-CMS/Thread-Container/2014/5/Is-there-anyway-to-predefine-what-blocks-should-appear-in-a-page/
it works now.
Hi, Vishal,
ContentArea is null, you need to initialize it before adding the first item:
if (clone.Courses == null) { clone.Courses = new ContentArea(); }
then add Items to it:
clone.Courses.Items.Add(new ContentAreaItem { ContentLink = ((IContent)courseBlock).ContentLink });
BR,
Marija
yes... thanks marija... i figured that out also during further investigation.. thanks for your reply
Hey guys,
i'm having a weird problem. I'm creating a importer, which programmatically created some pages and within such a page also programmatically create a shared block which i then need to add to a contentarea on that page.
Now some code. First my pageType and my BlockType.
i first programmatically create my periodePage and my courseBlock. (note that i have helpers to create those, but are not important here). My page and blocks are created without problem. I find the block in the 'For this Page' folder of the corresponding periodPage. That's good.
Now where everything goes wrong. I try adding the courseBlock to the Courses contentarea of the periodPage like so.
Now here i get a "Object reference not set to an instance of an object."
I found that the clone.Courses is always NULL upon, so clone.Courses.Items fails. Why the hell is this happening :-)
Now if i manually go to the page in the cms, and manually add the block to the contentarea and publish the page, and run this code again, then it works perfectly and i have 2 blocks added (1 manually and 1 programmatically). So it works.
Now when i remove all blocks from the contentarea, and run the above code, it gives me the 'Object reference not set to an instance of an object.' again.
So if there are no items it fails.. WHY O WHY??
Am i missing something.. I'm on the latest EPiServer 8.