More in this section

Forums / Developing with Sitefinity / Create backend menu item example

Create backend menu item example

4 posts, 0 answered
  1. Mike L
    Mike L avatar
    5 posts
    Registered:
    22 Aug 2006
    09 Mar 2012
    Link to this post
    Hi,

    Is there a example on creating a new menu node in the backend page from the module installation? Perhaps using the new fluent api? 

    thanks
  2. Stefani Tacheva
    Stefani Tacheva avatar
    718 posts
    Registered:
    31 May 2017
    09 Mar 2012
    Link to this post
    Hello,

    I took the source code from the Sitefinity documentation and made some changes. It is published here. The new page will be created under the parent backend page. I have marked the important changes I have made. The page is rendered as link and if you set the
    variable to false it will appear as Grouping page in the Backend Menu.

    protected void Page_Load(object sender, EventArgs e)
           {
               using (var fluent = App.WorkWith())
               {
    //Get the parent backend page the new page will be created under.
    //Alternatively you can pass Guid.Empty and it will be created in the root.
                   var myPage = fluent.Pages().LocatedIn(Telerik.Sitefinity.Fluent.Pages.PageLocation.Backend).Where(p => p.Title == "Content").Get().SingleOrDefault().Id;
                   CreatePage(Guid.NewGuid(), "MyNewBackendLinkPage", myPage);
               }
           }
     
           public void CreatePage(Guid pageId, string pageName, Guid parentPageNodeId, bool renderAsLink = true)
           {
               var pageDataId = Guid.NewGuid();
     
               PageManager manager = PageManager.GetManager();
               PageData pageData = null;
               PageNode pageNode = null;
     
               // Get the parent node Id
               if (parentPageNodeId == Guid.Empty)
               {
                   parentPageNodeId = SiteInitializer.BackendRootNodeId;
               }
     
               PageNode parent = manager.GetPageNode(parentPageNodeId);
     
               // Check whether exists
               var initialPageNode = manager.GetPageNodes().Where(n => n.Id == pageId).SingleOrDefault();
     
               if (initialPageNode != null)
               {
                   return;
               }
     
               // Create the page
               pageData = manager.CreatePageData(pageDataId);
               pageData.HtmlTitle = pageName;
               pageData.Title = pageName;
               pageData.Description = pageName;
               pageData.Culture = Thread.CurrentThread.CurrentCulture.ToString();
               pageData.UiCulture = Thread.CurrentThread.CurrentUICulture.ToString();
     
               pageNode = manager.CreatePage(parent, pageId, NodeType.Standard);
               pageNode.Page = pageData;
               pageNode.Name = pageName;
               pageNode.Description = pageName;
               pageNode.Title = pageName;
               pageNode.UrlName = Regex.Replace(pageName.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
               pageNode.ShowInNavigation = true;
               pageNode.DateCreated = DateTime.UtcNow;
               pageNode.LastModified = DateTime.UtcNow;
     
               //if set to false it will appear as Grouping page in the Backend Menu
               pageNode.RenderAsLink = renderAsLink;
     
               manager.SaveChanges();
     
               // Publish
               var bag = new Dictionary<string, string>();
               bag.Add("ContentType", typeof(PageNode).FullName);
               WorkflowManager.MessageWorkflow(pageId, typeof(PageNode), null, "Publish", false, bag);
           }


    Greetings,
    Stefani Tacheva
    the Telerik team
    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
  3. Mike L
    Mike L avatar
    5 posts
    Registered:
    22 Aug 2006
    09 Mar 2012
    Link to this post
    I modified it a bit using the fluent api example in the post, and it's working now. 

    just curious if there is a way to have the pagenode show up at the top even if there is no child pagenode?

    // create the page
                    App.WorkWith().Page()
                        .CreateNewPageGroup(parentPageNodeId, pageId)
                        .Do(p =>
                                {
                                    p.Title = pageName;
                                    p.Description = pageName;
                                    p.Title = pageName;
                                    p.UrlName = Regex.Replace(pageName.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
                                    p.ShowInNavigation = true;
                                    p.DateCreated = DateTime.UtcNow;
                                    p.LastModified = DateTime.UtcNow;
                                    p.RenderAsLink = true;
                                    p.Ordinal = -0.55f;
                                }).SaveChanges();

  4. Stefani Tacheva
    Stefani Tacheva avatar
    718 posts
    Registered:
    31 May 2017
    12 Mar 2012
    Link to this post
    Hello,

    Group page doesn't have a content and it shows the content of its first child page. If you want the page node to show up at the top even, use the source code below.
    protected void Page_Load(object sender, EventArgs e)
            {
                //var pageId = Guid.NewGuid();
                var sfBackendPageId = App.WorkWith().Pages().LocatedIn(Telerik.Sitefinity.Fluent.Pages.PageLocation.Backend).Where(p => p.Title == "Sitefinity").Get().SingleOrDefault().Id;
                CreateNewBackendPage(sfBackendPageId, "TestPageStandart");
            }
     
            public void CreateNewBackendPage(Guid pageId, string pageName)
            {
                var myPageID = Guid.NewGuid();
     
                // if you create a group page use CreateNewGroupPage
                App.WorkWith().Page()
                       .CreateNewStandardPage(pageId, myPageID)
                       .Do(p =>
                       {
                           p.Title = pageName;
                           p.Description = pageName;
                           p.Title = pageName;
                           p.UrlName = Regex.Replace(pageName.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
                           p.ShowInNavigation = true;
                           p.DateCreated = DateTime.UtcNow;
                           p.LastModified = DateTime.UtcNow;
     
                          //set PageData properties:
                          p.Page.Title = pageName;
                          p.Page.Description = pageName;
                          p.Page.Title = pageName;
                          p.Page.UrlName = Regex.Replace(pageName.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
                          p.Page.DateCreated = DateTime.UtcNow;
                          p.Page.LastModified = DateTime.UtcNow;
                       }).SaveChanges();
     
                // publish the standart page
                var bag = new Dictionary<string, string>();
                bag.Add("ContentType", typeof(PageNode).FullName);
                WorkflowManager.MessageWorkflow(myPageID, typeof(PageNode), null, "Publish", false, bag);
     
            }

    You can see the new page on this image.

    Kind regards,
    Stefani Tacheva
    the Telerik team
    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
4 posts, 0 answered