+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / How do I edit-save-publish a page template programmatically???

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

6 posts, 0 answered
  1. Marko
    Marko avatar
    148 posts
    Registered:
    30 Jul 2008
    24 Jan 2013
    Link to this post
    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?
  2. Craig Iredale
    Craig Iredale avatar
    0 posts
    Registered:
    20 Jan 2015
    24 Jan 2013
    Link to this post
    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
  3. Marko
    Marko avatar
    148 posts
    Registered:
    30 Jul 2008
    25 Jan 2013 in reply to Craig Iredale
    Link to this post
    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.  ;-)
  4. Craig Iredale
    Craig Iredale avatar
    0 posts
    Registered:
    20 Jan 2015
    25 Jan 2013
    Link to this post
    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
  5. Mohammad
    Mohammad avatar
    5 posts
    Registered:
    04 Sep 2013
    28 Nov 2013 in reply to Craig Iredale
    Link to this post
    where to write all this code?
  6. Svetoslav Manchev
    Svetoslav Manchev avatar
    735 posts
    Registered:
    29 Nov 2016
    29 Nov 2013
    Link to this post
    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
6 posts, 0 answered