Wednesday, 25 February 2015

SharePoint 2013: How do I reference a html 'source' file from another site collection in a Content Editor Web Part?

Content Editor Web Parts are fantastic for rendering content, but there is small problem if you want to reference a file (through a ContentLink) on another site collection - you are not allowed to! Cross-site scripting is not a good thing.

This problem becomes apparent when you have a 'source' site collection that will host all the html/js files and you want all other site collections to reference this (single) source of truth.

There are a few options available to resolve the problem.
1. Copy all the JS/Html files to each site collection and reference them locally. (Easy, but it will create a maintenance nightmare if you have lots of site collections).
2. Enable Anonymous access to the 'source' site collection. (Easy, but not a great solution)
3. Install Content Link Web Part from Codeplex. (Better, but requires a Farm Solution)
4. Move the link from the ContentLink to the Content in the webpart.
The key to resolving the problem is nested CDATA tags.

In my project, we were using AngularJS, so used ng-include to point to my source file.

Here is my sample from the elements.xml for my page Module:

<AllUsersWebPart WebPartOrder="0" WebPartZoneID="RowLeft">
<WebPart xmlns:xsd="" xmlns:xsi="" xmlns="">
  <Title />
  <Height />
  <Width />
  <DetailLink />
  <HelpLink />
  <PartImageSmall />
  <MissingAssembly>Cannot import this Web Part.</MissingAssembly>
  <IsIncludedFilter />
  <Assembly>Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
  <ContentLink xmlns="" />
  <Content xmlns=""><![CDATA[
<div id="description" ng-include="src='/Style Library/html/MyModule.html'" ></div>
  <PartStorage xmlns="" />

The key with nested CDATA is to close the root tags immediately once the nested tags are used and create a new CDATA tag immediately afterwards.

No comments:

Post a comment