Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
AI OnAI Off
Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
The following examples show how to create a page programmatically in Episerver CMS and set the MainBody property.
PageReference parent = PageReference.StartPage;
IContentRepository contentRepository = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentRepository>();
IContentTypeRepository contentTypeRepository = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentTypeRepository>();
PageData myPage = contentRepository.GetDefault<PageData>(parent, contentTypeRepository.Load("Standard page").ID);
[ContentType]
public class StandardPage : PageData
{
public virtual string MainBody { get; set; }
}
StandardPage standardPage = contentRepository.GetDefault<StandardPage>(parent);
Note: You can use other overloads of the GetDefaultPageData method.
myPage.PageName = "My new page";
myPage.URLSegment = EPiServer.Web.UrlSegment.CreateUrlSegment(myPage);
The code example below shows how to define the user defined property MainBody:
myPage.Property["MainBody"].Value = "<p>This is produced programmatically.</p>";
The following case achieves the same with a strongly typed model:
standardPage.MainBody = "<p>This is produced programmatically.</p>";
contentRepository.Save(myPage, EPiServer.DataAccess.SaveAction.Publish);
Note: The method call requires that the current user has the proper permissions to publish a page in this location. This causes a problem if the current user is an anonymous user and you still want the page to be published programmatically. You can use another overload of the Save method to permit the publishing, even if the current user does not have the necessary permissions:
contentRepository.Save(myPage, EPiServer.DataAccess.SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess);
Last updated: Sep 21, 2015