Monday, 16 November 2015

Install and Update Sandbox Solutions with CSOM

So recently, I was working on a SharePoint Online project and was looking for a way to automate the installation and update of a No Code Sandbox Solution (NCSS).

I was aware that you can install and activate Sandbox solutions in SPO with the following CSOM method:
Microsoft.SharePoint.Client.Publishing.DesignPackage.Install

There are a number of articles already covering this:

http://blog.symprogress.com/2013/07/apply-designpackage-using-client-object-model/

http://blogs.msdn.com/b/frank_marasco/archive/2014/08/10/upload-and-activate-sandbox-solutions-using-csom.aspx

The Office Dev Patterns and Practices project also uses this method to install a solution:
https://github.com/OfficeDev/PnP-Sites-Core/blob/master/Core/OfficeDevPnP.Core/AppModelExtensions/WebExtensions.cs

Just so you know, there are a couple of caveats to this approach as mentioned in the PnP documentation:
// NOTE: The lines below (in OfficeDev PnP) wipe/clear all items in the composed looks aka design catalog (_catalogs/design, list template 124).
Also, installing and activating the solution with this method will also automatically activate all Site-Collection level features, even if they have been set to AutoActivate = False


What I have found is, by changing the Major and Minor version numbers (which renames the WSP), you can also use the same method to update an existing solution

Here is my PowerShell script which Installs a solution (if it does not already exist) or updates it if the solution already exists, and the Major or Minor version number is different.

The PowerShell script:


The script is straightforward and similar to the other posts. The main difference from the PnP version being I do not run the DesignPackage.Uninstall method before Installing the solution and I also run the DesignPackage.Apply method after Installing the solution


1) Install a Solution:



After the script is run, I can see my WSP installed in the solution gallery with the Major and Minor versions I specified:

2) Update a Solution:


To update the solution, first we will need an updated WSP. It should have the relevant sections specified in the UpgradeActions Feature XML element.
@cann0nf0dder has a great post on configuring a sandbox solution for update here: Upgrading Sandbox Solutions in SharePoint

Once you have the updated WSP, all you need to do is change the Major and Minor version numbers in the script and Install your new WSP.


After the script is run, I can see my WSP is updated to the new version in the solution gallery with the Major and Minor versions I specified:


This way, the same DesignPackage.Install and DesignPackage.Apply methods can be used for updating sandbox solutions in SharePoint Online. 


6 comments:

Mark Wilson said...

Hey VMan!

Great post but the function is called installorupdatesolution so I would expect the first time I run it it installs but second time it updates. However on second run I get an error about needing to deactivate the solution first...feels like it is just install for first time?...

Vardhaman Deshpande said...

Hi Mark, How are you? Long time.

Did you change the major or minor versions in the script when you updated the solution?

When you do the second run, you should change the $wspMajorVersion or $wspMinorVersion variables in the script to indicate that you are updating to a new version of the WSP.

Without specifying new values to the $wspMajorVersion or $wspMinorVersion variables, SharePoint thinks that you are trying to activate the same WSP again and hence you get the "you need to deactivate the solution" error.

Mark Wilson said...

Not sure if they changed something but now if I try and called UnInstall on DesignPackage then I get a message about only being able to do that on public facing websites.

rlove said...

Hi VMan,

First off great script, gets me nearly where I need to be. However, I'm experiencing the same behavior as Mark. If the solution in the gallery is in the Activated state, the Uploading portion of the script fails and cannot overwrite the existing solution. Error: "You cannot delete or modify an active solution. Deactivate the solution first."

The script works perfectly if you manually Deactivate the solution first. Have you discovered a way through powershell to accomplish Deactivating the solution?

Vardhaman Deshpande said...

Hi rlove

Did you change the major or minor version in the script before running it for update?

rlove said...

Thanks, that did actually take care of the issue. Although I thought I was trying that before, it is working now. Cheers!