SharePoint Online:使用 PowerShell 激活功能
要求:使用 PowerShell 激活 SharePoint Online 网站集中的功能。
解决方案:在 SharePoint 本地部署中,我们使用 Enable-SPFeature cmdlet 来激活/启用功能。在 SharePoint Online 中,没有这样的 cmdlet 可通过 PowerShell 来激活该功能。但我们可以利用客户端对象模型 (CSOM) 来激活 SharePoint Online 中的功能。让我们激活“SharePoint Server 发布基础架构”功能。以下是用于激活网站集功能的 SharePoint Online PowerShell 示例:
如何启用 SharePoint Online 中的功能?
激活功能使与该功能相关的功能可用。它可以带来新的功能或项目,例如列表和库等。以下是如何在 SharePoint Online 中激活网站功能:
- 导航到需要激活该功能的站点。
- 在网站的主页上,单击“设置”齿轮图标,然后单击“设置”菜单中的“网站设置”选项。
- 在“站点设置”页面上,单击“站点操作”部分下的“管理站点功能”链接。
例如,以下是如何激活 SharePoint Online 中的发布功能:在“网站功能”页面上,单击要激活的网站功能旁边的“激活”按钮。
- 功能状态更改为“活动”,并在站点上启用关联的功能。
PowerShell 在 SharePoint Online 中启用网站集功能
您需要快速启用 SharePoint Online 中的某项功能吗? PowerShell 可以帮助启用 SharePoint Online 中的功能,而无需使用 Web 浏览器 GUI!让我向您展示如何使用 PowerShell 启用 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!"
此 PowerShell 支持 SharePoint Online 中的发布功能。
SharePoint Online:用于激活功能的 PowerShell
让我们在上面的代码中添加一些错误处理,例如“检查该功能是否已激活”,并在 PowerShell 中创建一个可重用的函数以在 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
这将激活 SharePoint Online 中的 SharePoint Server 发布基础结构功能。
如何使用PowerShell激活网站功能(Web)?
虽然上述代码激活网站集级别功能,但相同的代码也可用于激活任何 Web 级别功能。更改线路:
$Site=$Ctx.site
到
$Site=$Ctx.web
另外,为 Web 级功能设置 GUID!这将使用 SharePoint Online 中的 PowerShell 激活该功能。
PnP PowerShell 在 SharePoint Online 中启用发布功能:
让我们使用 Enable-PnPFeature cmdlet 在网站集级别使用 PnP PowerShell 激活 SharePoint Online 功能。
#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!"
}
同样,您可以在 Web 范围内使用 PnP PowerShell 激活发布功能,如下所示:
#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!"
}
要使用 PowerShell 禁用 SharePoint Online 中的功能,请参阅:SharePoint Online:使用 PowerShell 停用功能
