Most of you probably already know this, but I thought I would post about it for those who dont.
I have had a couple of questions from people on how to add a content editor web part to a site template (site definition) that contains custom html / javascript code as its content. They have no problem with inserting normal text into the web part that they include in the site template, only when they add some html code.
- Lets say we want to add the following table code:
<Table><td>My table in the Web Part</td></table>
The problem lies with the duplicate <![CDATA[ tags in the web part itself; the second one prematurely ends the first ones tag:
- The first <![CDATA[ tag is just after the "<AllUsersWebPart WebPartZoneID="Left" WebPartOrder="1"> tag.
- The second <![CDATA[ tag is just after the "<Content xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor">" tag.
Replace the second instance of the "<![CDATA[" and "]]" tag with <Value> and </Value>:
- From this:
<Content xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor"><![CDATA[<Table><td>My table in the Web Part</td></table>]]</Content>
- To this:
<Content xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor"><Value><Table><td>My table in the Web Part</td></table></Value></Content>his:
The other problem lies within the format of the html / javascript tags within the <Content...></Content> tags:
- The "<" and ">" opening and closing brackets need to be replaced with "<" and ">" respectively.
So, replace the opening and closing tags:
- From this:
<Table><td>My table in the Web Part</td></table>
- To this:
<Table><td>My table in the Web Part</td></table>
Now your content editor webpart will work in your site template, below is the whole piece of code:
<AllUsersWebPart WebPartZoneID="Left" WebPartOrder="1"><![CDATA[<WebPart xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/WebPart/v2">
<Title>My Table Web Part</Title>
<FrameType>None</FrameType>
<Description>Use for formatted text, tables, and images.</Description>
<IsIncluded>true</IsIncluded>
<ZoneID>Left</ZoneID>
<PartOrder>0</PartOrder>
<FrameState>Normal</FrameState>
<Height />
<Width />
<AllowRemove>true</AllowRemove>
<AllowZoneChange>true</AllowZoneChange>
<AllowMinimize>true</AllowMinimize>
<AllowConnect>true</AllowConnect>
<AllowEdit>true</AllowEdit>
<AllowHide>true</AllowHide>
<IsVisible>true</IsVisible>
<DetailLink />
<HelpLink />
<HelpMode>Modeless</HelpMode>
<Dir>Default</Dir>
<PartImageSmall />
<MissingAssembly>Cannot import this Web Part.</MissingAssembly>
<PartImageLarge>/_layouts/images/mscontl.gif</PartImageLarge>
<IsIncludedFilter />
<Assembly>Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
<TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>
<ContentLink xmlns="" />
<Content xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor"><Value><Table><td>My table in the Web Part</td></table></Value></Content>
<PartStorage xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />
</WebPart>]]></AllUsersWebPart>