<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Fredrik Haglund's blog &#187; IsMasterLanguageBranch</title>
	<atom:link href="http://blog.fredrikhaglund.se/blog/tag/ismasterlanguagebranch/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.fredrikhaglund.se</link>
	<description>Chatter about EPiServer, ASP.NET, CSS and Web Development.</description>
	<lastBuildDate>Tue, 28 Jun 2011 13:37:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Work with Unpublished PageData from code</title>
		<link>http://blog.fredrikhaglund.se/blog/2009/02/18/work-with-unpublished-pagedata-from-code/</link>
		<comments>http://blog.fredrikhaglund.se/blog/2009/02/18/work-with-unpublished-pagedata-from-code/#comments</comments>
		<pubDate>Wed, 18 Feb 2009 18:01:20 +0000</pubDate>
		<dc:creator>Fredrik Haglund</dc:creator>
				<category><![CDATA[EPiServer]]></category>
		<category><![CDATA[AccessLevel]]></category>
		<category><![CDATA[DataFactory]]></category>
		<category><![CDATA[EPiServer CMS]]></category>
		<category><![CDATA[GetChildren]]></category>
		<category><![CDATA[ILanguageSelector]]></category>
		<category><![CDATA[IsMasterLanguageBranch]]></category>
		<category><![CDATA[LanguageSelector]]></category>
		<category><![CDATA[PageVersion]]></category>
		<category><![CDATA[SaveAction]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[VersionStatus]]></category>

		<guid isPermaLink="false">http://blog.fredrikhaglund.se/blog/2009/02/18/work-with-unpublished-pagedata-from-code/</guid>
		<description><![CDATA[Mari Jørgensen wrote about Breaking change in GetChildren() and I would like to share some of my findings when working with PageData from code when you want to use the built-in flow for publishing. As you might know a Page Version can have VersionStatus Not Ready (CheckedOut), Ready To Publish (CheckedOut), Published and Previously Published. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://labs.episerver.com/en/Blogs/Mari-Jorgensen/">Mari Jørgensen</a> wrote about <a href="http://labs.episerver.com/en/Blogs/Mari-Jorgensen/Dates/2009/2/Breaking-change-in-GetChildren/">Breaking change in GetChildren()</a> and I would like to share some of my findings when working with <em>PageData</em> from code when you want to use the built-in flow for publishing.</p>
<p>As you might know a Page Version can have <em>VersionStatus</em> <strong>Not Ready </strong>(<em>CheckedOut</em>), <strong>Ready To Publish</strong> (<em>CheckedOut</em>), <strong>Published</strong> and <strong>Previously Published</strong>. Since you in almost all cases are only interested in the published version most methods in <em>DataFactory</em> class only returns <em>PageData</em> objects with the published version.</p>
<h3>Get unpublished pages and pages not in current language branch</h3>
<p><em>GetPage()</em> and <em>GetChildren()</em> returns page(s) published in the current language. You always have to use in a <em>ILanguageSelector</em> if you want to get <em>PageData</em> for another Language Branch than the current language branch.</p>
<pre class="csharpcode">PageDataCollection pages =
  DataFactory.Instance.GetChildren(
    CurrentPage.PageLink,  LanguageSelector.AutoDetect(<span class="kwrd">true</span>));</pre>
<p>This will retrieve all children the same way as the Page Tree in the Structure Tab in Edit Mode. If there is no published version in the <strong>Current Content Language</strong> it will return <em>PageData</em> for the Master Langauge Branch, regardless of Publish Status.</p>
<h3>Saving a page without publishing it</h3>
<p>It is easy to create a new page and not publish it. This can be used for moderation where an Editor uses the publish button in Edit mode to approve.</p>
<pre class="csharpcode">PageData page = DataFactory.Instance.GetDefaultPageData(
                  rootpage.PageLink, <span class="str">"My Page Type"</span>);
page.PageName = <span class="str">"New Page"</span>;
DataFactory.Instance.Save(page, SaveAction.CheckIn,
                          AccessLevel.NoAccess);</pre>
<p><em>SaveAction.CheckIn</em> will make you page Ready to Publish.</p>
<h3>Access Rights</h3>
<p>Even if the current your is not an Editor you may give them Edit access rights to their pages.  It is very easy to add access rights for the current user <span style="text-decoration: underline;">after</span> the page is saved. Note that all existing access right on the parent page will be inherited as usual.</p>
<pre class="csharpcode">PageAccessControlList acl = <span class="kwrd">new</span> PageAccessControlList(page.PageLink);
acl.Add(<span class="kwrd">new</span> AccessControlEntry(
          Membership.GetUser().UserName,
          AccessLevel.Read | AccessLevel.Edit | AccessLevel.Create | AccessLevel.Delete,
          SecurityEntityType.User));
acl.Save();</pre>
<p>It is also easy to filter a collection and remove pages you should not be able to change.</p>
<pre class="csharpcode"><span class="kwrd">new</span> FilterAccess(AccessLevel.Edit).Filter(pages);</pre>
<p>Another approach is to show the page but maybe disable the edit button.</p>
<pre class="csharpcode"><span class="kwrd">bool</span> canChange = page.QueryDistinctAccess(AccessLevel.Edit);</pre>
<h3>Page Versions and Unpublished Pages</h3>
<p>Property Values for the Published Version of a Page is stored in different tables in the database than all other versions of the page. You need something called <strong>WorkID</strong> in your <em>PageReference</em> to load other versions of a page than the published version.</p>
<p><strong>WARNING! Last time I checked <em>GetPage()</em> and <em>GetPages()</em> returned skeleton <em>PageData</em> objects, where all user defined properties are null, for unpublished pages if you did not have a <em>WorkID</em>.</strong></p>
<p>This is an example of how you have to use <em>PageVersion</em> class to retrieve a list of all versions of a page. Each <em>PageVersion</em> has a <em>PageReference</em> with both <em>PageID</em> and <em>WorkID</em>. </p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">static</span> PageData GetLastVersion(PageReference pageRef)
{
    PageVersionCollection pageVersions = PageVersion.List(pageRef);
    PageReference lastVersion = pageVersions[0].ID;
    <span class="kwrd">foreach</span> (PageVersion pageVersion <span class="kwrd">in</span> pageVersions)
    {
        <span class="kwrd">if</span> (pageVersion.IsMasterLanguageBranch)
        {
            lastVersion = pageVersion.ID;
        }
    }
    <span class="kwrd">return</span> DataFactory.Instance.GetPage(lastVersion,
             LanguageSelector.AutoDetect(<span class="kwrd">true</span>));
}</pre>
<p>When you have a <em>PageReference</em> with <em>WorkID</em> you can use it with <em>GetPage()</em> to retrieve other versions of a Page. Using and a <em>LanguageSelector</em> with fallback to Master Language is required to get around the filter.</p>
<h3>Update a page without creating a new version</h3>
<p>Sometimes you want to change a PageData object without creating a new version. In the example below UpdatePageFromForm copies values from text boxes to the page. If a value has changed it will be saved.</p>
<pre class="csharpcode">page = GetLastVersion(pageRef).CreateWritableClone();
UpdatePageFromForm(page);
<span class="kwrd">if</span> (page.IsModified)
{
    SaveAction saveAction = SaveAction.CheckIn;
    <span class="kwrd">if</span> (page.Status != VersionStatus.Published)
    {
        <span class="rem">// Update existing version if it is not published</span>
        saveAction = saveAction | SaveAction.ForceCurrentVersion;
    }
    DataFactory.Instance.Save(page, saveAction);
}</pre>
<p>That&#8217;s all for now folks!</p>
<p>Please, leave a comment if you learned something. It is good for my blogging morale to know that someone got helped&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.fredrikhaglund.se/blog/2009/02/18/work-with-unpublished-pagedata-from-code/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>

