While working on SharePoint 2013 (on-premise), we can easily move our development to another environment, which is not the case in SharePoint online. To combat this, I have created a PowerShell based solution, using which, you can easily move or create a SharePoint site.
Here I share the steps to achieve this:
- Create Site collection with Blank template
- Publishing Feature Activation
- WSP Upload
- Apply Custom Template
- Assets Upload with Check-In
- Upload Master page and Page Layouts
- Set Master Page
- Create Page
- Add Web Parts on Pages
- Add list Item in List
In this post, I will talk about the first four steps, and the rest in the next blog. Let’s start!
Pre-requisite: – To create the site collection with custom template, create custom wsp package from the development environment. Download the assets file like Script, css, images, etc.
Step – 1: Create Site Collection with Custom template
To create the site with custom template, first create the site with blank template and then associate it with the custom template.
a. To create a site collection, from Custom Template, choose <Select template later..> as shown
b. Give inputs to mandatory fields such as site name, URL, etc. Click on OK and the Site Collection is created with blank template.
Step – 2: Activate the required feature
The site collection that we are creating requires the Infrastructure Feature to be activated. So, activate the infrastructure feature.
Deployment with SharePoint Online Management Shell and DLL
A. To deploy on SharePoint Online, we need SharePoint Online Management Shell with Site collection administrator rights. Download the SharePoint Online Management Shell from here
B. Add required SharePoint client dlls as below :
#Adding the Client OM Assemblies
Add-Type -Path “FOLDER PATHDLLMicrosoft.SharePoint.Client.dll”
Add-Type -Path “FOLDER PATHDLLMicrosoft.SharePoint.Client.Runtime.dll”
Add-Type -Path “FOLDER PATHDLLMicrosoft.SharePoint.Client.Publishing.dll”
C. Run SharePoint Online Management Shell as an admin and give the path of the script. Write script name and run like this: FileName.ps1
D. Enter inputs like: Site URL, Username, Password, FeatureGuid, IsSiteCollection. These inputs are to be added during the run time or they can be already added in the script.
E.First authenticate the site with the username and the password
$spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUserName, $sPassword) $spoCtx.Credentials = $credentials
return $spoCtx
F. After successful authentication, call the feature activation function
To activate the feature on site collection level, get site collection context as below:
$spoSite= $spoCtx.Site
G. Activate the feature using this chunk of code:
Add the Guid ID ($sFeatureGuid) that needs to be executed
$spoSite.Features.Add($sFeatureGuid, $true, [Microsoft.SharePoint.Client.FeatureDefinitionScope]::Farm)
Write-Host “$spoCtx.RequestTimeout = Timeout.Infinite”
$spoCtx.RequestTimeout = 5000*10000;
Write-Host “ExecuteQuery !!”
$spoCtx.ExecuteQuery()
H. After the completion, the required feature has been activated.
Step – 3: WSP Upload
After feature activation, we are proceeding with using SharePoint Online Management Shell, how can we upload WSP on SharePoint Online
A. Add full path of custom WSP package and assign it to the variable “$wspFullFilePath”
B. Upload WSP to the Solution Gallery using the below line of code:
$solutionGallery = $ctx.Web.Lists.GetByTitle(“Solution Gallery”)
$solutionGalleryRootFolder = $solutionGallery.RootFolder
$ctx.Load($solutionGallery)
$ctx.Load($solutionGallery.RootFolder)
$ctx.ExecuteQuery()
C. Assign filename to specific variable as below:
$wspFileName = “
$wspPackageName = “
$wspMajorVersion =
$wspMinorVersion =
D. Create the FileCreationInformation object as below:
$fileCI = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$fileCI.Overwrite = $true
$fileCI.ContentStream = $wspfileStream
$fileCI.URL = $WspFileName
E. Upload the created object to solution gallery as below
$uploadedFile = $solutionGallery.RootFolder.Files.Add($fileCI);
$ctx.Load($uploadedFile);
$ctx.ExecuteQuery();
F. Now use below steps to install the solution:
i. Create the DesignPackageInfo object
$wsp = New-Object Microsoft.SharePoint.Client.Publishing.DesignPackageInfo
$wsp.PackageGuid = [System.Guid]::Empty
$wsp.PackageName = $wspPackageName
$wsp.MajorVersion = $majorVersion
$wsp.MinorVersion = $minorVersion
ii. Install the solution from the file url
$filerelativeurl = $solutionGallery.RootFolder.ServerRelativeUrl + “/” + $WspFileName;
[Microsoft.SharePoint.Client.Publishing.DesignPackage]::Install($ctx, $ctx.Site, $wsp, $filerelativeurl)
$ctx.ExecuteQuery()
iii. Use below code to Apply or Activate the solution
[Microsoft.SharePoint.Client.Publishing.DesignPackage]::Apply($ctx, $ctx.Site, $wsp);
$ctx.ExecuteQuery()
Note: The Install and Apply methods create a copy of the solution. This copy is renamed according to the Major and Minor Version specified in the DesignPackageInfo object. Once this copy is activated, we no longer need the original wsp file uploaded to the solution gallery.
Below is the code to remove original WSP after getting renamed WSPs for Major and Minor Version specified in the DesignPackageInfo object:
$uploadedSolutionFile = $solutionGallery.rootFolder.Files.GetByUrl($filerelativeurl);
$uploadedSolutionFile.DeleteObject();
$ctx.ExecuteQuery()
Step – 4: Apply Custom Template
Below are the set of code to apply the template to web:
A. Get root location of the web site:
$web = $spoCtx.Site.RootWeb
B. Apply custom template to the web, using its internal name as below
$templateName = “{86EAF624-2B05-41E0-A552-AC875FE3737A}#AdvanceIntranetV4-19Dec”
$web.ApplyWebTemplate($templateName)
C. Update the web with applied custom template
$web.update()
$spoCtx.ExecuteQuery()
Now that you have understood how this is done, I have the complete feature activation script for you below. You can directly copy paste the code with the required modifications.
I hope this was helpful. Stay tuned for my next blog where I will tell you how to upload the WSP in SharePoint Online.