Introduction
When migrating the content from On-Premises to the Cloud, you will follow a certain process, for instance:
- Informing the users about the migration
- Planning for the content to be migrated
- Performing the migration
- Validating the content
- Performing incremental work
- Locking down site
- URL re-direction
Again, all the above steps may not be exactly the same, but in most migration scenarios, the business requirement is to redirect the users to the new sites. This could be because users might have bookmarked the old on-premise URL and would like to have a redirection in place for some time until all the users are aware of the new site.
In this article, we will go through the URL redirection for the SP2013 on-premises site to Office 365 SharePoint online. To have this redirection we will not use SharePoint designer, instead will use the concept of Custom Action. This is applicable for SharePoint 2013 and SharePoint 2016 sites.
The idea here is when a user navigates to Old URL the javascript gets rendered which notifies the user via a notification banner regarding the new site and redirects the user to the new site.
Custom Action
Before we go to steps, we need to understand what Custom-Action is. A Custom-Action is a new concept introduced in SharePoint 2010 which is used to customize the ribbon and list item menus. In SharePoint 2013, with the introduction of the add-in model, there is no need to deploy a full trust code to customize ribbons and list menu items. Here you use REST API or SharePoint CSOM (client-side object model) to achieve these kinds of customizations which are referred to as ‘Remote Provisioning Pattern’. The advantage here is there is no hard update to application pages, all the rendering happens during the runtime.
Upload the redirection script
In this stage, upload the URL redirect script to Style Library in the Site Contents page and then publish the file to the final version.
Below are the steps,
Step 1
Have the following code saved as ‘SiteURLRedirection.js’,
- var title = "";
- var desSite = "https://contoso.sharepoint.com/sites/ContosoNewHome";
- var str1 = "Site has been migrated to the new URL - " + desSite
- var str2 = "You will be redirected to the new URL shortly ..."
-
- function getUrlVars() {
- var vars = [],
- hash;
- var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
- for (var i = 0; i < hashes.length; i++) {
- hash = hashes[i].split('=');
- vars.push(hash[0]);
- vars[hash[0]] = hash[1];
- }
- return vars;
- }
-
- function ShowStatusBarMessage(title, message) {
- var statusId = SP.UI.Status.addStatus(title, message, true);
- SP.UI.Status.setStatusPriColor(statusId, 'yellow');
- return statusId;
- }
-
- function ShowNotificationMessage(tooltip, message, sticky) {
- SP.UI.Notify.addNotification(message, sticky, tooltip, null);
- }
- if (getUrlVars()["stopredirect"] == "1") {} else {
- setTimeout(function() {
- var nid = ShowStatusBarMessage(title, str1, true);
- setTimeout(function() {
- SP.UI.Status.removeStatus(nid);
- nid = ShowStatusBarMessage(title, str2, true);
- setTimeout(function() {
- SP.UI.Status.removeStatus(nid);
- setTimeout("location.href='" + desSite + "'", 1000);
- }, 1000);
- }, 2000);
- }, 2000);
- }
Step 2
In the variable ‘dessite’, make sure to update the URL to the correct destination according to your needs.
Step 3
Upload the script to your ‘Style Library’ which is available in site contents. As a best practice, create a folder inside the style library (I am creating it as ‘CustomScripts’). This way when registering this script as a custom action, it will be easy to modify or manage custom action.
Step 4
Observe that when uploading any custom scripts to the style library, it will be in checked-out mode. Make sure to check this script and publish the final version. This is very important.
Register the Custom Action
Now you have all the bells and whistles ready, you just need to have the final action which is redirection to happen. To achieve this run the following lines of PowerShell command. This command creates the custom action which will redirect the users to a new destination. No matter what pages within the old site collection the user goes it needs to redirect to the new site. Whether the user is on the Documents page, homepage, or site contents page, articles page, etc, it should redirect to a new destination.
Please note that the redirection happens to the home page of the URL that you have mentioned in the script. For example, if the user is in the documents library in the old site, it will not navigate to the document library in the new site, it only navigates to the new site home page.
- $loadInfo1 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
- $loadInfo2 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
- #Must be SharePoint Site Collection Administrator URL
- $siteUrl = Read - Host - Prompt "Input Site URL"
- $ctx = New - Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
- $site = $ctx.Site
- $username = Read - Host - Prompt "Input Admin ID"
- write - host "Please enter your password"
- $password = read - host - AsSecureString
- $ctx.Credentials = New - Object System.Net.NetworkCredential($username, $password)
- $ctx.Load($site)
- $ctx.ExecuteQuery()
- $userCustomActions = $site.get_userCustomActions();
- $newUserCustomAction = $userCustomActions.add();
- $newUserCustomAction.set_location('ScriptLink');
- $newUserCustomAction.set_scriptSrc('~SiteCollection/Style Library/CustomScripts/SiteURLReDirection.js');
- $newUserCustomAction.set_sequence(10);
- $newUserCustomAction.set_title('Contoso URL Redirection');
- $newUserCustomAction.set_description('Contoso URL Redirection');
- $newUserCustomAction.update();
- $ctx.ExecuteQuery()
Note
Since the JavaScript needs to run on every page in the site collection I am using location type as ‘ScriptLink’. Make sure to update the scriptSrc to the correct location. The sequence can be between 1 and 1000. I have given as 10. Set a proper title and description. In this case, I have given as ‘Contoso URL Redirection’ for both title and description.
Process
At first script prompts for the URL of the site collection, enter the site collection URL.
Next, it asks for an admin ID. The account needs to be a site collection admin. After that, it prompts for the password.
On successful authentication, you should see the following messages in the notification banner.
Unregister the custom action
For some reason, if you need to cancel the redirection you can run the following PS command to unregister the custom action. After this observe that URL redirection no longer works.
- $loadInfo1 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
- $loadInfo2 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
- #Must be SharePoint Administrator URL
- $siteUrl = Read - Host - Prompt "Input Site URL"
- $ctx = New - Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
- $site = $ctx.Site
- $username = Read - Host - Prompt "Input Admin ID"
- write - host "Please enter your password"
- $password = read - host - AsSecureString
- $ctx.Credentials = New - Object System.Net.NetworkCredential($username, $password)
- $ctx.Load($site)
- $ctx.ExecuteQuery()
- $userCustomActions = $site.get_userCustomActions();
- $ctx.Load($userCustomActions)
- $ctx.ExecuteQuery()
- foreach($action in $userCustomActions) {
- if ($action.title - eq 'Contoso URL Redirection') {
- $action.DeleteObject()
- Write - Host $action.Title 'Contoso Branding Customization Custom action deleted.'
- $ctx.ExecuteQuery()
- }
- }
- $ctx.ExecuteQuery()
Powershell Output
Conclusion
Thus, in this article, we have seen how to make the URL redirection from on-prem site to cloud SPO site, using custom action. We haven’t used SharePoint designer to achieve this.
References
- https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/custom-actions-sharepoint-add-in
- https://docs.microsoft.com/en-us/previous-versions/office/developer/sharepoint-2010/hh500259(v=office.14)?redirectedfrom=MSDN
- https://blog.mastykarz.nl/deploying-custom-actions-app-model/