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:

Office 365: Do More With Office 365 section in the Office 365 community!

This time I would like to share with you a new section created by Microsoft in the Office 365 community site that is intended to simplify the access to general support topics so anyone can find quickly find them. This new section is named Do More With Office 365 and you can find it in the following link: http://community.office365.com/en-us/w/do_more_with_office_365/default.aspx

image_thumb[1]

Office 365: Sección “Do More With Office 365” en el sitio de comunidad!

En esta ocasión os dejo la referencia a una nueva sección creada por Microsoft en la comunidad de Office 365 pensada para facilitar el acceso a temas de soporte de forma rápida. Se trata de la sección Do More With Office 365 que podéis encontrar en el siguiente enlace: http://community.office365.com/en-us/w/do_more_with_office_365/default.aspx

image

SharePoint Online: How to add a user to a SharePoint Group using PowerShell!

This time I would like to share a new PowerShell script ready to add users to SharePoint Groups in SharePoint Online using PowerShell. You can download the script from the TechNet Script Center Gallery: How to add users to a SharePoint Group in SharePoint Online

   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

   7: #  -> $sGroup: SPO Group where users are going to be added

   8: #  -> $sUserToAdd: User to be added

   9: ############################################################################################################################################

  10:  

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

  12:  

  13: #Definition of the function that allows to create a SharePoint Group in a SharePoint Online Site

  14: function Add-SPOUsersToGroup

  15: {

  16:     param ($sSiteColUrl,$sUsername,$sPassword,$sGroup,$sUserToAdd)

  17:     try

  18:     {    

  19:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  20:         Write-Host "Adding User $sUser to group $sGroup $sSiteColUrl" -foregroundcolor Green

  21:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  22:      

  23:         #Adding the Client OM Assemblies        

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

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

  26:  

  27:         #SPO Client Object Model Context

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

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

  30:         $spoCtx.Credentials = $spoCredentials 

  31:         

  32:         #Getting the SharePoint Groups for the site                        

  33:         $spoGroups=$spoCtx.Web.SiteGroups

  34:         $spoCtx.Load($spoGroups)        

  35:         #Getting the specific SharePoint Group where we want to add the user

  36:         $spoGroup=$spoGroups.GetByName($sGroup);

  37:         $spoCtx.Load($spoGroup)       

  38:         #Ensuring the user we want to add exists

  39:         $spoUser = $spoCtx.Web.EnsureUser($sUserToAdd)

  40:         $spoCtx.Load($spoUser)

  41:         $spoUserToAdd=$spoGroup.Users.AddUser($spoUser)

  42:         $spoCtx.Load($spoUserToAdd)

  43:         $spoCtx.ExecuteQuery()     

  44:                 

  45:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  46:         Write-Host "SharePoint User $sUser added succesfully!!" -foregroundcolor Green

  47:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  48:         $spoCtx.Dispose()

  49:     }

  50:     catch [System.Exception]

  51:     {

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

  53:     }    

  54: }

  55:  

  56: #Required Parameters

  57: $sSiteColUrl = "https://<Office365_Site>" 

  58: $sUsername = "<Office365_UserAccount>" 

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

  60: $sPassword=convertto-securestring "<Office365_Password>" -asplaintext -force

  61: $sGroup="<SPO_Group>"

  62: $sUserToAdd="i:0#.f|membership|<Office365_UserAccount>"

  63:  

  64: Add-SPOUsersToGroup -sSiteColUrl $sSiteColUrl -sUsername $sUsername -sPassword $sPassword -sGroup $sGroup -sUserToAdd $sUserToAdd

As you can see, you have only to use the right CSOM objects so you can add a user to an existing SharePoint Group. And this is the result you get:

image_thumb[1]

SharePoint Online: Como añadir un usuario a un grupo por medio de PowerShell!

