How to add a content block item to dynamically created page
I'd like to add a content block control item to a page I am dynamically creating. How can I do it?
Telerik.Sitefinity.Modules.Pages.PageManager pManager =
new Telerik.Sitefinity.Modules.Pages.PageManager();
//Guid templateGuid = pManager.GetTemplates().First().Id;
var newPageNode = App.WorkWith().Page()
.CreateNewStandardPage(PageLocation.Frontend)
.Do(p =>
p.Name = "name";
p.Title = "title";
p.UrlName = "urlName";
p.ShowInNavigation = true;
p.LastModified = DateTime.UtcNow;
p.DateCreated = DateTime.UtcNow;
p.Page.Title = "name";
//set title which appears in page header
p.Page.HtmlTitle = "title";
p.Description = "Some description";
)
.CheckOut()
//.SetTemplateTo(templateGuid)
.Publish()
.SaveChanges();
SiteMapBase.Cache.Flush();
Hi,
First you need to set up your template which will be used for the pages that are going to be created. You can either use one of the built in templates or your custom template. For your example I have created a template through the UI called 'test'. Then after you create the page you need to to assign a placeholder for your content block which has been previously initialized. Please refer to the attached code sample, which demonstrates the wanted behaviour.
Kind regards,
Victor Velev
the Telerik team
Hi Victor,
Thanks for assisting me, I am new to the Sitefinity API, so your explanation is helpful. I tried running the test code you provided, but I am running into the following run time error
Object reference not set to an instance of an object.
Line 35: var pageTemplate = pManager.GetTemplates().Where(tm => tm.Title == "test").First();
Line 36: Guid templateGuid = pageTemplate.Id;
Line 37: PageNode newPageNode = App.WorkWith().Page()
Line 38: .CreateNewStandardPage(PageLocation.Frontend)
Line 39: .Do(p =>
Can you help me determine the cause of the error?
Hello,
Can you tell me if you have created a custom page template called 'test' ? Are you trying to implement this functionality using new project? Please share with me the steps you took when installing the code to verify the procedure is correct.
Regards,
Victor Velev
the Telerik team