以编程方式创建组页面Sitefinity后端

时间:2014-02-24 18:25:32

标签: c# asp.net sitefinity

我搜索过,找不到有关如何在sitefinity API中创建组页面的任何文档。我知道如何以编程方式创建页面。

    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.CurrentFrontendRootNodeId;
    }

    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;

    // Check whether home page
    if (isHomePage)
    {
        SystemManager.CurrentContext.CurrentSite.SetHomePage(pageId);
    }

    manager.SaveChanges();

    // Publish
    var bag = new Dictionary<string, string>();
    bag.Add("ContentType", typeof(PageNode).FullName);
    WorkflowManager.MessageWorkflow(pageId, typeof(PageNode), null, "Publish", false, bag);
}

这会创建一个常规页面。我已经尝试将NodeType从标准更改为NodeType.Group,并保留其余代码。但是,这会引发异常“当前页面不是标准页面”。我找了pageNode属性,使页面成为一个组页面而不是常规页面,但我找不到任何。如果有人知道怎么做,我真的很感激。

4 个答案:

答案 0 :(得分:0)

可能会抛出异常,因为您正在尝试为创建的Group节点创建PageData。我相信Group节点没有页面数据,而只是用于对其他页面节点进行分组。

尝试删除pageData到PageGroup.Page属性的创建和分配,这应该允许您保存创建的页面节点。

此外,如果你安装了sitefinity SDK,那么资源文件夹中有一个SampleUtilities类,它有很多帮助方法来创建页面,模板等,作为使用API​​的一个很好的参考点。

我希望这有用!

答案 1 :(得分:0)

这是一些可能对您有帮助的代码:

// Define our new view
        var templateIconView = new TemplateIconView { ID = TemplateIconViewControlId };

        var pageManager = PageManager.GetManager();
        pageManager.Provider.SuppressSecurityChecks = true;

        // Get the root
        var parentNode = pageManager.GetPageNode(SiteInitializer.SitefinityNodeId);

        // Check whether exists
        var initialPageNode = pageManager.GetPageNodes().SingleOrDefault(n => n.Id == AdditionalToolsPageId);
        if (initialPageNode != null) {

            pageManager.Delete(initialPageNode);
            pageManager.SaveChanges();

        }

        var homePageNode = pageManager.GetPageNodes().SingleOrDefault(n => n.Id == HomePageId);
        if (homePageNode != null) {

            pageManager.Delete(homePageNode);
            pageManager.SaveChanges();
        }

        // Create the Group Page Node
        var groupPageNode = pageManager.CreatePage(parentNode, AdditionalToolsPageId, NodeType.Group);
        groupPageNode.Name = "KonstruiTools";
        groupPageNode.Description.SetString("Konstrui Tools", true);
        groupPageNode.Title.SetString("Konstrui Tools", true);
        groupPageNode.UrlName = Regex.Replace("Konstrui Tools", @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-").ToLower();
        groupPageNode.ShowInNavigation = true;
        groupPageNode.DateCreated = DateTime.UtcNow;
        groupPageNode.LastModified = DateTime.UtcNow;

        pageManager.SaveChanges();

        // Create the actual page PageData
        var pageData = pageManager.CreatePageData(HomePageId);
        pageData.HtmlTitle = "Configure Template Icons";
        pageData.Title = "Configure Template Icons";
        pageData.Description = "Configure Template Icons";
        pageData.Culture = Thread.CurrentThread.CurrentCulture.ToString();
        pageData.UiCulture = Thread.CurrentThread.CurrentUICulture.ToString();
        pageData.EnableViewState = true;
        pageData.EnableViewStateMac = true;

        // Add our control to the page with default permissions
        var control = pageManager.CreateControl<PageControl>(templateIconView, Constants.VALUE_DEFAULT_BACKEND_PLACEHOLDER);
        pageManager.SetControlDefaultPermissions(control);
        pageData.Controls.Add(control);

        // Assign the Default Backend Template
        pageData.Template = pageManager.GetTemplate(SiteInitializer.DefaultBackendTemplateId);

        // Create the actual page PageNode
        var pageNode = pageManager.CreatePage(groupPageNode, HomePageId, NodeType.Standard);
        pageNode.Page = pageData;
        pageNode.Name = "ConfigureTemplateIcons";
        pageNode.Description = "Configure Template Icons";
        pageNode.Title = "Configure Template Icons";
        pageNode.UrlName = Regex.Replace("Configure Template Icons", @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-").ToLower();
        pageNode.ShowInNavigation = true;
        pageNode.DateCreated = DateTime.UtcNow;
        pageNode.LastModified = DateTime.UtcNow;
        pageNode.RenderAsLink = true;

        pageManager.SaveChanges();

        // Publish
        var bag = new Dictionary<string, string>();
        bag.Add("ContentType", typeof(PageNode).FullName);
        WorkflowManager.MessageWorkflow(HomePageId, typeof(PageNode), null, "Publish", false, bag);


        pageManager.Provider.SuppressSecurityChecks = false;

这会在rootlevel的后端内部创建一个grouppage,它会保存另一个带控件的页面。

答案 2 :(得分:0)

这是我最终做的事情。它将在另一个组页面下创建一个组页面,这正是我需要的。

                var pageId = Guid.NewGuid();

                PageManager manager = PageManager.GetManager();

                PageNode pageNode = null;

                //Find the modules section which will serve as the 
                //parent page.
                string pageTitle = ConfigurationManager.AppSettings["ModulesSection"].ToString();

                //Gets the actual Modules page.
                var modulesNode = SitefinityHelper.GetPageNode(pageTitle);

                if (modulesNode.Id == Guid.Empty)
                {
                    modulesNode.Id = SiteInitializer.CurrentFrontendRootNodeId;
                }

                PageNode modulesPage = manager.GetPageNode(modulesNode.Id);

                // Check whether exists
                var initialPageNode = manager.GetPageNodes().Where(n => n.Id ==     
                pageId).SingleOrDefault();

                if (initialPageNode != null)
                {
                    return false;
                }

                //Creates the page under the Modules section as a Group Page.
                pageNode = manager.CreatePage(modulesPage, pageId, NodeType.Group);

                pageNode.Name = newModule.Name;
                pageNode.Description = newModule.Description;
                pageNode.Title = newModule.Name;
                pageNode.UrlName = Regex.Replace(newModule.Name.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
                pageNode.ShowInNavigation = true;
                pageNode.DateCreated = DateTime.UtcNow;
                pageNode.LastModified = DateTime.UtcNow;
                manager.SaveChanges();e here

答案 3 :(得分:0)

您可以使用流畅的API创建群组页面。在流畅的API

中给出了简单的方法
App.WorkWith()
    .Page()
    .CreateNewPageGroup()
    .Do(x => x.Title = "Testpage2")
    .Do(x => x.Name = "Testpage2")
    .Do(x => x.UrlName = "testpage2")
    .SaveChanges();