Insert link to CSS/JavaScript script from “Site Assets” library using custom action SharePoint

I had a requirement from a client to apply custom css and JavaScript to a site collection including sub sites.

The JavaScript file and Css file can be uploaded to “Site Assets” library using Module Element in SharePoint solution

Module element to deploy CSS file

<Elements xmlns=http://schemas.microsoft.com/sharepoint/>
<Module Name=CustomCss Url=SiteAssets>
<File Path=CustomCss\custom.css Url=CustomCss\custom.css />
</Module>
</Elements>

Module element to deploy CSS file

<Elements xmlns=http://schemas.microsoft.com/sharepoint/>
<Module Name=Jquery Url=SiteAssets>
<File Path=Jquery\jquery-1.11.1.min.js Url=Jquery/jquery-1.11.1.min.js />
</Module>
</Elements>

There are two options how to add the reference o the js file and css file

  1. The masterpage file can be modified to insert links to the JavaScript and css files.
  2. Use customaction to insert a link

I chose the second option which can be deployed as a sandboxed solution. To include a reference to  JavaScript in all pages in a SharePoint site from “Site Assets” library the customAction element is used with the ScriprSrc attribute.

<?xml version=1.0 encoding=utf-8?>
<Elements xmlns=http://schemas.microsoft.com/sharepoint/>
<CustomAction ScriptSrc=~SiteCollection/SiteAssets/jquery/jquery-1.11.1.min.jsLocation=ScriptLinkSequence=100“/>
</Elements>

To apply a Custom CSS deployed to “Site Assets ” library to a site the custom action element is used with ScriptBlock tag. The ScriptBlock generates the JavaScript code on the page which adds the reference to the css file when executed.

<?xml version=1.0 encoding=utf-8?>
<Elements xmlns=http://schemas.microsoft.com/sharepoint/>
<CustomAction Id=CssSiteCol Location=ScriptLinkScriptBlock=document.write(‘&lt;link rel=&quot;stylesheet&quot; After=&quot;Corev15.css&quot; type=&quot;text/css&quot; href=&quot;/PWA/SiteAssets/CustomCss/custom.css&quot;&gt;&lt;/’ + ‘link&gt;‘); Sequence=202 />
</Elements>

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s