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

Forums / Developing with Sitefinity / Creating Pages Programmatically!!!

Creating Pages Programmatically!!!

15 posts, 2 answered
  1. Daniel
    Daniel avatar
    62 posts
    Registered:
    20 Apr 2009
    05 Mar 2010
    Link to this post
    Hi Support,

    I am attempting to create some pages for sitefinity through VB.NET code - I have read the creating pages API and translate it as best I can however I am getting an error on line 1

    1.Dim cms As New CmsManager
    2.Dim page As ICmsPage = cms.CreatePage("Test Page")
    3.Dim template As IPageTemplate = cms.GetTemplate("Test Master")
    4.page.Template = template
    5.cms.SavePage(page)
    The error is Object Reference not set to an instance of an object.

    Thanks in advance

    Dan
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    05 Mar 2010
    Link to this post
    Hello Daniel,

    Try using the code shown below

    Dim cms As New CmsManager()
    Dim page As ICmsPage = cms.CreatePage("DeleteThisPage")
    Dim template As IPageTemplate = cms.GetTemplate("Default")
    page.Staged.SetTemplate(template.ID, "Levels")
    cms.SavePage(page)


    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. Daniel
    Daniel avatar
    62 posts
    Registered:
    20 Apr 2009
    05 Mar 2010
    Link to this post
    Hi Ivan

    That code still produces the same error - the error is on the Dim CMS as New CMSManager Line

    Thanks for your prompt reply!

    Dan
  4. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    05 Mar 2010
    Link to this post
    Hello Daniel,

    Attached is a sample of how to instantiate CmsManager in VB.NET. The attached code instantiates the cms manager the same way as in Ivan's post. Inspect your code for other issues.

    Regards,
    Radoslav Georgiev
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  5. Daniel
    Daniel avatar
    62 posts
    Registered:
    20 Apr 2009
    05 Mar 2010
    Link to this post
    Hi Radoslav,

    I have looked at the attatched items and it was only an empty aspx file no vb.net!

    Below is the entirity of my code: Below is the entirity of my code:
    01.Option Explicit On
    02.Option Strict On
    03. 
    04.Imports Telerik.Cms
    05. 
    06.Public Class Form1
    07.    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    08.        Try
    09.            Dim cms As New CmsManager
    10.            Dim page As ICmsPage = cms.CreatePage("Test Page")
    11.            Dim template As IPageTemplate = cms.GetTemplate("Test Master")
    12.            page.Template = template
    13.            cms.SavePage(page)
    14.        Catch ex As Exception
    15.            MsgBox(ex.Message)
    16.        End Try
    17.    End Sub
    18.End Class
  6. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    05 Mar 2010
    Link to this post
    Hello Daniel,

    I am sorry, I have omitted the code file. Attached is the proper page and code. Here is the VB used:

    Partial Class Default2
        Inherits System.Web.UI.Page
     
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim cmsManager As New Telerik.Cms.CmsManager()
            Dim allPages As IList = cmsManager.GetPages()
            For Each Page As Telerik.Cms.ICmsPage In allPages
                Response.Write(Page.Title + "<br />")
            Next
        End Sub
    End Class

    Best wishes,
    Radoslav Georgiev
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  7. Daniel
    Daniel avatar
    62 posts
    Registered:
    20 Apr 2009
    05 Mar 2010
    Link to this post
    Hi,

    I am still not sure if I am doing it write as I am trying to do it from a standalone app - can I point the CMS Manager to a particular instance of Sitefinity?

    Thanks for your percerveriance

    Dan
  8. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    05 Mar 2010
    Link to this post
    Hi Daniel,

    This is puts the the scenario of use in a completely different area. If you wish to use Sitefinity API to pull data from a Sitefinity website in another application you have to work with web services. Your CmsManager must be instantiated in the context of the Sitefinity website. The best way is to create WebServices which will be hosted on your Sitefinity website and expose the the API. Then you can call those web services and they will return data. You can see following samples on our blogs how to use web services.

    In the particular case you have to create a CreatePage web method for your webservice where you will put the code.

    Sincerely yours,
    Radoslav Georgiev
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
    Answered
  9. Gabe Sumner
    Gabe Sumner avatar
    440 posts
    Registered:
    09 Sep 2007
    05 Mar 2010
    Link to this post
    Hey Daniel

    You might take a look at Using WinForms for Sitefinity Content Migration.  I wrote this awhile ago and it roughly covers the scenario you're describing.

    Best of luck,

    Gabe Sumner
    Telerik | Sitefinity CMS
    Answered
  10. Daniel
    Daniel avatar
    62 posts
    Registered:
    20 Apr 2009
    05 Mar 2010
    Link to this post
    Suppose I should have mentioned that a little bit earlier sorry about that!

    Thank you very much for your replys

    Dan
  11. Daniel
    Daniel avatar
    62 posts
    Registered:
    20 Apr 2009
    08 Mar 2010
    Link to this post
    Hi Guys

    First off a big thanks for helping me with the original problem - I have a little tweak I was wondering about though!

    I am looking to put all the templates in the system into a dropdown box so the user can select the template he/she desires however I cant seem to get the template name into a datatable I was wondering if you could help please

    Thanks again

    Daniel

    Below is the code I have so far
    01.Dim MyList As IList = cms.GetTemplates
    02.Dim dsDS As New DataSet
    03.dsDS.Tables.Add("dS")
    04.Dim col As New DataColumn("Col", GetType(String))
    05.dsDS.Tables("dS").Columns.Add(col)
    06.For Each item As Data.CmsTemplateContent In MyList
    07.    Dim row As DataRow = dsDS.Tables("dS").NewRow
    08.    row("Col") = item.Template.Name
    09.    dsDS.Tables("dS").Rows.Add(row)
    10.Next
  12. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    08 Mar 2010
    Link to this post
    Hello Daniel,

    You can use GetTemplates method of CmsManager class.

    var cmsManager = new CmsManager();
    IList alltemplates = cmsManager.GetTemplates();

    You can bind the DropDownList or RadComboBox using alltemplates list.

    Sincerely yours,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  13. Daniel
    Daniel avatar
    62 posts
    Registered:
    20 Apr 2009
    08 Mar 2010
    Link to this post
    Hi Ivan

    I am trying to pull back all the items through an XML Web Service.

    I have code (from a previous project) to convert a Datatable to XML and Pass that back to another function on the client side to convert the xml to a Datatable this datatable is then used as the datasource.

    I unfortunatly cant pass the AllTemplates IList back to the client through a web service is there any other way to accomplish this?

    Thanks

    Dan
  14. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    08 Mar 2010
    Link to this post
    Hello Daniel,

    Since you are using an XML web service you can use DataContractSerializer and serialize the list of templates into XML. You can create a sort of a proxy class and pass only data you need. Bellow is a small sample on how to create this proxy class and pass needed information.

    WebPageTemplate.cs:
    using System;
    using System.Runtime.Serialization;
    using Telerik.Cms;
     
    /// <summary>
    /// Summary description for WebPageTemplate
    /// </summary>
    [DataContract]
    public class WebPageTemplate
    {
        public WebPageTemplate(IPageTemplate pageTemplate)
        {
            this.CmsProviderName = pageTemplate.CmsProvider.Name;
            this.Name = pageTemplate.Name;
            this.ContainsUrlSrvc = pageTemplate.ContainsUrlSrvc;
            this.ID = pageTemplate.ID;
        }
        [DataMember]
        public string Name { get; set; }
     
        [DataMember]
        public string CmsProviderName { get; set; }
     
        [DataMember]
        public bool ContainsUrlSrvc { get; set; }
     
        [DataMember]
        public Guid ID { get; set; }
    }

    Then in your web service you can use something like this: 
    CmsManager cmsManager = new CmsManager();
    var alltemplates = cmsManager.GetTemplates().Cast<IPageTemplate>().Select(t => new WebPageTemplate(t)).ToList();
    DataContractSerializer serializer = new DataContractSerializer(typeof(List<WebPageTemplate>));
    FileStream stream = new FileStream("C:\\sf\\Blank37SP2\\Example.xml", FileMode.OpenOrCreate);
    serializer.WriteObject(stream, alltemplates);

    which will serialize the list of our custom objects to XML. For the sake of simplicity I am writing this to a XML file, however you can use WriteObject method and write to a stream and then pass this stream through your web service.

    Regards,
    Radoslav Georgiev
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  15. Daniel
    Daniel avatar
    62 posts
    Registered:
    20 Apr 2009
    08 Mar 2010
    Link to this post
    Hi

    Thanks for the great suggestions.

    Hopefully that is all for now!!

    Thanks again

    Dan
Register for webinar
15 posts, 2 answered