Logo
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
EPL Standings
 
 
Windows Server

Microsoft Content Management Server : Managing Resources (part 2) - Replacing Resources

- 2015 Chevrolet Camaro Z28 - The Legend Returns
- Wagon Audi Allroad Vs. Subaru Outback
- 996 Carrera 4S is Driving Perfection
3/20/2011 5:04:37 PM

Replacing Resources

When updating a resource, it is a good idea to replace its content instead of deleting it and uploading it again. Replacing means you preserve the GUID and URL so that hyperlinks to the resource in postings will not only be kept intact but also immediately linked to the updated content.

Using the PAPI, resources are replaced using the Resource.SetContentFromFile() method. In this example, we will build a new dialog for replacing resources in CMS Explorer:

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

2.
In Design view, drag styles.css from Solution Explorer and drop it onto the web form.

3.
Toggle to HTML view and set the form’s ENCTYPE property to multipart/form-data:

<form id="Form1" method="post" runat="server"
enctype="multipart/form-data">
</form>

4.
Add a table with four rows:

<table>
<tr>
<td colspan="2">
<h1>Replace Resource</h1>
<h2>Current Resource: (Add Literal for displaying the path here)</h2>
</td>
</tr>
<tr>
<td><INPUT type="file" id="File1" runat="server"></td>
</tr>
<tr>
<td colspan="2">(Add the Label for error messages here)</td>
</tr>
<tr>
<td colspan="2" align="right">
(Add the Replace Resource button here)
<INPUT type="button" value="Close"
onclick="javascript:window.close();">
</td>
</tr>
</table>

5.
In Design view, drag and drop controls and set them up as detailed below:

ControlPropertyValue
LiteralIDlitCurrentResource
LabelIDlblErrorMessage
 Text(empty string)
ButtonIDbtnReplace
 TextReplace Resource

6.
Add the Microsoft.ContentManagement.Publishing namespace to the code-behind file.

. . . code continues . . .
// MCMS PAPI using Microsoft.ContentManagement.Publishing;

namespace CmsExplorer
{
. . . code continues . . .
}

7.
In the Page_Load() event handler, get the GUID of the resource to be replaced from the CMSObjectGuid parameter. We then pass it to the Searches.GetByGuid() method and display the resource’s path in the litCurrentResource literal. Once we have an instance of the resource to be replaced, we check to see if the user has the necessary rights by checking the value returned by the Resource.CanSetContent property. If the Resource.CanSetContent property returns true, it means that the user is an MCMS administrator, a resource manager, a template designer, or a channel manager assigned to the parent resource gallery. The btnReplace button is enabled accordingly.
private CmsHttpContext cmsContext;
private Resource currentResource;

private void Page_Load(object sender, System.EventArgs e)
{
cmsContext = CmsHttpContext.Current;
lblErrorMessage.Text = string.Empty;
string cmsObjectGuid = string.Empty;
if (Request.QueryString["CMSObjectGuid"]!=null)
{
cmsObjectGuid = Request.QueryString["CMSObjectGuid"];
currentResource = cmsContext.Searches.GetByGuid(cmsObjectGuid)
as Resource;
if (currentResource!=null)
{
litCurrentResource.Text = currentResource.Path;
btnReplace.Enabled = currentResource.CanSetContent;
}
else
{
lblErrorMessage.Text = "Resource not found!";
}
}
}


8.
Switch to Design view and double-click on the btnReplace button. In the btnReplace_Click() event handler, add the following code:

private void btnReplace_Click(object sender, System.EventArgs e)
{
try
{
if (File1.PostedFile != null)
{
// the temp folder for saving the posted file to
string filePath = CmsHttpContext.Current.TemporaryUploadFolder;
// get the file name from the posted file
string fileName = File1.PostedFile.FileName;
int indexOfSlash = fileName.LastIndexOf("\\") + 1;
fileName = fileName.Substring(indexOfSlash,fileName.Length -
indexOfSlash);
// check to see if the file name is valid
if(!Utility.ValidateMCMSObjectName(fileName))
{
throw new Exception("Specified template gallery name is not "
+ "valid. Must be only alphanumeric "
+ "characters, open or close parens, "
+ "hyphens, underscores, periods, or "
+ "spaces. No period at the end and no "
+ "consecutive periods are allowed. ");
}
// append the file name to the path of the temp folder
filePath += "\\" + fileName;
// save the file to a specified temp folder
File1.PostedFile.SaveAs(filePath);
// replace the contents of the resource
currentResource.SetContentFromFile(filePath);
currentResource.Name = fileName;
// commit the changes
cmsContext.CommitAll();
// display the success message
lblErrorMessage.Text = "Resource replaced successfully!";
}
else
{
// no file was selected for uploading
lblErrorMessage.Text = "Please select a file to upload";
}
}
catch(Exception ex)
{
// rollback all changes
cmsContext.RollbackAll();
// the CMS context needs to be disposed of after a rollback
cmsContext.Dispose();
// display error message
lblErrorMessage.Text = ex.Message;
}
}


We first retrieve the name of the file to be uploaded and check to see if it is valid. After saving the uploaded file to the Temp folder in the MCMS Server directory, we call the Resource.SetContentFromFile() method, which accepts the path of a valid file in the file system. Just as when creating resources, this file must reside on the MCMS server. After the contents of the resource are updated, we commit the changes to the content repository.

Save and build the solution. Let’s try out the Replace Resource dialog:

1.
Open CMS Explorer.

2.
Click Resources and navigate to the PlantCatalog resource gallery.

3.
Click the Edit link next to the CoconutTree.JPG resource.

4.
Click Replace.

5.
In the Replace Resource dialog, browse to an existing JPG file.

6.
Click Replace Resource.

7.
Click Close to close the dialog.

The content of the resource is replaced with the newly uploaded file. Notice that you are only allowed to replace the resource with a file of the same MIME Type. For example, the CoconutTree.JPG resource could only be replaced with other *.JPG files. Should you have chosen, say a *.XLS file, you would have gotten the following error message:

MIME Content-Types do not match. application/vnd.ms-excel does not match image/jpg.

Top Search -----------------
- Windows Server 2008 R2 : Work with RAID Volumes - Understand RAID Levels & Implement RAID
- Windows Server 2008 R2 Administration : Managing Printers with the Print Management Console
- Configuring Email Settings in Windows Small Business Server 2011
- Windows Server 2008 R2 : Configuring Folder Security, Access, and Replication - Implement Permissions
- Monitoring Exchange Server 2010 : Monitoring Mail Flow
- Windows Server 2008 R2 :Task Scheduler
- Windows Server 2008 R2 : File Server Resource Manager
- Windows Server 2008 R2 : Installing DFS
- Exchange Server 2010 : Managing Anti-Spam and Antivirus Countermeasures
- Windows Server 2008 R2 : Configuring Folder Security, Access, and Replication - Share Folders
Other -----------------
- Routing with Windows Server 2003 : Configuring Packet Filters
- Routing with Windows Server 2003 : Configuring and Managing Routing Protocols (part 2) - OSPF Overview & Understanding DHCP Relay Agent
- Routing with Windows Server 2003 : Configuring and Managing Routing Protocols (part 1) - Configuring RIP
- Routing with Windows Server 2003 : Configuring NAT
- Windows Server 2008 R2 : Choosing Between Traditional VPN Technologies and DirectAccess
- DirectAccess in Windows Server 2008 R2 (part 2)
- DirectAccess in Windows Server 2008 R2 (part 1)
- Understanding Network Services and Active Directory Domain Controller Placement for Exchange Server 2010 : Understanding AD Functionality Modes and Their Relationship to Exchange Server Groups
- Understanding Network Services and Active Directory Domain Controller Placement for Exchange Server 2010 : Exploring DSAccess, DSProxy, and the Categorizer
- Understanding Network Services and Active Directory Domain Controller Placement for Exchange Server 2010 : Defining the Global Catalog (part 2)
 
 
Most view of day
- Windows Server 2003 : Planning a Backup Strategy
- Install Windows Server 2008 R2 Roles (part 1) - Install Roles on a Windows Server 2008 R2 Full Server Installation
- Exchange Server 2007 : Using OWA Mail Features (part 1)
- SQL Server 2008 R2 : A Performance Monitoring Approach (part 1)
- Windows Server 2008 R2 : Group Policy Processing—How Does It Work?
- Share point 2010 : Managing Data Connections (part 2) - Creating BDC Models, Importing BDC Models
- Exchange Server 2010 : Securing Windows for the Edge Transport Server Role
Top 10
- Windows Phone 8 : Configuring Basic Device Settings - Providing Feedback
- Windows Phone 8 : Configuring Basic Device Settings - About Your Phone
- Windows Phone 8 : Configuring Basic Device Settings - Find My Phone
- Windows Phone 8 : Configuring Basic Device Settings - Accessibility (part 2) - Enabling the Screen Magnifier, Using Speech for Phone Accessibility
- Windows Phone 8 : Configuring Basic Device Settings - Accessibility (part 1) - Adjusting the Text Size, Enabling High Contrast
- Microsoft Visio 2010 : Linking External Data to Shapes (part 6) - Using Link Data - Linking Data to Shapes Using Link Data
- Microsoft Visio 2010 : Linking External Data to Shapes (part 5) - Using Link Data - Preparing a Master for Link Data , Importing Data for Link Data
- Microsoft Visio 2010 : Linking External Data to Shapes (part 4) - Using the Database Wizard - Taking the Data-Linked Light Bulb Shape for a Spin
- Microsoft Visio 2010 : Linking External Data to Shapes (part 3) - Using the Database Wizard - Setting Up the Excel File as a Data Source
- Microsoft Visio 2010 : Linking External Data to Shapes (part 3) - Using the Database Wizard - Setting Up the Excel File as a Data Source
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
2015 Camaro