Stefan Goßner

Senior Escalation Engineer for SharePoint (WSS, SPS, MOSS, SP2010) and MCMS

Blogs

Accessing placeholder content and DataSource.RawContent

  • Comments 3
  • Likes

I have seen several posts in the newsgroup where people suggested to use the DataSource.RawContent property to access the content of a placeholder.

This method is highly not recommended! It completly bypasses the internal processing if the actual placeholder implementation which means that the content received relies on the current implementation of the placeholder. If a placeholder creator (e.g.) decides to store the content no longer in clear text in the database but in encrypted form then DataSource.RawContent would return the useless encrypted stuff.

Or a less theoretical idea: what if someone decides that it is a good idea if the AttachmentPlaceholder should not only expose a property to return the Url but also a property to store the Attachment size? Currently only the Url is stored in the placeholder so the method using DataSource.RawContent will return the Url correctly (e.g. /nr/rdonlyres/.... But with this new functionality additional information needs to be stored. Maybe the developer then store the data as follows: <url>/nr/rdonlyres/...</url><size>12000</size> DataSource.RawContent will return this string which is not a valid Url as you can see. The developer of the placeholder will ensure that that Url property of the placeholder only returns the Url and the new expose Size property returns 12000.

Only when the correct properties and methods exposed by the actual placeholder definition are used then you can expect that the content received will be the same between hotfixes and service packs!

So how does bad code and good code look like?

I have seen recommendations to access the Url of an ImagePlaceholder in the current posting using this statement:

bad: string Url = CmsHttpContext.Current.Posting.Placeholders["Image"].Datasource.RawContent.ToString;

the correct method for an ImagePlaceholder would like as follows:

good: string Url = ((ImagePlaceholder)(CmsHttpContext.Current.Posting.Placeholders["Image"])).Src;

The same needs to be done for Attachment, Html and XmlPlaceholders:

string Url = ((AttachmentPlaceholder)(CmsHttpContext.Current.Posting.Placeholders["Attachment"])).Url;
string Html = ((HtmlPlaceholder)(CmsHttpContext.Current.Posting.Placeholders["HtmlContent"])).Html;
string Xml = ((XmlPlaceholder)(CmsHttpContext.Current.Posting.Placeholders["XmlContent"])).XmlAsString;

If you are unsure which placeholder type you have use the following method:

if (CmsHttpContext.Current.Posting.Placeholders["ph"] is ImagePlaceholder)
{
    ...
}
if (CmsHttpContext.Current.Posting.Placeholders["ph"] is AttachmentPlaceholder)
{
    ...
}
if (CmsHttpContext.Current.Posting.Placeholders["ph"] is HtmlPlaceholder)
{
    ...
}
if (CmsHttpContext.Current.Posting.Placeholders["ph"] is XmlPlaceholder)
{
    ...
}

If you have any custom placeholder definitions add them to the list above.

Comments