2002 was a great year……
MJ was still in the league, Phil Jackson wrapped up his third three-peat with the Lakers and the playoffs were entertaining. Who doesn’t remember a dominant and relevant Shaq referring to the Kings as the Sacramento ‘Queens’ and being entertained with another game seven in the Western Conference Finals.
Combining the high cost of hardware with the somewhat slow speeds of internet access, developers in the early 2000s had to be aware of how long their web pages would render for their visitors. The performance concerns that developers had back then are still relevant today.
There are a few options to include scripting into sites:
- Create or Alter a Page Layout using Visual Studio or SharePoint Designer (SPD)
- Inline scripting injected inside a script tag
- Reference to a script file inside your site
- Reference to a Content Delivery Network (CDN)
The benefit of this approach is that now the scripts will be available to any resource that uses the master page. As opposed to altering numerous pages to include a script file, you now have to make one entry in the master page.
When visiting Alaska, there is no need to pack that surf board. If scripting is used for one webpart that is needed on only one page, then it is unnecessary to have this script load for every other page inside your site. Including the script on the master page will ensure that the file is loaded into the browser whether or not the scripting is actually used.
To alleviate the issue of loading only necessary components, page layouts are used to reference scripting when a specific page type is loaded by a visitor. An easy approach to this is to create a new page layout using SharePoint Designer 2010 and include your script inside a content region with an ID of PlaceHolderAdditionalPageHead.
Saving the best for last? The last option for including script inside SharePoint involves using a Content Editor Web Part (CEWP) to link to a text file that contains your custom script.
None. This abomination of an approach essentially does the same as using a page layout by ensuring that the script is only loaded on a specific page.
By not paying attention to permissions or communication, an unknowing page editor could delete this web part from the SharePoint page which could then cripple the entire page. Perhaps this scenario is unlikely for the masses but it is fair to say that using CEWP is not even close to being robust as page layouts. When new pages are created, the creator can select your custom page layout that includes custom scripting. I would much rather have my scripts propagated inside a reusable container as opposed to manual entries to individual pages.
Using files that contain custom script provides another layer of flexibility. This enables developers the ability to alter the script directly as opposed to editing master pages or page layouts. Putting the files in a location that developers can access without relying on SharePoint Designer 2010 could also be viewed as a win especially in the scenarios where access to SharePoint Designer is locked down.
<SharePoint:ScriptLink ID=”ScriptLink1″ Name=”~sitecollection/Style Library/custom/JS/jquery-1.4.4.min.js” runat=”server” />
MSDN: ScriptLink Class http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.scriptlink.aspx
Content Delivery Network (CDN)
Inside the script tag, instead of referencing a relative path to an internal resource you can point to a publicly accessible file hosted on a Content Delivery Network (CDN), for example: http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.js. The benefit of this approach is that it will help out with performance. In regards to global visitors, using a CDN will dynamically determine which hosting server is closest to the request and serve content accordingly. Pay attention to this especially when thinking of Asia or Europe visitors who would normally have to download resources from US based servers. The glaring downfall to using a CDN is that it could be considered malicious to some corporate networks that could have this traffic blocked by their firewall.
There are several options for including custom scripts inside your SharePoint farm. Your decision will impact how quickly content is rendered to visitors. It is up to you to make a qualified decision as to which approach fits your corporate identity. Do you have development resources? Is this for a Proof of Concept site? Do security restrictions limit access to external sources? This blog post should outline what options are available to you with additional resources provided below.
Extra Credit Reading
Creating a Page Layout in SharePoint 2010 using Visual Studio 2010 (http://blog.beckybertram.com/Lists/Posts/Post.aspx?ID=71)
Exploring the Power of Page Layouts in SharePoint 2010 WCM Sites (http://channel9.msdn.com/Events/TechEd/Europe/2012/OSP335)
Page Layouts in SharePoint (http://blogs.msdn.com/b/kaevans/archive/2011/04/02/page-layouts-in-sharepoint.aspx)
jQuery Tools: ‘The missing UI library for the Web. jQuery Tools is a collection of the most important user-interface components for modern websites. ‘ http://jquerytools.org/