Microsoft Content Management Server : Copying Postings
Copying can be done using the Posting.CopyTo() method. Let’s create a Copy Posting dialog for CMS Explorer:
Add a new web form to the CMSExplorer project. Name the new web form CopyPosting.aspx.
In Design view, drag and drop the Styles.css file from Solution Explorer onto the form. This applies the stylesheet to the page.
Toggle to HTML view and add the code as shown below for a table with four rows, between the <form> tags:
<table>
<tr>
<td colspan="2">
<h1>Copy Posting</h1>
<h2>Original Path: (Add Literal for displaying the path here)</h2>
</td>
</tr>
<tr>
<td>Destination Channel:</td>
<td>(Add the text box for the Destination Channel here)</td>
<tr>
<td colspan="2">(Add the Label for displaying error messages here)</td>
</tr>
<tr>
<td colspan="2" align="right">
(Add the Copy Posting button here)
<INPUT type="button" value="Close"
onclick="javascript:window.close();">
</td>
</tr>
</table>Switch back to Design view. Drag and drop controls onto the web form and arrange them as shown in the screenshot overleaf.
Control Property Property Value Literal ID litCurrentPosting TextBox ID txtDestination Button ID btnCopy Text Copy Posting Label ID lblErrorMessage Text (empty string)
Double-click on the web form to get to its code-behind file. Import the Microsoft.ContentManagement.Publishing namespace. We will put the path of the posting we’re copying into the litCurrentPosting label within the Page_Load() event handler.
. . . code continues . . .
// MCMS PAPIusing Microsoft.ContentManagement.Publishing;
namespace CmsExplorer
{
/// <summary>
/// Summary description for CopyPosting.
/// </summary>
public class CopyPosting : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtDestination;
protected System.Web.UI.WebControls.Label lblErrorMessage;
protected System.Web.UI.WebControls.Button btnCopy;
protected System.Web.UI.WebControls.Literal litCurrentPosting;
private void Page_Load(object sender, System.EventArgs e)
{
// display the path of the posting to be copiedPosting posting = CmsHttpContext.Current.Searches.GetByGuid(
Request.QueryString["CMSObjectGuid"]) as Posting;
if (posting != null)
litCurrentPosting.Text = posting.Path;
else
lblErrorMessage.Text = "Requested posting does not exist.";}
. . . code continues . . .
Toggle back to Design view and double-click on the btnCopy button. In the btnCopy_Click() event handler, add the code as follows:
Code View: Scroll / Show All
private void btnCopy_Click(object sender, System.EventArgs e)
{
CmsHttpContext cmsContext = CmsHttpContext.Current;// get the posting to be copied
Posting currentPosting;
currentPosting = cmsContext.Searches.GetByGuid(
Request.QueryString["CMSObjectGuid"]) as Posting;
if (currentPosting != null)
{
try
{
// get the destination channel
Channel destination;
destination = cmsContext.Searches.GetByPath(
txtDestination.Text) as Channel;
if (destination != null)
{
// check if we can create postings
if (!destination.CanCreatePostings)
{
throw new Exception("Current user does not have "
+ "permissions to create postings "
+ "in channel: " + destination.Path);
}
// copy the posting
Posting copiedPosting;
copiedPosting = currentPosting.CopyTo(destination);
// you can proceed to set the properties of the
// copied posting
// e.g. copiedPosting.DisplayName =
// "A Copy of the posting";
// commit the changes
cmsContext.CommitAll();
// display the success message
lblErrorMessage.Text = "Posting copied successfully!";
}
else
{
// cannot access the destination channel
lblErrorMessage.Text = "Invalid Destination channel path";
}
}
catch(Exception ex)
{
// rollback the changes
cmsContext.RollbackAll();
// after a rollback it is required to dispose of the CMS context
cmsContext.Dispose();
// display the error message
lblErrorMessage.Text = ex.Message;
}
}
}
The code first gets a reference to the posting to be copied followed by the destination channel specified in the txtDestination textbox. The Posting.CopyTo() method accepts the destination channel object as an input parameter. Once the copy has been created, you can proceed to set its properties. The copy operation will only be persisted after a call to CommitAll() is made.
In order to test the Copy Posting dialog, we first create a channel to which to copy the postings:
Navigate to http://localhost/CmsExplorer.
Select New | New Channel.
Name the new channel MyNewChannel2. Click Create Channel and then click Close.
Click MyNewChannel.
Click the Edit button on the row for My New Posting.
Click Copy. This will open a new browser window containing the Copy Posting dialog.
In the Copy Posting dialog, enter /Channels/MyNewChannel2 in the Destination Channel textbox, click Copy Posting, and then click Close.
Notice that a copy of My New Posting is created in MyNewChannel2. The copied posting has the same properties and placeholder content as the original posting with the following exceptions:
CreatedBy, LastModifiedBy, and OwnedBy; which hold the server account of the person who executed the copy
CreatedDate and LastModifiedDate; which contain the date and time that the copy operation was carried out
After copying, the copied posting’s state is Saved. It has to be approved before it is published.