Overview
Custom Actions offer a flexible way to extend capabilities of the SharePoint. The possibilities span the range of including custom JavaScript on every page to extending the Ribbon. In SharePoint 2013/SharePoint Online you can leverage the CSOM/REST to manage custom actions. Below are demonstrated two simple examples of using custom actions in real world scenarios and I hope you you’ll find them useful.
Example 1. Enable jQuery
Let’s get started with an example that demonstrate how to add jQuery library to Office 365/SharePoint Online site. Unfortunately it is not supported to reference external resources, for example from Microsoft Ajax Content Delivery Network (CDN) that hosts popular third party JavaScript libraries including jQuery. The prerequisite for referencing JavaScript files is that they could only be accesible when located within the site collection. So, the first step would be to save a jQuery library into Style Library: /Style Library/Scripts/jQuery/jquery-2.1.1.js.
The following Activate-JQuery.ps1 script demonstrates how to enable jQuery library in Office 365/SharePoint Online site
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
[string]$SiteUrl, | |
[string]$UserName, | |
[string]$Password | |
) | |
. ".\UserCustomActions.ps1" | |
<# | |
.SYNOPSIS | |
Enable jQuery Library | |
.DESCRIPTION | |
Enable jQuery Library in Office 365/SharePoint Online site | |
.EXAMPLE | |
.\Activate-JQuery.ps1 -SiteUrl "https://tenant-public.sharepoint.com" -UserName "username@tenant.onmicrosoft.com" -Password "password" | |
#> | |
Function Activate-JQuery([string]$SiteUrl,[string]$UserName,[string]$Password) | |
{ | |
$context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl) | |
$context.Credentials = Get-SPOCredentials –UserName $UserName –Password $Password | |
$sequenceNo = 1482 | |
$jQueryUrl = "~SiteCollection/Style Library/Scripts/jQuery/jquery-2.1.1.js" | |
Add-ScriptLinkAction –Context $Context –ScriptSrc $jQueryUrl –Sequence $sequenceNo | |
$context.Dispose() | |
} | |
Activate–JQuery –SiteUrl $SiteUrl –UserName $UserName –Password $Password |
Dependencies: UserCustomActions.ps1
Example 2. Enable Google Analytics
The following Activate-GoogleAnalytics.ps1 script demonstrates how to activate tracking code in Office 365/SharePoint Online site
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
[string]$SiteUrl, | |
[string]$UserName, | |
[string]$Password | |
) | |
. ".\UserCustomActions.ps1" | |
<# | |
.SYNOPSIS | |
Activate Google Analytics | |
.DESCRIPTION | |
Activate Google Analytics in Office 365/SharePoint Online site | |
.EXAMPLE | |
.\Activate-GoogleAnalytics.ps1 -SiteUrl "https://tenant-public.sharepoint.com" -UserName "username@tenant.onmicrosoft.com" -Password "password" | |
#> | |
Function Activate-GoogleAnalytics([string]$SiteUrl,[string]$UserName,[string]$Password) | |
{ | |
$context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl) | |
$context.Credentials = Get-SPOCredentials –UserName $UserName –Password $Password | |
$sequenceNo = 1480 | |
#Insert your Tracking code here or specify valid Tracking ID | |
$TrackingCode = "(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ | |
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), | |
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) | |
})(window,document,'script','//www.google-analytics.com/analytics.js','ga'); | |
ga('create', 'UA-XXXXXXXX-X', 'auto'); | |
ga('send', 'pageview');" | |
Add-ScriptLinkAction –Context $Context –ScriptBlock $TrackingCode –Sequence $sequenceNo | |
$context.Dispose() | |
} | |
Activate–GoogleAnalytics –SiteUrl $SiteUrl –UserName $UserName –Password $Password |
Dependencies: UserCustomActions.ps1
Follow these instructions to use Google Analytics to collect data from Office 365/SharePoint Online sites.
To set up the web tracking code:
- Find the tracking code snippet for your property.
Sign in to your Google Analytics account, and click Admin in the top menu bar. From the Account and Propertycolumns, select the property you’re working with. Click Tracking Info / Tracking Code.
- Find your tracking code snippet. It’s in a box with several lines of JavaScript in it. Everything in this box is your tracking code snippet. It starts with
<script>
and ends with</script>
.
The tracking code contains a unique ID that corresponds to each Google Analytics property. Don’t mix up tracking code snippets from different properties, and don’t reuse the same tracking code snippet on multiple domains. Click to expand this image and see where the tracking code snippet is in the interface. - Open Activate-GoogleAnalytics.ps1, paste the tracking code into $TrackingCode variable and run the script to register tracking code in Office 365/SharePoint Online site
- Check your set up.
Make sure that the tracking snippet installed on your website matches the code shown in the view, and see more ways you can verify your set up.