SharePoint Online: How to enable a Sandbox solution using PowerShell (I)!

A typical issue when we work with SharePoint Online is we have more limitations when using the available APIS compared with the things we can do using the APIs available OnPremises. For instance, this situation happens when we want to automate the activation of Sandbox solutions in the solution gallery of a SharePoint Online site collection. In order to do this kind of automation, we have to follow SharePoint Online rules what means we can only use the Client Side Objet Model (CSOM). Indeed, in this blog post I will use some of the objects and classes available in the Publishing namespace in the CSOM as a way to activate an existing Sandbox solution. As you can see in the script (you can download it from How to activate a Sandbox solution in SharePoint Online, in order to actívate a Sandbox solution you have to use the DesignPackageInfo class available in the Publishing CSOM and then the static class DesignPackage that allows to activate the solution in the solution gallery:

   1: ############################################################################################################################################

   2: #Script that allows to add Users to a SPO Group

   3: # Required Parameters:

   4: #  -> $sUserName: User Name to connect to the SharePoint Online Site Collection.

   5: #  -> $sPassword: Password for the user.

   6: #  -> $sSiteColUrl: SharePoint Online Site Collection.

   7: #  -> $sSiteCollectionRelativePath: Site Collection Relative Path.

   8: #  -> $sSolutionName: Sandbox solution name.

   9: #  -> $sSolutionFile: Sandbox solution file.

  10: ############################################################################################################################################

  11:  

  12: $host.Runspace.ThreadOptions = "ReuseThread"

  13:  

  14: #Definition of the function that allows to activate a Sandbox solution in SharePoint Online

  15: function Activate-SandboxSolution

  16: {

  17:     param ($sSiteColUrl,$sUserName,$sPassword,$sSiteCollectionRelativePath,$sSolutionName,$sSolutionFile)

  18:     try

  19:     { 

  20:         #Adding the Client OM Assemblies        

  21:         Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.dll"

  22:         Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.Runtime.dll"

  23:         Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.Publishing.dll"

  24:  

  25:         #SPO Client Object Model Context

  26:         $spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteColUrl) 

  27:         $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUsername, $sPassword)  

  28:         $spoCtx.Credentials = $spoCredentials      

  29:     

  30:         $spoDesignPackageInfo=New-Object Microsoft.SharePoint.Client.Publishing.DesignPackageInfo

  31:         $spoDesignPackageInfo.PackageGuid=[GUID]::Empty

  32:         $spoDesignPackageInfo.MajorVersion=1

  33:         $spoDesignPackageInfo.MinorVersion=1

  34:         $spoDesignPackageInfo.PackageName=$sSolutionName

  35:         $sSolutionRelativePath=$sSiteCollectionRelativePath + "_catalogs/solutions/" + $sSolutionFile

  36:         $sSolutionRelativePath

  37:         #[Microsoft.SharePoint.Client.Publishing.DesignPackage] | Get-Member -Static

  38:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  39:         Write-Host "Activating the Sandbox Solution in the Solution Gallery!!" -foregroundcolor Green

  40:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  41:         $spoDesignPackage=[Microsoft.SharePoint.Client.Publishing.DesignPackage]

  42:         $spoDesignPackage::Install($spoCtx,$spoCtx.Site,$spoDesignPackageInfo,$sSolutionRelativePath)      

  43:         $spoCtx.ExecuteQuery()

  44:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  45:         Write-Host "Sandbox Solution successfully activated in the Solution Gallery!!" -foregroundcolor Green

  46:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  47:         $spoCtx.Dispose()

  48:     }

  49:     catch [System.Exception]

  50:     {

  51:         write-host -f red $_.Exception.ToString()   

  52:     }    

  53: }

  54:  

  55: #Required Parameters

  56: $sSiteColUrl = "https://<Your_SPO_Site_Collection>" 

  57: $sUserName = "<Your_SPO_User>" 

  58: #$sPassword = Read-Host -Prompt "Enter your password: " -AsSecureString  

  59: $sPassword=convertto-securestring "<Your_SPO_Pasword>" -asplaintext -force

  60: $sSolutionName="<Sandbox_Solution_Name>"

  61: $sSolutionFile="<Sandbox_Solution_File>"

  62: $sSiteCollectionRelativePath="/sites/<SiteCollectionName>/"

  63:  

  64: Activate-SandboxSolution -sSiteColUrl $sSiteColUrl -sUserName $sUserName -sPassword $sPassword -sSiteCollectionRelativePath $sSiteCollectionRelativePath -sSolutionName $sSolutionName -sSolutionFile $sSolutionFile

Once you execute the script, the Sanbox solutions is activated in the Sandbox gallery. However, you must note that the actual Sanbox solution is not activated…instead, a copy of the original Sandbox solution is activated.

image_thumb

Referencias:

SharePoint Online: Como activar una solución Sandbox por medio de PowerShell (I)!

