Save page to database
Namespace:
EPiServerAssembly: EPiServer (in EPiServer.dll)
Version: 5.2.375.7
Syntax
| C# |
|---|
public PageReference Save( PageData page, SaveAction action ) |
Parameters
- page
- Type: EPiServer.Core..::.PageData
A initalized PageData object containing data to be saved
- action
- Type: EPiServer.DataAccess..::.SaveAction
Action that will be performed
Remarks
Note: The action parameter is a SaveAction enumeration that is located in a non-documented part of the SDK. You can however use it as it is a part of the EPiServer API and available from code.
If PageLink is empty a new page will be created
Access right required:
- If SaveAction.Save: if it is new page AccessLevel.Create, if it is new version of the page AccessLevel.Create | AccessLevel.Edit
- If SaveAction.CheckIn: if it is new page and check in is required AccessLevel.Edit, otherwise SaveAction.Save applies
- If SaveAction.Publish: if pubblishing is required access level is AccessLevel.Publish, otherwise SaveAction.Save case applies
Examples
The following example shows how to programmatically create a page.
CopyMarkup
CopyC#
<%@ Page Language="c#" Codebehind="createPage.aspx.cs" AutoEventWireup="false" Inherits="development.createPage" EnableViewState="False" EnableSessionState="True" %> <%@ Register TagPrefix="EPiServer" Namespace="EPiServer.WebControls" Assembly="EPiServer" %> <html> <head> <title>Creates a page programatically</title> </head> <body> <form id="createPage" method="post" runat="server"> <input type="hidden" id="selectedPageLink" name="selectedPageLink" runat="server"> <input type="hidden" id="selectedPageName" name="selectedPageName"> <b>Where do you want to create the new page:</b><br> <EPiServer:PageDataSource runat="server" ID="Pages" PageLink="<%#EPiServer.Core.PageReference.StartPage%>" IncludeRootPage="true" EnableVisibleInMenu="false" /> <asp:TreeView runat="server" ID="PageTree" DataSourceID="Pages" MaxDataBindDepth="5" ShowExpandCollapse="true" ExpandDepth="0" SelectedNodeStyle-BackColor="lightgray" OnSelectedNodeChanged="PageTreeSelect"> <DataBindings> <asp:TreeNodeBinding SelectAction="SelectExpand" TextField="PageName" ValueField="PageLink" /> </DataBindings> </asp:TreeView> <br> Parent id:<br /> <asp:TextBox ID="parentId" runat="server" MaxLength="3"></asp:TextBox> <br /> Name:<br /> <asp:TextBox ID="txtName" runat="server"></asp:TextBox> <br /> <asp:Button ID="cmdCreatePage" runat="server" Text="Create Page"></asp:Button> </form> </body> </html>
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using EPiServer; using EPiServer.Core; using EPiServer.DataAbstraction; namespace development { /// <summary> /// Summary description for createPage this page should be located in some directory . /// </summary> public class createPage : EPiServer.SimplePage { protected System.Web.UI.WebControls.TreeView PageTree; protected System.Web.UI.WebControls.Button cmdCreatePage; protected System.Web.UI.WebControls.TextBox txtName; protected System.Web.UI.WebControls.TextBox parentId; private void cmdCreatePage_Click(object sender, System.EventArgs e) { PageType pageType = PageType.Load("[Public] Standard page"); int id = PageReference.StartPage.ID; Int32.TryParse(parentId.Text, out id); PageReference parent = new PageReference(id); // You can find the ID of the page type by hoovering the mouse // over the page type link in admin mode. PageData newPage = DataFactory.Instance.GetDefaultPageData(parent, pageType.ID); newPage.PageName = txtName.Text; ((PropertyLongString)newPage.Property["MainBody"]).Value = "This text was <font color=red>autogenerated</font>."; // Save. Note the EPiServer.DataAccess.SaveAction enum is not documented DataFactory.Instance.Save(newPage, EPiServer.DataAccess.SaveAction.Publish); newPage.LinkURL = UriSupport.BuildUrlWithPageReference(newPage.LinkURL, newPage.PageLink); Response.Write("Saved new page with the page identity = '" + newPage.PageLink.ID + "'<br>"); Response.Write("The link to the new page is: <a href=\"" + newPage.LinkURL + "\">" + newPage.LinkURL + "</a>"); } protected void PageTreeSelect(object sender, EventArgs e) { parentId.Text = PageTree.SelectedNode.Value; } private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here DataBind(); } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.Load += new EventHandler(this.Page_Load); this.cmdCreatePage.Click += new EventHandler(this.cmdCreatePage_Click); } #endregion } }
Examples
The following example shows how to edit and save en existing page.
CopyC#
PageData data; DataFactory factory = EPiServer.DataFactory.Instance; data = factory.GetPage(EPiServer.Core.PageReference.StartPage); // This will add todays date after the name data.PageName += " " + DateTime.Now.ToShortDateString(); // Save and publish factory.Save(data, EPiServer.DataAccess.SaveAction.Publish);
Exceptions
| Exception | Condition |
|---|---|
| EPiServer.Core..::.AccessDeniedException | Thrown when user doesn't have enough access rights to perform this action |