How do I edit-save-publish a page template programmatically???
This is missing from online documentation. There is info on how to edit-save-publish everything else (pages, lists, blogs, etc.), but nothing on Page Templates.
I've been using this to edit something in PAGES, and it's been working. How do I do it for templates?
var pageManager = PageManager.GetManager();
var pages = App.WorkWith().Pages().LocatedIn(PageLocation.Frontend).Get().ToList();
foreach
(var p
in
pages)
if
(p.NodeType == NodeType.Standard)
var pageData = p.Page;
//check out and get draft
var tempPage = pageManager.EditPage(pageData.Id);
//do something...
//check in and save changes
pageManager.PagesLifecycle.CheckIn(tempPage);
pageManager.SaveChanges();
// Publish
var bag =
new
Dictionary<
string
,
string
>();
bag.Add(
"ContentType"
,
typeof
(PageNode).FullName);
WorkflowManager.MessageWorkflow(pageData.Id,
typeof
(PageNode),
null
,
"Publish"
,
false
, bag);
var pageManager = PageManager.GetManager();
var templates = App.WorkWith().PageTemplates().Where(t => !t.IsBackend).Get().ToList();
foreach
(var t
in
templates)
var tempTemplate = pageManager.EditTemplate(t.Id);
//do something...
pageManager.TemplatesLifecycle.CheckIn(tempTemplate);
pageManager.SaveChanges();
// Publish
var bag =
new
Dictionary<
string
,
string
>();
bag.Add(
"ContentType"
,
typeof
(PageTemplate).FullName);
WorkflowManager.MessageWorkflow(tempTemplate.Id,
typeof
(PageTemplate),
null
,
"Publish"
,
false
, bag);
Hi Marko,
Thank you for contacting support.
Try removing all the lines after the // Publish
comment. Also try replacing the two lines after //do something...
with the following
var master = pageManager.TemplatesLifecycle.CheckOut(tempTemplate);
master = pageManager.TemplatesLifecycle.CheckIn(master);
pageManager.TemplatesLifecycle.Publish(master);
pageManager.SaveChanges();
Thanks. That worked great.
Now, it would be even better if you add information about the API for dealing with page templates to API documentation, so that people who are in need for this don't have to turn to forums for basics. ;-)
where to write all this code?
Hi Mohammad,
You can create a new WebForm in VisualStudio and add the code in the code behind on PageLoad. Then you need to run this form. To do that you need to be log-in in the backend as administrator at the same time.
You could find code examples in Sitefinity documentation on that article.
More information for Page templates is available here.
Regards,
Svetoslav Manchev
Telerik