Can't add contentblock to specified placeholder.
I want to create new page from a template and add contentblock at runtime.
TemplateA -> It has 10 placeholder. There are some Sitefinity content in 8 placeholder. 2 placeholder are empty. This two empty placeholder will be filled on the newly created page.
PageA -> It's new page and its parent is TemplateA
The code below creates new page from TemplateA and adds two ContentBlock to page. But not in specified placeholder. I looked at debug and can't see that two empty placeholder. Newly created page has 8 placeholder.
So , contentblock has been added random place at the new page.
01.
public
void
CreateSubPageTr(
string
pageName,
string
pageContentHeader,
string
pageContent)
02.
03.
PageTemplate pgTempalate=
null
;
04.
string
UrlNameCharsToReplace = @
"[^\w\-\!\$\'\(\)\=\@\d_]+"
;
05.
string
UrlNameReplaceString =
"-"
;
06.
Guid pageId = Guid.NewGuid();
07.
Guid pageDataId = Guid.NewGuid();
08.
09.
PageManager manager = PageManager.GetManager();
10.
manager.Provider.SuppressSecurityChecks =
true
;
11.
12.
PageData pageData = manager.CreatePageData(pageDataId);
13.
IQueryable<PageTemplate> allTemplates = manager.GetTemplates();
14.
15.
foreach
(PageTemplate item
in
allTemplates)
16.
17.
if
(!
string
.IsNullOrEmpty(item.Title.Value) && item.Title.Value.CompareTo(
"TemplateA"
) == 0)
18.
19.
pgTempalate = item;
20.
21.
22.
23.
pageData.Template = pgTempalate;
24.
pageData.HtmlTitle = pageName;
25.
pageData.Title = pageName;
26.
pageData.Culture =
"tr"
;
27.
pageData.IsAutoCreated =
true
;
28.
pageData.LocalizationStrategy = Telerik.Sitefinity.Localization.LocalizationStrategy.Split;
29.
30.
31.
ContentBlock cb1 =
new
ContentBlock();
32.
cb1.Html = pageContentHeader;
33.
cb1.ID =
"c001"
;
34.
cb1.Visible =
true
;
35.
36.
ContentBlock cb2 =
new
ContentBlock();
37.
cb2.Html = pageContent;
38.
cb2.ID =
"c002"
;
39.
cb1.Visible =
true
;
40.
41.
PageControl pageControl1 = manager.CreateControl<PageControl>(cb1,
"cphPageTitle"
);
42.
PageControl pageControl2 = manager.CreateControl<PageControl>(cb2,
"cphPageContent"
);
43.
pageData.Controls.Add(pageControl1);
44.
pageData.Controls.Add(pageControl2);
45.
46.
pageData.Visible =
true
;
47.
pageData.Status = ContentLifecycleStatus.Live;
48.
pageData.Owner =
new
Guid(
"9db23d80-8a13-41be-8342-622e867eeaca"
);
49.
pageData.UiCulture =
"tr"
;
50.
pageData.TranslationInitialized =
true
;
51.
52.
53.
PageNode parent = manager.GetPageNode(Telerik.Sitefinity.Abstractions.SiteInitializer.FrontendRootNodeId);
54.
PageNode pageNode = manager.CreatePage(parent, pageId, NodeType.Standard);
55.
56.
pageNode.Page = pageData;
57.
pageNode.Name = pageName;
58.
pageNode.Title = pageName;
59.
pageNode.UrlName = Regex.Replace(pageName.ToLower(), UrlNameCharsToReplace, UrlNameReplaceString);
60.
pageNode.ShowInNavigation =
false
;
61.
pageNode.DateCreated = DateTime.UtcNow;
62.
pageNode.LastModified = DateTime.UtcNow;
63.
64.
manager.RecompileItemUrls<PageNode>(pageNode);
65.
66.
var draft = manager.EditPage(pageDataId,
new
System.Globalization.CultureInfo(
"tr"
));
67.
manager.SavePageDraft(draft,
new
System.Globalization.CultureInfo(
"tr"
));
68.
manager.SaveChanges();
69.
Hello Volkan Demirpence,
First you need to set up your template which will be used for the pages that are going to be created. Y 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,
Is there native code? I got "This item is locked" error with that code. I couldn't debug with fluent code.
Error has been given at this step :
" PageNode newPageNode = App.WorkWith().Page()
.CreateNewStandardPage(PageLocation.Frontend)
.Do(p ..............."
Error has been corrected at previous post.
I've separated the code and debugged it. I used CheckIn() instead of Publish().
But, the same problem continues. ContentBlock is not added to the specified placeholder.
My code is below.
01.
public
PageNode CreateNewFrontendPage(
string
name,
string
title,
string
urlName,
string
placeHolderName, PageTemplate pageTemplate, PageManager pManager)
02.
03.
var contentBlock =
new
ContentBlock();
04.
StandardPageFacade spf = App.WorkWith().Page().CreateNewStandardPage(PageLocation.Frontend);
05.
06.
ControlFacade cfd= spf.Do(p =>
07.
08.
p.Name = name;
09.
p.Title = title;
10.
p.UrlName = urlName;
11.
p.ShowInNavigation =
true
;
12.
p.LastModified = DateTime.UtcNow;
13.
p.DateCreated = DateTime.UtcNow;
14.
p.Page.Title = name;
15.
p.Page.HtmlTitle = title;
16.
p.Description =
""
;
17.
)
18.
.CheckOut()
19.
.Control();
20.
21.
spf = cfd.CreateNew(contentBlock, placeHolderName).Done()
22.
.SetTemplateTo(pageTemplate.Id)
23.
.CheckIn();
24.
spf = spf.SaveAndContinue();
25.
PageNode newPageNode = spf.Get();
26.
return
newPageNode;
27.
Hi Volkan Demirpence,
I have modified my code to match yours, however the behaviour was as expected.
What I have also done is to create a masterpage with a placeholder with an id to the one that is called, and also a page template.
public PageNode CreateNewFrontendPage(string name, string title, string urlName, string PlaceHolderid, string TemplateTitle)
Telerik.Sitefinity.Modules.Pages.PageManager pManager = new Telerik.Sitefinity.Modules.Pages.PageManager();
var contentBlock = new ContentBlock();
var manager = PageManager.GetManager();
var pageTemplate = pManager.GetTemplates().Where(tm => tm.Title == TemplateTitle).First();
Guid templateGuid = pageTemplate.Id;
PageNode 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;
p.Page.HtmlTitle = title;
p.Description = "Some description";
)
.CheckOut()
.Control().CreateNew(contentBlock, PlaceHolderid).Done()
.SetTemplateTo(templateGuid)
.CheckIn()
.SaveAndContinue().Get();
return newPageNode;
Hi Victor,
please, I have some problem with some small modification of this code.
I made me own simple web-control
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="SitefinityWebApp.WebUserControl1" %>
<
asp:Label
ID
=
"Label1"
runat
=
"server"
Text
=
"Label"
></
asp:Label
>
using System;
using System.Diagnostics;
namespace SitefinityWebApp
public partial class WebUserControl1 : System.Web.UI.UserControl
protected void Page_Load(object sender, EventArgs e)
Debug.WriteLine(Label1.Text);
var contentBlock = new ContentBlock();
var contentBlock = new WebUserControl1();
Hi Vrabec,
I have answered you in the support ticket you have opened.
Kind regards,
Victor Velev
the Telerik team