Microsoft Content Management Server : Managing Template Galleries and Templates (part 4) - Moving Template Galleries and Templates
Moving Template Galleries and Templates
Template galleries and templates are moved using Template Explorer or Site Manager by dragging and dropping the object from one container into another. The PAPI offers the TemplateGalleryItem.MoveTo() method to move these objects programmatically.
The technique is similar to copying templates. Here’s what we need to do:
- First, get a
reference to the template gallery item that we wish to move.
- Next,
get a reference to the destination template gallery that the template
gallery item is to be moved to.
- Finally, call the TemplateGalleryItem.MoveTo()
method.
Let’s build a Move Template dialog for CMS Explorer.
Add a web form named MoveTemplate.aspx to the project.
1. | Drag and drop styles.css onto the web form. | |||||||||||||||||||||
2. | In HTML view,
enter the following code:<table> | |||||||||||||||||||||
3. | Toggle to
Design view. Drag and drop the
following controls from the Web Forms section of the Toolbox and set
their properties as shown below:
When completed, the web form appears in Design view as shown below: | |||||||||||||||||||||
4. | Double-click
on the form to get to its code-behind file. Add the Microsoft.ContentManagement.Publishing
namespace:. . . code continues . . . using Microsoft.ContentManagement.Publishing;
| |||||||||||||||||||||
5. | In the Page_Load() event handler, we get
an instance of the template to be moved. In previous examples, we did a
check to see if the user has rights to work with an item only after the
user has clicked on the button. Let’s try a different approach this
time. Instead of returning an error message to users who do not have the
necessary permissions, we will enable or disable the button
accordingly. We check to see if the user has rights to move the template by checking the Template.CanMove property value. If Template.CanMove returns true, it means that the user is either an administrator or assigned to a rights group that belongs to a channel manager or template designer role of the current template gallery. If the user has the rights to move the template, we keep the btnMove button enabled; otherwise, it’s disabled. private CmsHttpContext cmsContext; private Template originalTemplate;
// Clear all error messages, if any. } | |||||||||||||||||||||
6. | Next, we
get an instance of the destination template gallery. We perform the move
operation only if the user has rights to create templates in the
destination template gallery. Once we have ascertained that the
destination template gallery is valid, we will call the Template.MoveTo()
method, passing the instance of the destination template gallery as an
input parameter. After moving a template, its state is Saved. Until the call to Template.Submit() is made for the template that has been moved, other template designers are not be able to modify the moved template or any of its connected templates. Toggle to Design view and double-click on the btnMove button to get to the btnMove_Click() event handler. Add the code shown below: private void btnMove_Click(object sender, System.EventArgs e) { |
Let’s move a template!
1. | Navigate to the MyTemplate
template that was created earlier. |
2. | Click Edit | Move. |
3. | In the Destination
Gallery field, enter /templates/tropicalgreen/. |
4. | Click Move Template. |
5. | Click Close to
close the dialog. |
6. | Back at
the CMS Explorer interface, navigate to the TropicalGreen template gallery, which now
contains the template, MyTemplate. |
Can I use the PAPI to move template galleries?
No, template galleries can only be moved using Site Manager or Template Explorer (available in Visual Studio .NET) by dragging the template gallery and dropping it into the destination.
When template galleries are moved, all templates within it are also moved. However, unlike moving templates, moving a template gallery does not change the state of the templates it contains.