Problems With Programmatic Checkin
We have a web site that we've created to import data from a legacy
system in to Sitefinity. This web site worked fine in Sitefinity 4.3. In
Sitefinity 5.0, however, I am receiving a null reference error.
The function in question is as follows:
private static void AddControlToPage(PageManager manager, PageNode page, Control control, string placeHolder,
string caption)
if (page == null) return;
var temp = manager.EditPage(page.Page.Id);
if (temp == null) return;
if (String.IsNullOrEmpty(control.ID))
control.ID = GenerateUniqueControlIdForPage(temp);
var pageControl = manager.CreateControl<PageDraftControl>(control, placeHolder);
pageControl.Caption = caption;
pageControl.SiblingId = GetLastControlInPlaceHolderInPageId(temp, placeHolder);
manager.SetControlDefaultPermissions(pageControl);
temp.Controls.Add(pageControl);
====>manager.PagesLifecycle.CheckIn(temp);
manager.UnlockPage(page.Id, false);
manager.SaveChanges();
The line I have marked above is where it is throwing the exception.
Stack trace is as follows:
at Telerik.Sitefinity.Modules.Pages.Configuration.PagesConfig.OnPropertiesInitialized()
at Telerik.Sitefinity.Configuration.ConfigElement.InitializeProperties()
at Telerik.Sitefinity.Configuration.ConfigSection.InitializeProperties()
at Telerik.Sitefinity.Configuration.ConfigElement.EnsurePropertiesInitialized()
at
Telerik.Sitefinity.Configuration.Data.XmlConfigProvider.GetPropertyDefaultValue(ConfigElement
defaultConfig, ConfigProperty property)
at
Telerik.Sitefinity.Configuration.Data.XmlConfigProvider.GetXmlElement(XmlDocument
xmlDoc, ConfigElement config, ConfigElement defaultConfig, String
tagName, Type definedType, Boolean isPolicy, XmlElementFactory
xmlElementFactory)
at
Telerik.Sitefinity.Configuration.Data.XmlConfigProvider.<>c__DisplayClass1.<SaveElement>b__0(XmlDocument
xmlDoc)
at Telerik.Sitefinity.Configuration.Data.XmlConfigProvider.GetXmlDocument(Func`2 getXmlElement)
at
Telerik.Sitefinity.Configuration.Data.XmlConfigProvider.SaveElement(ConfigElement
config, Func`2 getXmlElement, String relativePath)
at
Telerik.Sitefinity.Configuration.Data.XmlConfigProvider.SaveElement(ConfigElement
config, ConfigElement defaultConfig, Boolean isPolicy)
at Telerik.Sitefinity.Configuration.Data.XmlConfigProvider.SaveSectionInternal(ConfigSection section)
at Telerik.Sitefinity.Configuration.Data.XmlConfigProvider.SaveSection(ConfigSection section)
at Telerik.Sitefinity.Configuration.ConfigManager.SaveSection(ConfigSection section)
at Telerik.Sitefinity.Modules.Pages.PageManager.SetHomePage(Guid pageNodeId)
at Telerik.Sitefinity.Lifecycle.LifecycleDecoratorPages.CheckIn(PageDraft temp, CultureInfo culture, Boolean deleteTemp)
at Apply.Web.Converter.AddControlToPage(PageManager manager, PageNode
page, Control control, String placeHolder, String caption) in
C:\workspace\NNDS\LB\LLSitefinity.Development\EK2Features\LessonBuilderApp\Apply.Web\Converter.cs:line
536
at Apply.Web.Converter.CreateComponentPage(PageManager manager,
PageTemplate template, PageNode lessonNode, LessonBuilderEntities sfctx,
Lesson lesson, Control control, Locality locality, Int32 sortOrder,
String type, String keywords, String description) in
C:\workspace\NNDS\LB\LLSitefinity.Development\EK2Features\LessonBuilderApp\Apply.Web\Converter.cs:line
425
at Apply.Web.Converter.HandleStorybook(PageManager manager,
PageTemplate template, PageNode lessonNode, LessonBuilderEntities sfctx,
Lesson lesson, ExcelRow storyRow, Locality locality, Int32 sortOrder,
String pageTitle) in
C:\workspace\NNDS\LB\LLSitefinity.Development\EK2Features\LessonBuilderApp\Apply.Web\Converter.cs:line
283
at Apply.Web.Converter.CreateFromExcel() in
C:\workspace\NNDS\LB\LLSitefinity.Development\EK2Features\LessonBuilderApp\Apply.Web\Converter.cs:line
139
at Apply.Web.Global.Application_Start(Object sender, EventArgs e) in
C:\workspace\NNDS\LB\LLSitefinity.Development\EK2Features\LessonBuilderApp\Apply.Web\Global.asax.cs:line
26
I've submitted a support ticket, but I'm hoping that more eyes will see it here on the forums and someone might have some ideas.
Thanks,
Chris
Hi Chris,
We have answered you in the support thread that was opened for the issue. If you wish you can share the information from the support ticket with the community.
All the best,
Victor Velev
the Telerik team
Hello Victor/ Chris,
We are also facing teh exact problem as you were facing earlier at this line.
pageManager.PagesLifecycle.CheckIn(temp);
It gives us Null reference exception.
It would be highly appreciable if you can share the fix for this.
Other thing- is this line really necessary while creating programatic pages because without this it works just fine.
Thanks,
Chetan
Hi Chetan,
Please have a look at the following peace of code, which can help you to get your code working as well:
In the Page_Load method of a sample aspx page, place the following code:
protected
void
Page_Load(
object
sender, EventArgs e)
var contentBlock =
new
ContentBlock();
contentBlock.Html=
"Test"
;
var page = PageManager.GetManager().GetPageNode(
new
Guid(
"0f44fa5e-936f-4789-a12c-538ce5e839f0"
));
AddControlToPage(page, contentBlock,
"body"
,
"caption"
);
private
void
AddControlToPage(PageNode page, Control control,
string
placeHolder,
string
caption)
var manager = PageManager.GetManager();
if
(page ==
null
)
return
;
var temp = manager.EditPage(page.Page.Id);
if
(temp ==
null
)
return
;
if
(String.IsNullOrEmpty(control.ID))
control.ID = GenerateUniqueControlIdForPage(temp);
var pageControl = manager.CreateControl<PageDraftControl>(control, placeHolder);
pageControl.Caption = caption;
pageControl.SiblingId = GetLastControlInPlaceHolderInPageId(temp, placeHolder);
manager.SetControlDefaultPermissions(pageControl);
temp.Controls.Add(pageControl);
manager.PagesLifecycle.CheckIn(temp);
page.Page.LockedBy = Guid.Empty;
manager.SaveChanges();
private string GenerateUniqueControlIdForPage(PageDraft pageDraft)
private Guid GetLastControlInPlaceHolderInPageId(PageDraft pageDraft, string placeHolder)