+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Create Custom PageControl - Object reference not set to an Instance of an object

Create Custom PageControl - Object reference not set to an Instance of an object

3 posts, 0 answered
  1. Brian
    Brian avatar
    7 posts
    Registered:
    28 Jan 2014
    07 Mar 2014
    Link to this post

    I am writing a custom web site migration routine to extract the contents of the div tags from one site and then create sitefinity pages dynamically using the PageManager API.

     I've had success automating the ContentBlock PageControl. However, we have a few custom controls that style the content blocks in a special way.  I am using the same technique as the ContentBlock control, however the page designer is rendering the control as "Object reference not set to an Instance of an object" in design mode.  When I click on Edit to view the properties of this control, it looks fine and contains the properties that I set.  When I try to navigate to the page that was created, it just bombs out with a site maintenance message.

     Here is the snippet of code that I am using.  Note, the code doesn't generate any errors, the page that was created programmatically does display an error:

    pageNode.Page.Controls.Add(cphPrimaryContent);
     
    var cphRelatedLinks = manager.CreateControl<PageControl>("~/CustomControls/RelatedLinks/RelatedLinks.ascx", "cphRelatedLinks");
    cphRelatedLinks.ObjectType = typeof(RelatedLinks).FullName;
    cphRelatedLinks.Caption = "Related links";
    cphRelatedLinks.Description = "Related links";
     
    var pcContent = manager.CreateProperty();
    pcContent.Name = "Content";
    pcContent.Value = "<ul><li><a href=\"http://www.google.com\">Related Link</a></li></ul>";
    cphRelatedLinks.Properties.Add(pcContent);
     
    pageNode.Page.Controls.Add(cphRelatedLinks);
     

    This code is modeled after this code which works fine for ContentBlock:

     

    var cphPrimaryContent = manager.CreateControl<PageControl>();
    cphPrimaryContent.ObjectType = typeof(ContentBlock).FullName;
    cphPrimaryContent.PlaceHolder = "cphPrimaryContent";
    cphPrimaryContent.Caption = "Content block";
    cphPrimaryContent.Description = "Content block";
     
    var pcHhtml = manager.CreateProperty();
    pcHhtml.Name = "Html";
    pcHhtml.Value = "<p>awesome</p>";
    cphPrimaryContent.Properties.Add(pcHhtml);
     
    pageNode.Page.Controls.Add(cphPrimaryContent);

     

    I am developing this for a Fortune 500 company with a very limited time/budget for this particular project. A prompt reply is appreciated.

    Peace,
    Brian

  2. Pavel Donchev
    Pavel Donchev avatar
    28 posts
    Registered:
    06 Dec 2012
    11 Mar 2014
    Link to this post

    Hello , Brian!

    Wild guess number one would be you are missing the placeholder for your control.

  3. Brian
    Brian avatar
    7 posts
    Registered:
    28 Jan 2014
    11 Mar 2014 in reply to Pavel Donchev
    Link to this post

    CreateControl has a constructor and the place holder (cphRelatedLinks) is being passed in.  I have tried several different combinations of creating the control.  A control always shows up in the place holder (in design mode), but it displays the error message "Object reference not set to an Instance of an object" rather than the content it is supposed to display.  

3 posts, 0 answered