/// <summary>
/// Creates the publishing page.
/// </summary>
/// <param name="web">
/// The destination web
/// </param>
/// <param name="pageContentTypeId">
/// The page content type id.
/// </param>
/// <param name="pageLayout">
/// The page layout to be used by the new publishing page.
/// </param>
/// <param name="checkInComments">
/// The check in comments.
/// </param>
public void CreatePublishingPage(SPWeb web, string pageName, string promotedPageContentTypeId, string pageLayout, string checkInComments)
{ PublishingPage page = null;
try
{ // Ensure the destination web is a publishing web
if (!PublishingWeb.IsPublishingWeb(web))
{ throw new ArgumentException("Destination web is not a publishing web"); }
// Get the publishing web
PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
// Create the page
// This code uses a Collaboris helper class to create the publishing page.
// If you would like to see the source code for the helper method please let us know
page = SPPublishingHelper.CreatePublishingPage(publishingWeb, pageContentTypeId, pageLayout, pageName);
// Initialise the values the values of any publishing page properties here
//.........
// Update the page
page.Update();
// Checkin and publish the page
page.ListItem.File.CheckIn(checkInComments);
page.ListItem.File.Publish(checkInComments);
// Get the default content approval workflow id that is a associated with the list
Guid approvalworkflowId = page.ListItem.ParentList.DefaultContentApprovalWorkflowId;
// If there is an apporval workflow associated with the list then we need to start it programmatically
if (approvalworkflowId != Guid.Empty)
{ // Find the right workflow (there can be several associated with the pages list)
foreach (SPWorkflowAssociation workflowAssociation in page.ListItem.ParentList.WorkflowAssociations)
{ if (workflowAssociation.Id == approvalworkflowId)
{ // We have found the workflow so start it
listItem.Web.Site.WorkflowManager.StartWorkflow(listItem, workflowAssociation, workflowAssociation.AssociationData, true);
break;
}
}
}
}
catch (Exception e)
{ // Rollback the changes
// If the page has been created then we need to delete it
if (page != null)
{ page.ListItem.File.Delete();
}
// Build a friendly error message and use a Collaboris helper class to log the error
// Please let us know if you would like to see the code for the SPLogger class
string friendlyMessage = string.Format(CultureInfo.CurrentCulture, SPResources.GetString("ModuleName", "FailedToCreatePageWithUrl"), destinationUrl); SPLogger.LogToOperations(e, friendlyMessage);
throw;
}
}