Uno de los problemas típico cuando trabajamos con SharePoint Online es que a nivel de API nos encontramos con limitaciones importantes a la hora de realizar operaciones que con el modelo de objetos en servidor resultan sencillas. Esto sucede si queremos automatizar la activación de soluciones de tipo Sandbox en la galería de soluciones de una colección de Sitios de SharePoint Online…para poder realizar este automatismo, no nos queda otra que recurrir al modelo de objetos en cliente y en particular en el caso de este artículo a la variante CSOM para Publishing. Como podéis ver en el script, que podéis descargaros desde How to activate a Sandbox solution in SharePoint Online, la clave de todo está en hacer uso del objeto del DesignPackageInfo del CSOM de Publishing que define un objeto de tipo paquete de diseño de SharePoint encapsulado en la solución Sanbox y luego instalarlo haciendo uso de la clase estática DesignPackage:

   1: ############################################################################################################################################

   2: #Script that allows to add Users to a SPO Group

   3: # Required Parameters:

   4: #  -> $sUserName: User Name to connect to the SharePoint Online Site Collection.

   5: #  -> $sPassword: Password for the user.

   6: #  -> $sSiteColUrl: SharePoint Online Site Collection.

   7: #  -> $sSiteCollectionRelativePath: Site Collection Relative Path.

   8: #  -> $sSolutionName: Sandbox solution name.

   9: #  -> $sSolutionFile: Sandbox solution file.

  10: ############################################################################################################################################

  11:  

  12: $host.Runspace.ThreadOptions = "ReuseThread"

  13:  

  14: #Definition of the function that allows to activate a Sandbox solution in SharePoint Online

  15: function Activate-SandboxSolution

  16: {

  17:     param ($sSiteColUrl,$sUserName,$sPassword,$sSiteCollectionRelativePath,$sSolutionName,$sSolutionFile)

  18:     try

  19:     { 

  20:         #Adding the Client OM Assemblies        

  21:         Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.dll"

  22:         Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.Runtime.dll"

  23:         Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.Publishing.dll"

  24:  

  25:         #SPO Client Object Model Context

  26:         $spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteColUrl) 

  27:         $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUsername, $sPassword)  

  28:         $spoCtx.Credentials = $spoCredentials      

  29:     

  30:         $spoDesignPackageInfo=New-Object Microsoft.SharePoint.Client.Publishing.DesignPackageInfo

  31:         $spoDesignPackageInfo.PackageGuid=[GUID]::Empty

  32:         $spoDesignPackageInfo.MajorVersion=1

  33:         $spoDesignPackageInfo.MinorVersion=1

  34:         $spoDesignPackageInfo.PackageName=$sSolutionName

  35:         $sSolutionRelativePath=$sSiteCollectionRelativePath + "_catalogs/solutions/" + $sSolutionFile

  36:         $sSolutionRelativePath

  37:         #[Microsoft.SharePoint.Client.Publishing.DesignPackage] | Get-Member -Static

  38:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  39:         Write-Host "Activating the Sandbox Solution in the Solution Gallery!!" -foregroundcolor Green

  40:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  41:         $spoDesignPackage=[Microsoft.SharePoint.Client.Publishing.DesignPackage]

  42:         $spoDesignPackage::Install($spoCtx,$spoCtx.Site,$spoDesignPackageInfo,$sSolutionRelativePath)      

  43:         $spoCtx.ExecuteQuery()

  44:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  45:         Write-Host "Sandbox Solution successfully activated in the Solution Gallery!!" -foregroundcolor Green

  46:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  47:         $spoCtx.Dispose()

  48:     }

  49:     catch [System.Exception]

  50:     {

  51:         write-host -f red $_.Exception.ToString()   

  52:     }    

  53: }

  54:  

  55: #Required Parameters

  56: $sSiteColUrl = "https://<Your_SPO_Site_Collection>" 

  57: $sUserName = "<Your_SPO_User>" 

  58: #$sPassword = Read-Host -Prompt "Enter your password: " -AsSecureString  

  59: $sPassword=convertto-securestring "<Your_SPO_Pasword>" -asplaintext -force

  60: $sSolutionName="<Sandbox_Solution_Name>"

  61: $sSolutionFile="<Sandbox_Solution_File>"

  62: $sSiteCollectionRelativePath="/sites/<SiteCollectionName>/"

  63:  

  64: Activate-SandboxSolution -sSiteColUrl $sSiteColUrl -sUserName $sUserName -sPassword $sPassword -sSiteCollectionRelativePath $sSiteCollectionRelativePath -sSolutionName $sSolutionName -sSolutionFile $sSolutionFile

Y el resultado que se obtiene de la ejecución del script anterior es que se activa correctamente la Solución Sandbox, aunque como véis no se activa la solución original sino que se crea una copia de la misma que es la que finalmente se activa.

image

Referencias: