Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Server

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

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
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.

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)
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server