Page workflow and versioning
List of examples:
Content locking
Checking out a page
// Gets a page named 'On Roasts'
// The page is retrieved from the "MySite" site and in the "en-us" culture
TreeNode page = new DocumentQuery<TreeNode>()
.WhereEquals("DocumentName", "On Roasts")
.OnSite("MySite")
.Culture("en-us")
.TopN(1)
.Published()
.PublishedVersion()
.FirstOrDefault();
// Checks if the page isn't already checked-out
if (!page.IsCheckedOut)
{
// Checks out the page
page.CheckOut();
}
Checking in a page
// Gets a page named 'On Roasts'
// The page is retrieved from the "MySite" site and in the "en-us" culture
TreeNode page = new DocumentQuery<TreeNode>()
.WhereEquals("DocumentName", "On Roasts")
.OnSite("MySite")
.Culture("en-us")
.Published()
.PublishedVersion()
.TopN(1)
.FirstOrDefault();
// Checks if the page is checked-out
if (page.IsCheckedOut)
{
// Checks in the page
page.CheckIn();
}
Undoing a page checkout
// Gets a page named 'On Roasts'
// The page is retrieved from the "MySite" site and in the "en-us" culture
TreeNode page = new DocumentQuery<TreeNode>()
.WhereEquals("DocumentName", "On Roasts")
.OnSite("MySite")
.Culture("en-us")
.Published()
.PublishedVersion()
.TopN(1)
.FirstOrDefault();
// Checks if the page is checked-out
if (page.IsCheckedOut)
{
// Undoes the checkout
page.UndoCheckOut();
}
Updating a page with the use of content locking
// Gets a page named 'On Roasts'
// The page is retrieved from the "MySite" site and in the "en-us" culture
TreeNode page = new DocumentQuery<TreeNode>()
.WhereEquals("DocumentName", "On Roasts")
.OnSite("MySite")
.Culture("en-us")
.Published()
.PublishedVersion()
.TopN(1)
.FirstOrDefault();
// Checks whether the page is already checked-out
if (!page.IsCheckedOut)
{
// Checks out the page
page.CheckOut();
// Sets new values for the 'DocumentName' and 'ArticleTitle' fields
page.DocumentName = "Updated article name";
page.SetValue("ArticleTitle", "Updated article title");
// Updates the page in the database
page.Update();
// Checks in the page
page.CheckIn();
}
Updating multiple pages with the use of content locking
// Retrieves pages of multiple pages types from the "MySite" site in the "en-us" culture
IEnumerable<TreeNode> pages = new MultiDocumentQuery()
.Path("/Articles")
.Types("Custom.Articles", "Custom.NewsBulletin")
.OnSite("MySite")
.Culture("en-us")
.Published()
.PublishedVersion()
.TopN(1)
.WithCoupledColumns();
// Note: When updating pages of multiple page types under workflow or versioning, always retrieve
// the pages with all columns to avoid data loss using the 'WithCoupledColumns' parameterization.
// Optionally, you can specify the page types to retrieve using the 'Types' parameterization.
// Updates the "DocumentName" and "ArticleTitle" fields of each retrieved page
foreach (TreeNode page in pages)
{
// Checks whether the page is already checked-out
if (!page.IsCheckedOut)
{
// Checks out the page
page.CheckOut();
page.DocumentName = "Updated article name";
page.SetValue("ArticleTitle", "Updated article title");
// Updates the page in the database
page.Update();
// Checks in the page
page.CheckIn();
}
}
Page workflow management
Moving a page to a different step
// Gets a page named 'On Roasts'
// The page is retrieved from the "MySite" site and in the "en-us" culture
TreeNode page = new DocumentQuery<TreeNode>()
.WhereEquals("DocumentName", "On Roasts")
.OnSite("MySite")
.Culture("en-us")
.Published()
.PublishedVersion()
.TopN(1)
.FirstOrDefault();
// Gets the page's workflow
WorkflowInfo workflow = page.GetWorkflow();
// Checks if the page uses workflow
if (workflow == null)
{
return;
}
// Based on current workflow step, the page is sent for approval, approved, or published
// For basic workflows that automatically publish changes, archives the page
page.MoveToNextStep();
// If the page is published, rejects the page
page.MoveToPreviousStep();
// Moves the page to the first workflow step
page.MoveToFirstStep();
Moving a page to the Published step of the workflow
// Gets a page named 'On Roasts'
// The page is retrieved from the "MySite" site and in the "en-us" culture
TreeNode page = new DocumentQuery<TreeNode>()
.WhereEquals("DocumentName", "On Roasts")
.OnSite("MySite")
.Culture("en-us")
.Published()
.PublishedVersion()
.TopN(1)
.FirstOrDefault();
// Gets the page's workflow
WorkflowInfo workflow = page.GetWorkflow();
// Checks if the page uses workflow
if (workflow != null)
{
// Publishes the page with a comment. There needs to be only one workflow path to the Published step.
page.Publish("Review finished, publishing page.");
}
Moving a page to the Archived step of the workflow
// Gets a page named 'On Roasts'
// The page is retrieved from the "MySite" site and in the "en-us" culture
TreeNode page = new DocumentQuery<TreeNode>()
.WhereEquals("DocumentName", "On Roasts")
.OnSite("MySite")
.Culture("en-us")
.Published()
.PublishedVersion()
.TopN(1)
.FirstOrDefault();
// Gets the page's workflow
WorkflowInfo workflow = page.GetWorkflow();
// Checks if the page uses workflow
if (workflow != null)
{
// Archives the page with a comment
page.Archive("The page is obsolete, archiving.");
}
Page versioning
Rolling back to the oldest version of a page
// Gets a page named 'On Roasts'
// The page is retrieved from the "MySite" site and in the "en-us" culture
TreeNode page = new DocumentQuery<TreeNode>()
.WhereEquals("DocumentName", "On Roasts")
.OnSite("MySite")
.Culture("en-us")
.Published()
.PublishedVersion()
.TopN(1)
.FirstOrDefault();
VersionHistoryInfo versionHistory = VersionHistoryInfo.Provider.Get()
.WhereEquals("DocumentID", page.DocumentID)
.Columns("VersionHistoryID")
// Reorders the data so that the oldest version of the page is first in the result
.OrderByAscending("VersionHistoryID")
.TopN(1)
.FirstOrDefault();
if (versionHistory != null)
{
// Rolls back the specific page version
page.VersionManager.RollbackVersion(versionHistory.VersionHistoryID);
}
Deleting the latest version of a page
// Gets a page named 'On Roasts'
// The page is retrieved from the "MySite" site and in the "en-us" culture
TreeNode page = new DocumentQuery<TreeNode>()
.WhereEquals("DocumentName", "On Roasts")
.OnSite("MySite")
.Culture("en-us")
.Published()
.PublishedVersion()
.TopN(1)
.LatestVersion()
.FirstOrDefault();
if (page != null)
{
// Deletes the latest version of the page
page.VersionManager.DestroyDocumentVersion(page.DocumentCheckedOutVersionHistoryID);
}
Destroying the version history of a specific page
// Gets a page named 'On Roasts'
// The page is retrieved from the "MySite" site and in the "en-us" culture
TreeNode page = new DocumentQuery<TreeNode>()
.WhereEquals("DocumentName", "On Roasts")
.OnSite("MySite")
.Culture("en-us")
.Published()
.PublishedVersion()
.TopN(1)
.FirstOrDefault();
if (page!= null)
{
// Destroys the page history
page.VersionManager.DestroyDocumentHistory(page.DocumentID);
}
Versioning without workflow
Creating a versioned page without workflow
// Gets the current site's root "/" page, which will serve as the parent page
TreeNode parentPage = new DocumentQuery<TreeNode>()
.Path("/", PathTypeEnum.Single)
.OnSite("MySite")
.Published()
.PublishedVersion()
.TopN(1)
.FirstOrDefault();
if (parentPage != null)
{
// Creates a new page of the custom page type
TreeNode newPage = TreeNode.New("Custom.Article");
// Sets the properties of the new page
newPage.DocumentName = "Articles";
newPage.DocumentCulture = "en-us";
// Inserts the new page as a child of the parent page
newPage.Insert(parentPage);
// Manually publishes the page as the page is not using content locking
newPage.MoveToPublishedStep();
}
Updating a versioned page without workflow
// Gets a page named 'On Roasts'
// The page is retrieved from the "MySite" site and in the "en-us" culture
TreeNode page = new DocumentQuery<TreeNode>()
.WhereEquals("DocumentName", "On Roasts")
.OnSite("MySite")
.Culture("en-us")
.LatestVersion()
.TopN(1)
.FirstOrDefault();
// Note: When updating pages of multiple page types under workflow or versioning, always retrieve
// the pages with all columns to avoid data loss using the'WithCoupledColumns' parameterization.
// Optionally, you can specify the page types to retrieve using the 'Types' parameterization.
if (page != null)
{
// Ensures that a new version of the updated page is created (relevant even when not using content locking)
page.CheckOut();
// Sets new value to the 'DocumentName' and 'ArticleTitle'
page.DocumentName = "Updated article name";
page.SetValue("ArticleTitle", "Updated article title");
// Updates the page in the database
page.Update();
// Creates a new version of the updated page
page.CheckIn();
}