We all know how useful the Content Editor Web Part (CEWP) can be. It is often called the Swiss Army Knife of SharePoint Web Parts: You can use it to add text, images, scripts and styles to a page, and even display content from external files. It’s this last point that we’ll be diving into in this article.
One way the CEWP can be used is by entering HTML or scripts into the HTML source. In SharePoint 2010, click inside the CEWP, and in the Format Text ribbon, click the HTML dropdown and select Edit HTML Source. Here, you can paste your HTML or script into the CEWP source and call it a day. Note that not all kinds of scripts will work properly when using this method.
An alternative to pasting a script or HTML into the CEWP’s HTML editor is pointing the Web Part to an external TXT or HTML file stored in a document library and have it display the contents of that file on your page. One benefit of this is method of displaying content is that you don’t have to edit the SharePoint page in order to change the content of the CEWP. Even better, if you use this same content on multiple pages, you only need to edit the source file to make the change to more than one page.
Let’s take this concept one step further and look at adding a CEWP to a page layout or master page. In SharePoint Designer, you can open a custom page layout or master page and simply drop a CEWP right into the page. More specifically, with the page layout or master page open, you would position your cursor where you want the CEWP to go (either in the code view or designer view), click the Insert tab in the Ribbon, click the Web Part dropdown, and finally select Content Editor from the list. This will drop code for the CEWP right into your page layout or master page.
The next step is to right-click the Web Part and choose Web Part Properties. This opens a properties box that is almost identical to the one you see when you edit a Web Part in the browser. In the Content Link section, type the URL for the content you’d like to pull in, and the CEWP will seamlessly integrate that content into your page. This is particularly useful for adding content that could potentially be changed to every page in a site, without the headache of having to make a change to hundreds of pages.
Why is this method superior to simply including HTML or scripts directly in your master page or page layouts? For starters, it can put control of the contents of these areas back in the end users’ hands. This way, a site owner can make changes to content without having to edit a page layout or master page, since they typically wouldn’t have access to do that anyway.
The Content Editor Web Part is definitely one of the most useful and flexible Web Parts you have in your arsenal for SharePoint.
Ryan Keller is a consultant at SharePoint911, a Rackspace company.