How do I edit-save-publish a page template programmatically?

Posted by Community Admin on 04-Aug-2018 16:23

How do I edit-save-publish a page template programmatically???

All Replies

Posted by Community Admin on 24-Jan-2013 00:00

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);
       

I tried this, but it fails:
    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);
    

Any ideas?

Posted by Community Admin on 24-Jan-2013 00:00

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();


Regards,
Craig Iredale
the Telerik team

Posted by Community Admin on 25-Jan-2013 00:00

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.  ;-)

Posted by Community Admin on 25-Jan-2013 00:00

Hi Marko,

Glad that it works.  Your suggestion is good.  I will tell the documentation team to add information about creating and editing  page templates to the API documentation.

Regards,
Craig Iredale
the Telerik team

Posted by Community Admin on 28-Nov-2013 00:00

where to write all this code?

Posted by Community Admin on 29-Nov-2013 00:00

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

Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

This thread is closed