Programmatically adding blocks to a contentarea - ERROR

Vote:
 

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.

[ContentType(DisplayName = "Periode Folder", GUID = "4149B2DF-C531-4D42-89FA-6A2BE01DB162", Description = "")]   
    [AvailableContentTypes(Availability.None)] // allow no pages 
    public class TimelinePeriodFolder : SiteFolder
    {
        [Display(
           GroupName = SystemTabNames.Content,
           Name = "Vakken",
           Description = "",
           Order = 10)]
        [CultureSpecific]
        [AllowedTypes(new[] { typeof(CourseBlock) })]
        public virtual ContentArea Courses { get; set; }   
    }
[ContentType(DisplayName = "Course", GUID = "5D276769-537B-4D93-86AE-75ECB3A2502C", Description = "Course block")]
    public class CourseBlock : SiteBlockData
    {
        [CultureSpecific]
        [Display(
            Name = "Titel",
            Description = "",
            GroupName = SystemTabNames.Content,
            Order = 2)]        
        public virtual string Title { get; set; }
        
        [CultureSpecific]
        [Display(
            Name = "Leerlijn",
            Description = "",
            GroupName = SystemTabNames.Content,
            Order = 4)]
        [Required]
        [SelectOne(SelectionFactoryType = typeof(EnumMultipleSelectionFactory))]
        public virtual string Learning { get; set; }

        [Display(GroupName = SystemTabNames.Content,
         Name = "Aantal EC",
         Description = "Geef het aantal te verdienen EC op",
         Order = 6)]
        [CultureSpecific]
        public virtual int EC { get; set; }

        [CultureSpecific]
        [Display(
            Name = "Introductie",
            Description = "",
            GroupName = SystemTabNames.Content,
            Order = 6)]   
        [UIHint(UIHint.Textarea)]     
        public virtual string Intro { get; set; }

        [CultureSpecific]
        [Display(
            Name = "Beschrijving",
            Description = "",
            GroupName = SystemTabNames.Content,
            Order = 19)]
        public virtual XhtmlString Body { get; set; }
                
    }


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.

var periodPage = GetOrCreateContainerPage(parentPage.ContentLink, "some name");

var courseBlock = GetOrCreateBlock(periodPage.ContentLink, " block name");

Now where everything goes wrong. I try adding the courseBlock to the Courses contentarea of the periodPage like so.

//fill contentarea
                    var clone = periodPage.CreateWritableClone() as TimelinePeriodFolder;
                    // add new block to the target block content area
                    clone.Courses.Items.Add(new ContentAreaItem
                    {                               
                        ContentLink = ((IContent)courseBlock).ContentLink
                    });
                    
                    _contentRepository.Save(clone, SaveAction.Publish, AccessLevel.NoAccess);

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.

#123166
Jun 25, 2015 17:56
Vote:
 

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.

#123185
Jun 26, 2015 9:18
Vote:
 

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

#123186
Edited, Jun 26, 2015 9:20
Vote:
 

yes... thanks marija... i figured that out also during further investigation.. thanks for your reply

#123187
Jun 26, 2015 9:46
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* 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.