Skip to content

Instantly share code, notes, and snippets.

@timabell
Created March 26, 2012 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save timabell/2205546 to your computer and use it in GitHub Desktop.
Save timabell/2205546 to your computer and use it in GitHub Desktop.
PageDataExtensions for making it easier to work with EpiServer 6.1 pages
using System;
using EPiServer;
using EPiServer.Core;
using EPiServer.DataAccess;
using EPiServer.Security;
using EPiServer.Web.Hosting;
namespace EpiServerShine
{
static internal class PageDataExtensions
{
public static void Rename(this PageData page, string name)
{
// ref for the clone: http://marekblotny.blogspot.co.uk/2008/03/notsupportedexception-property-title-is.html
var writableClone = page.CreateWritableClone();
writableClone.PageName = name;
DataFactory.Instance.Save(writableClone, SaveAction.Publish, AccessLevel.NoAccess);
}
/// <summary>
/// Sets the property of a page to the specified value, checking property exists first.
/// </summary>
/// <param name="page">The page to set the property on.</param>
/// <param name="property">Name of the property.</param>
/// <param name="value">The new value for the property.</param>
/// <exception cref="Exception">Thrown if the property doesn't exist on the page.</exception>
public static void SetProperty(this PageData page, string property, object value)
{
var propertyData = page.Property[property];
if (propertyData == null)
{
throw new Exception(String.Format("Property '{0}' missing for page type '{1}'", property, page.PageTypeName));
}
propertyData.Value = value;
}
/// <summary>
/// Gets a page's page directory, creating it if it's missing (if requested), and allowing a bypass of the access check.
/// </summary>
/// <param name="page">The page to create the page directory for.</param>
/// <param name="createIfNotExist">if set to <c>true</c> [create if not exist].</param>
/// <param name="bypassAccessCheck">if set to <c>true</c> [bypass access check].</param>
/// <returns>The existing or newly created directory, or null if the directory doesn't exist and <see cref="createIfNotExist"/> set to false.</returns>
public static UnifiedDirectory GetPageDirectory(this PageData page, bool createIfNotExist, bool bypassAccessCheck)
{
if (!bypassAccessCheck)
{
// default implementation is sufficient if not bypassing access check
return page.GetPageDirectory(createIfNotExist);
}
// If the caller doesn't want the missing directory created, or the directory already exists then use the standard method.
var existingDirectory = page.GetPageDirectory(false);
if (!createIfNotExist || existingDirectory != null)
{
return existingDirectory;
}
/* Need to create the page by bypassing the access check. The default method provides no method of doing this.
* ref http://blog.fredrikhaglund.se/blog/2008/09/05/page-folders-and-uploading-of-files-in-episerver/
* Figure out what the path would be and create it directly with CreateSubdirectory(), setting the BypassAccessCheck as we go. */
// Get the page's folder ID
var folderId = (int)page.Property["PageFolderID"].Value;
// Get the root directory for all page folders
var pageDirectoryRootVirtualPath = VirtualPathHandler.PageDirectoryRootVirtualPath;
var pageRootDirectory = (VersioningDirectory)GenericHostingEnvironment.VirtualPathProvider.GetDirectory(pageDirectoryRootVirtualPath);
// Bypass security and Create page folder
pageRootDirectory.BypassAccessCheck = true;
return pageRootDirectory.CreateSubdirectory(folderId.ToString(), page);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment