Add Content Editor Web Part into Site Template

In Onet.xml, find the module(s) that need to have a content editor web part and add the following:


<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 Web part</Title>
            <FrameType>None</FrameType>
            <Description>Say Hello Web Part</Description>
            <FrameState>Normal</FrameState>
            <Assembly>Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
            <TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>
            <ContentLink xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />
            <Content xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor">
                &lt;script&gt; 

                function SayHello() 
                { 
			alert('Hello');
                  	
                      //all done 
                      return; 
                    } 
                  } 
                }


                _spBodyOnLoadFunctionNames.push(&quot;SayHello&quot;) 

                &lt;/script&gt;
            </Content>
            <PartStorage xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />
          </WebPart>
          ]]>

        </AllUsersWebPart>

Note some characters need to be encoded.

In case a web part is broken, use contents=1 query string, for example: http://myurladdress/default.aspx?contents=1. This will show a list of all the web parts on the page.

Cheers 🙂

Advertisements