En esta ocasión os dejo un nuevo script que permite añadir usuarios a grupos de SharePoint en SharePoint Online por medio de PowerShell. Podéis descargaros el script desde el siguiente enlace: How to add users to a SharePoint Group in SharePoint Online

   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

   7: #  -> $sGroup: SPO Group where users are going to be added

   8: #  -> $sUserToAdd: User to be added

   9: ############################################################################################################################################

  10:  

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

  12:  

  13: #Definition of the function that allows to create a SharePoint Group in a SharePoint Online Site

  14: function Add-SPOUsersToGroup

  15: {

  16:     param ($sSiteColUrl,$sUsername,$sPassword,$sGroup,$sUserToAdd)

  17:     try

  18:     {    

  19:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  20:         Write-Host "Adding User $sUser to group $sGroup $sSiteColUrl" -foregroundcolor Green

  21:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  22:      

  23:         #Adding the Client OM Assemblies        

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

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

  26:  

  27:         #SPO Client Object Model Context

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

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

  30:         $spoCtx.Credentials = $spoCredentials 

  31:         

  32:         #Getting the SharePoint Groups for the site                        

  33:         $spoGroups=$spoCtx.Web.SiteGroups

  34:         $spoCtx.Load($spoGroups)        

  35:         #Getting the specific SharePoint Group where we want to add the user

  36:         $spoGroup=$spoGroups.GetByName($sGroup);

  37:         $spoCtx.Load($spoGroup)       

  38:         #Ensuring the user we want to add exists

  39:         $spoUser = $spoCtx.Web.EnsureUser($sUserToAdd)

  40:         $spoCtx.Load($spoUser)

  41:         $spoUserToAdd=$spoGroup.Users.AddUser($spoUser)

  42:         $spoCtx.Load($spoUserToAdd)

  43:         $spoCtx.ExecuteQuery()     

  44:                 

  45:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  46:         Write-Host "SharePoint User $sUser added succesfully!!" -foregroundcolor Green

  47:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  48:         $spoCtx.Dispose()

  49:     }

  50:     catch [System.Exception]

  51:     {

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

  53:     }    

  54: }

  55:  

  56: #Required Parameters

  57: $sSiteColUrl = "https://<Office365_Site>" 

  58: $sUsername = "<Office365_UserAccount>" 

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

  60: $sPassword=convertto-securestring "<Office365_Password>" -asplaintext -force

  61: $sGroup="<SPO_Group>"

  62: $sUserToAdd="i:0#.f|membership|<Office365_UserAccount>"

  63:  

  64: Add-SPOUsersToGroup -sSiteColUrl $sSiteColUrl -sUsername $sUsername -sPassword $sPassword -sGroup $sGroup -sUserToAdd $sUserToAdd

Cómo veis, de nuevo basta con hacer uso de los objetos adecuados del modelo de objetos en cliente para poder añadir en este caso un usuario a un grupo de SharePoint existente. Y el resultado que se obtiene es el siguiente:

image

SharePoint Online: How to create a SharePoint Group through PowerShell!

This time I would like to share with you how to create a new SharePoint Group in a SharePoint Online site using PowerShell. You can download the script from the TechNet script gallery: How to create a SharePoint Group in a SharePoint Online Site

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

   2: #Script that allows to create a SharePoint Group in a SharePoint Online Site

   3: # Required Parameters:

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

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

   6: #  -> $sSiteCollectionUrl: SharePoint Online Site

   7: #  -> $sGroupToCreate: SPO Group to create

   8: #  -> $sGroupToCreateDescription: SPO Group description

   9: ############################################################################################################################################

  10:  

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

  12:  

  13: #Definition of the function that allows to create a SharePoint Group in a SharePoint Online Site

  14: function Create-SPOGroup

  15: {

  16:     param ($sSiteColUrl,$sUsername,$sPassword,$sGroupToCreate,$sGroupToCreateDescription)

  17:     try

  18:     {    

  19:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  20:         Write-Host "Creating SharePoint Group $sGroupToCreate in $sSiteColUrl" -foregroundcolor Green

  21:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  22:      

  23:         #Adding the Client OM Assemblies        

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

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

  26:  

  27:         #SPO Client Object Model Context

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

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

  30:         $spoCtx.Credentials = $spoCredentials 

  31:  

  32:         #Root Web Site

  33:         $spoRootWebSite = $spoCtx.Web

  34:         #Object for creating a new SPO Group

  35:         $spoGroupCreationInfo=New-Object Microsoft.SharePoint.Client.GroupCreationInformation

  36:         $spoGroupCreationInfo.Title=$sGroupToCreate

  37:         $spoGroupCreationInfo.Description=$sGroupToCreateDescription

  38:         $spoGroup=$spoRootWebSite.SiteGroups.Add($spoGroupCreationInfo)

  39:         $spoCtx.ExecuteQuery()

  40:         

  41:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  42:         Write-Host "SharePoint Group $sGroupToCreate in $sSiteColUrl created succesfully!!" -foregroundcolor Green

  43:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  44:         $spoCtx.Dispose()

  45:     }

  46:     catch [System.Exception]

  47:     {

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

  49:     }    

  50: }

  51:  

  52: #Required Parameters

  53: $sSiteColUrl = "https://<SPO_Site>/" 

  54: $sUsername = "<SPO_User>" 

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

  56: $sPassword=convertto-securestring "<Password>" -asplaintext -force

  57: $sGroupToCreate="CustomSPOGroup"

  58: $sGroupToCreateDescription="Custom SPO Group"

  59:  

  60: Create-SPOGroup -sSiteColUrl $sSiteColUrl -sUsername $sUsername -sPassword $sPassword -sGroupToCreate $sGroupToCreate -sGroupToCreateDescription $sGroupToCreateDescription

As you can see, create a SharePoint Group in a SharePoint Online is quite simple using the GroupCreationInformation object. And once you execute the script, this is the result you can see in your SharePoint Online site.

image_thumb