.comment-link {margin-left:.6em;}

kHSw

Tuesday, August 17, 2004

Apply a style to a Content Management Placeholder

When you are editing a posting in Microsoft Content Management Server, your stylesheet is not applied to the placeholders. Most users find this unacceptable.

So I was looking for an easy and quick way to change the font-family and the font-size for all the placeholders.

I wrote a generic function I could use on all templates:



Imports Microsoft.ContentManagement.Publishing
Public Class kHSw
Public Shared Function BuildCSSScript(ByVal pPosting As Posting) As String
Dim pStringBuilder As New System.Text.StringBuilder
Dim sClassName As String = ConfigurationSettings.AppSettings("PlaceholderClassName")
pStringBuilder.Append("<script>")
For Each pPlaceholder As Placeholder In pPosting.Placeholders
pStringBuilder.Append("window.document.forms[0].NCPHRICH_" & pPlaceholder.Name & ".className = '" & sClassName &amp;amp;amp;amp; "';")
Next
pStringBuilder.Append("</script>")
Return pStringBuilder.ToString()
End Function
End Class


We call this function on every template in the Page_Load:

If kHSw.PostingIsInEditMode Then
RegisterStartupScript("SetClass", VBI.BuildCSSScript(ThisPosting))
End If


Where PostingIsInEditMode is definied as:

Imports Microsoft.ContentManagement.Publishing
Imports Microsoft.ContentManagement.WebControls

Public Class kHSw
Public Shared Function PostingIsInEditMode() As Boolean
If CmsHttpContext.Current.Mode = PublishingMode.Unpublished And WebAuthorContext.Current.Mode = WebAuthorContextMode.AuthoringReedit Or WebAuthorContext.Current.Mode = WebAuthorContextMode.AuthoringNew) Then
Return True
Else
Return False
End If
End Function
End Class

Now you can add the class for your placeholders to your stylesheet (the name of that class should be the value of PlaceholderClassName in the appSettings-section in the web.config).

Thanks to Dirk Dooms for pointing out a bug in the previously published code.

0 Comments:

Post a Comment

<< Home


 
Stefanie Worm is het liefste vrouwtje van de wereld.
Melina is de liefste schatsie van de wereld (Erik De Maeyer).