You can change the parent container of a posting by moving it. Postings can be moved in both the Web Author (using the Move Posting
option) and Site Manager (dragging and dropping postings from one
channel to another). Once moved, postings require moderator approval if
any are assigned for the destination channel.
The PAPI provides the Posting.MoveTo()
method to move postings using code. One common application of the
method is to automatically approve postings that are moved. When
reorganizing large numbers of postings, it is often quicker to write a
script to automate the move and to approve them immediately instead of
performing these steps manually.
To see how the Posting.MoveTo() method is used within code, we’ll create a Move Posting dialog for CMS Explorer:
Add a new web form to the CMSExplorer project. Name the new web form MovePosting.aspx.
In Design view, drag and drop the Styles.css file from Solution Explorer onto the form.
Toggle to HTML view. Add the following code for a table with four rows between the <form> tags.
<table>
<tr>
<td colspan="2">
<h1>Move 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 Move Posting button here)
<INPUT type="button" value="Close"
onclick="javascript:window.close();">
</td>
</tr>
</table>
In Design view, add controls and arrange them as shown in the diagram below:
Control | Property Property | Value |
---|
Literal | ID | litCurrentPosting |
TextBox | ID | txtDestination |
Button | ID | btnMove |
| Text | Move Posting |
Label | ID | lblErrorMessage |
| Text | (empty string) |
Double-click on the form to get to the code-behind file. As before, import the Microsoft.ContentManagement.Publishing namespace. In the Page_Load() event handler, populate the litCurrentPosting Literal with the path of the posting to be moved.
. . . code continues . . .
// MCMS PAPI
using Microsoft.ContentManagement.Publishing;
namespace CmsExplorer
{
/// <summary>
/// Summary description for MovePosting.
/// </summary>
public class MovePosting : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtDestination;
protected System.Web.UI.WebControls.Label lblErrorMessage;
protected System.Web.UI.WebControls.Button btnMove;
protected System.Web.UI.WebControls.Literal litCurrentPosting;
private void Page_Load(object sender, System.EventArgs e)
{
// display the path of the posting to be moved
Posting currentPosting;
currentPosting = CmsHttpContext.Current.Searches.GetByGuid(
Request.QueryString["CMSObjectGuid"]) as Posting;
if (currentPosting != null)
litCurrentPosting.Text = currentPosting.Path;
else
lblErrorMessage.Text = "Requested posting does not exist.";
}
#region Web Form Designer generated code
. . . code continues . . .
#endregion
}
}
Toggle to Design view and double-click on the btnMove button. In the btnMove_Click() event handler, add the following code:
private void btnMove_Click(object sender, System.EventArgs e)
{
CmsHttpContext cmsContext = CmsHttpContext.Current;
// get the posting to be moved
Posting 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);
currentPosting.MoveTo(destination);
// you can approve the posting
// or set its properties here
// e.g. currentPosting.Approve();
cmsContext.CommitAll();
// display the success message
lblErrorMessage.Text = "Posting moved successfully!";
}
else
{
lblErrorMessage.Text = "Invalid Destination Channel";
}
}
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;
}
}
else
{
lblErrorMessage.Text = "Requested posting does not exist.";
}
}
The Posting.MoveTo() method accepts the destination channel as an input parameter. In the code above, we use the Searches.GetByPath() method to get the destination channel based on the path entered in the txtDestination textbox. If the channel exists, we pass it in as an input parameter to the Posting.MoveTo() method.
Let’s see the move in action. First, if you haven’t already done so, build the CMSExplorer project, then:
Open http://localhost/CMSExplorer.
Click on the Edit button of the MyPosting posting and click Move.
In the Move Posting dialog, enter /Channels/MyNewChannel2/ as the destination channel. Click Move Posting.
And MyPosting gets moved from MyNewChannel to MyNewChannel2.
Moving postings does not
modify any of their placeholder or property values. However, if there
are moderators assigned to the destination channel, the posting would be
in a “Waiting For Moderator Approval” state. At the same time, the LastModifiedBy and LastModifiedDate
properties of the moved posting are modified to indicate the person who
executed the move and the date and time that the operation was
performed.