SharePoint Online: activar una función mediante PowerShell
Requisito: Active una característica en la colección de sitios de SharePoint Online mediante PowerShell.
Solución: en SharePoint local, utilizamos el cmdlet Enable-SPFeature para activar/habilitar funciones . En SharePoint Online, no existe ningún cmdlet disponible a través de PowerShell para activar la característica. Pero podemos utilizar el modelo de objetos del lado del cliente (CSOM) para activar funciones en SharePoint Online. Activemos la función "Infraestructura de publicación de SharePoint Server". A continuación se muestra un ejemplo de SharePoint Online PowerShell para activar una característica de colección de sitios:
¿Cómo habilitar una función en SharePoint Online?
La activación de una característica hace que la funcionalidad asociada con la característica esté disponible. Podría traer nuevas funcionalidades o artefactos como listas y bibliotecas, etc. A continuación se explica cómo activar una característica del sitio en SharePoint Online:
- Navegue hasta el sitio donde debe activarse la función.
- En la página de inicio del sitio, haga clic en el ícono de ajustes de Configuración y luego en la opción Configuración del sitio en el menú Configuración.
- En la página Configuración del sitio, haga clic en el enlace "Administrar características del sitio" en la sección Acciones del sitio.
Digamos, por ejemplo, cómo activar la función de publicación en SharePoint Online: En la página Funciones del sitio, haga clic en el botón Activar junto a la función del sitio que desea activar.
- El estado de la característica cambia a Activo y las capacidades de la característica asociada se habilitan en el sitio.
PowerShell para habilitar la función de colección de sitios en SharePoint Online
¿Necesita habilitar una función en SharePoint Online rápidamente? ¡PowerShell puede ayudar a habilitar una característica en SharePoint Online sin tener que usar la GUI del navegador web! Permítame mostrarle cómo usar PowerShell para habilitar una característica en SharePoint Online:
#Load SharePoint CSOM Assemblies
Import-Module Microsoft.Online.SharePoint.Powershell
#Variables for Processing
$SiteURL = "https://crescent.sharepoint.com/Sites/Sales"
$FeatureGUID =[System.GUID]("f6924d36-2fa8-4f0b-b16d-06b7250180fa") #Publishing Feature ID
$LoginName ="Salaudeen@crescent.OnMicrosoft.com"
$LoginPassword ="Password"
#Get the Client Context
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
#Login Credentials
$SecurePWD = ConvertTo-SecureString $LoginPassword -asplaintext -force
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $LoginName, $SecurePWD
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Credential.UserName,$Credential.Password)
#Get the Site
$site = $ctx.site
#sharepoint online powershell activate feature
$site.Features.Add($FeatureGUID, $force, [Microsoft.SharePoint.Client.FeatureDefinitionScope]::farm)
$ctx.ExecuteQuery()
write-host "Feature has been Activated!"
Este PowerShell permite funciones de publicación en SharePoint Online.
SharePoint Online: PowerShell para activar la función
Agreguemos algo de manejo de errores al código anterior, como "verificar si la función ya está activada" y crear una función reutilizable en PowerShell para habilitar la función en SharePoint Online.
#Load SharePoint Online CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Function to Enable Feature in SharePoint Online
Function Enable-SPOFeature
{
param ($SiteCollURL,$UserName,$Password,$FeatureGuid)
Try
{
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteCollURL)
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $Password)
$Ctx.Credentials = $Credentials
$Site=$Ctx.Site
#Check the Feature Status
$FeatureStatus = $Site.Features.GetById($FeatureGuid)
$FeatureStatus.Retrieve("DefinitionId")
$Ctx.Load($FeatureStatus)
$Ctx.ExecuteQuery()
#sharepoint online activate feature using powershell (if its not enabled already)
if($FeatureStatus.DefinitionId -eq $null)
{
Write-Host "Enabling Feature $FeatureGuid..." -ForegroundColor Yellow
$Site.Features.Add($FeatureGuid, $true, [Microsoft.SharePoint.Client.FeatureDefinitionScope]::None) | Out-Null
$Ctx.ExecuteQuery()
Write-Host "Feature Enabled on site $SiteCollURL!" -ForegroundColor Green
}
else
{
Write-host "Feature is Already Active on the Site collection!" -ForegroundColor Red
}
}
Catch
{
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
}
#Parameters to Activate Feature
$SiteCollURL = "https://Crescent.sharepoint.com/sites/Demo"
$UserName = "SPAdmin@Crescent.com"
$Password = "Password goes here"
$FeatureGuid= [System.Guid] ("f6924d36-2fa8-4f0b-b16d-06b7250180fa") #Publishing Feature
$SecurePassword= ConvertTo-SecureString $Password -asplaintext -force
#sharepoint online enable feature powershell
Enable-SPOFeature -SiteCollURL $SiteCollURL -UserName $UserName -Password $SecurePassword -FeatureGuid $FeatureGuid
Esto activa la característica Infraestructura de publicación de SharePoint Server en SharePoint Online.
¿Cómo activar la función del sitio (Web) usando PowerShell?
Si bien el código anterior activa la función de nivel de colección de sitios, el mismo código también se puede utilizar para activar cualquier función de nivel web. Cambiar la línea:
$Site=$Ctx.site
A
$Site=$Ctx.web
Además, configure el GUID para la función de nivel web. Esto activa la característica usando PowerShell en SharePoint Online.
PnP PowerShell para habilitar la función de publicación en SharePoint Online:
Activemos una característica de SharePoint Online usando PnP PowerShell en el nivel de colección de sitios usando el cmdlet Enable-PnPFeature.
#Config Variable
$SiteURL = "https://Crescent.sharepoint.com/Sites/Procurement"
$FeatureId = "f6924d36-2fa8-4f0b-b16d-06b7250180fa" #Site Scoped Publishing Feature
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#get the Feature
$Feature = Get-PnPFeature -Scope Site -Identity $FeatureId
#Get the Feature status
If($Feature.DefinitionId -eq $null)
{
#sharepoint online powershell enable feature
Write-host -f Yellow "Activating Feature..."
Enable-PnPFeature -Scope Site -Identity $FeatureId -Force
Write-host -f Green "Feature Activated Successfully!"
}
Else
{
Write-host -f Yellow "Feature is already active!"
}
De manera similar, puede activar la función de publicación usando PnP PowerShell en el ámbito web como:
#Config Variable
$SiteURL = "https://Crescent.sharepoint.com/Sites/Procurement"
$FeatureId = "94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb" #Web Scoped Publishing Feature
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#Get the Feature to activate
$Feature = Get-PnPFeature -Scope Web -Identity $FeatureId
#Get the Feature status
If($Feature.DefinitionId -eq $null)
{
#activate publishing feature sharepoint online using powershell
Write-host -f Yellow "Activating Feature..."
Enable-PnPFeature -Scope Web -Identity $FeatureId -Force
Write-host -f Green "Feature Activated Successfully!"
}
Else
{
Write-host -f Yellow "Feature is already active!"
}
Para deshabilitar una característica en SharePoint Online usando PowerShell, consulte: SharePoint Online: Desactivar característica usando PowerShell
