Search
Windows Server

Microsoft Content Management Server : Copying Postings

Published 3 min readBy Programming4us Editorial Team
Postings can be copied from one channel to another or even to the same channel. Unlike connected postings, postings that are created from the copy action exist as independent entities. They do not share placeholder values, and so changes to the properties of the original posting do not affect the copy in any way.

Copying can be done using the Posting.CopyTo() method. Let’s create a Copy Posting dialog for CMS Explorer:

  1. Add a new web form to the CMSExplorer project. Name the new web form CopyPosting.aspx.

  2. In Design view, drag and drop the Styles.css file from Solution Explorer onto the form. This applies the stylesheet to the page.

  3. 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>
  4. Switch back to Design view. Drag and drop controls onto the web form and arrange them as shown in the screenshot overleaf.

    ControlPropertyProperty Value
    LiteralIDlitCurrentPosting
    TextBoxIDtxtDestination
    ButtonIDbtnCopy
     TextCopy Posting
    LabelIDlblErrorMessage
     Text(empty string)

  5. 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 PAPI

    using 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 copied

    Posting 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 . . .

  6. 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:

  1. Navigate to http://localhost/CmsExplorer.

  2. Select New | New Channel.

  3. Name the new channel MyNewChannel2. Click Create Channel and then click Close.

  4. Click MyNewChannel.

  5. Click the Edit button on the row for My New Posting.

  6. Click Copy. This will open a new browser window containing the Copy Posting dialog.

  7. 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.