Microsoft Content Management Server 2002 makes it easy to create multiple websites using the same template set. This feature is very useful if you are creating sites that require different feel and look but same/similar functionality or at-least share some templates between each other.The challenge here is how to add a CSS to the rendered posting so the feel and look is different depending on which MCMS Site Hierarchy you are in.
Following is the solution. This solution allows you to treat CSS files as normal Content and hence can be managed by MCMS and also incorporates the MCMS work flow. Doing so allows the Desktop individuals to make changes to the CSS without bothering the IT team.
This implementation make use of the XML PlaceholderControl that I posted earlier in this Blog
In the .aspx file
In the Register Control Section :
<%@ Register TagPrefix="cnsCtl" Namespace="Jawad.Cms.CustomPlaceholders" Assembly="Jawad.Cms" %><%@ Register TagPrefix="cc2" Namespace="Cdi.Ces.Cms.CustomPlaceholders" Assembly="Cdi.Ces.Cms" %>
(inside the HTML Body) :
<form id="Form1" method="post" runat="server">
<cnsCtl:XMLPlaceholder id="PLContentPlaceholder" runat="server" PlaceholderToBind="plSinglePlaceholder" TextBoxHeight="800"
TextBoxColumns="100"></cnsCtl:XMLPlaceholder>
</form>
Now in the Code behind file :
protected Jawad.Cms.CustomPlaceholders.XMLPlaceholder PLContentPlaceholder;
private CmsHttpContext ctx;
private void Page_Load(object sender, System.EventArgs e)
{
if ( !IsPostBack)
{
ctx = CmsHttpContext.Current;
if ( ctx != null)
{
if ( ctx.Mode == PublishingMode.Published)
{
Posting pst = ctx.Posting ;
if ( pst != null )
{
Response.Write (((XmlPlaceholder)pst.Placeholders ["plSinglePlaceholder"]).XmlAsString);
Response.End ();
}
}
}
}
}
Once finished the .aspx file you need to create the MCMS Template lets say named “CSSTemplate” using MCMS Explorer from Visual Studio.Net and add an XMLPlaceholder definition to it named plSinglePlaceholder
Now you can place that posting in the Site Hierarchy you want For example
/Channels/PetShop/en-Ca/ConfigFiles/SiteCSS
another site on same server
/Channels/ToolShop/en-CA/ConfigFiles/SiteCSS
Where SiteCSS is posting name and other elements are channels in the Path.
When you create a new template you can add the CSS files as follows in to your template ....
<link href="<%= GetCSSPath() %>" type=text/css rel=stylesheet>
The GetCSSPath is a function that gives the Path to the CSS Posting ....Its implementation is as follows ( Please correct syntax errors if any)
public string GetCSSPath()
{
string path = string.Empty;
CmsHttpContext ctx = CmsHttpContext.Current;
if ( ctx != null)
{
string currentPostingPath = ctx.Posting.Path;
string siteName = currentPostingPath.Split('/')[1]; // Second Element is always site Name
// You might want to use dynamic approach to find complete path I am hard coding the fix elements here.
path = string.Format("/Channels/{0}/en-CA/ConfigFiles/SiteCSS",siteName);
}
return path;
}