SharePoint Online:使用 PowerShell 删除网站列
要求:使用 PowerShell 删除 SharePoint Online 中的网站列。
如何删除 SharePoint Online 中的网站栏?
您是否曾向 SharePoint Online 环境添加网站栏,但后来决定不再需要它?如果需要删除 SharePoint Online 中的网站栏,可以使用 Web 浏览器 UI 或 PowerShell 来执行此操作。这个过程比较简单,只需要几个步骤。在这篇博文中,我们将讨论如何删除 SharePoint Online 中的网站栏。我们将介绍通过 Web 浏览器删除网站栏所需的步骤以及如何使用 PowerShell 在 SharePoint Online 中删除网站栏。让我们开始吧!
要删除 SharePoint Online 网站栏,请执行以下步骤:
这消除了 SharePoint Online 中的网站栏。当您删除网站栏时,使用该网站栏的任何列表或库都不会受到影响,并且数据仍然安全!
SharePoint Online PowerShell 删除网站列:
为了使用 PowerShell 删除 SharePoint Online 中的网站栏,首先,您需要连接到 SharePoint Online 网站。这将提示您输入凭据。连接后,接下来您需要识别要删除的网站栏。最后,您必须调用DeleteObject 方法。
以下是如何使用 PowerShell 删除网站栏:
#Load SharePoint 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"
#Set Variables for Site URL and Site column Title
$SiteURL= "https://crescent.sharepoint.com/sites/sales"
$ColumnName="Sales Region" #Case sensitive
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred
#Check if the site column exists
$Fields = $Ctx.web.Fields
$Ctx.Load($Fields)
$Ctx.ExecuteQuery()
$FieldNames = $Fields | select -ExpandProperty Title
#delete site column in sharepoint online
If($FieldNames.Contains($ColumnName))
{
#Delete the site column
$SiteColumn = $Fields.GetByTitle($ColumnName)
$Sitecolumn.DeleteObject()
$Ctx.ExecuteQuery()
Write-host -f Green "Site Column Deleted Successfully!"
}
else
{
Write-host -f Yellow "Site Column Doesn't Exists!"
}
}
Catch {
write-host -f Red "Error deleting Site Column!" $_.Exception.Message
}
脚本运行后,指定的网站栏将被删除。
SharePoint Online:PowerShell 删除组中的所有网站列
使用 PowerShell 可以轻松删除网站栏。您可以在 SharePoint Online 中快速删除网站列,以在清理网站集时节省时间和精力。让我向您展示如何使用 PowerShell 删除组中的所有网站栏:
#Load SharePoint 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 Remove All Site Columns of a Group
Function Remove-SiteColumnGroup([String]$SiteURL, [String]$SiteColumnGroup)
{
Try{
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
$Ctx.Load($Ctx.Web)
$Ctx.Load($Ctx.Web.Fields)
$Ctx.ExecuteQuery()
#Get the site column Group
$SiteColumns = $Ctx.Web.Fields | where {$_.Group -eq $SiteColumnGroup}
if($SiteColumns -ne $NULL)
{
#Loop Through Each Field
ForEach($Column in $SiteColumns)
{
Write-host "Removing Site Column:" $($Column.InternalName)
$Column.DeleteObject()
$Ctx.ExecuteQuery()
}
Write-host -f Green "Site Columns Deleted Successfully!"
}
Else
{
Write-host -f Yellow "Site Column Group '$($SiteColumnGroup)' Does not Exist!"
}
}
Catch {
write-host -f Red "Error Removing Site Columns!" $_.Exception.Message
}
}
#Set Config Parameters
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$SiteColumnGroup="Crescent Projects"
#Get Credentials to connect
$Cred= Get-Credential
#Call the function to Import the Site Columns from XML
Remove-SiteColumnGroup $SiteURL $SiteColumnGroup
PnP PowerShell 从 SharePoint Online 网站删除网站列:
您的 SharePoint Online 网站中是否有要删除的列?使用 PnP PowerShell cmdlet Remove-PnPField 在 SharePoint Online 中删除网站栏是一个简单的过程:
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ColumnName= "ProjectStartDate" #Internal Name
#Get Credentials to connect
$Cred = Get-Credential
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials $Cred
#Remove Site column
Remove-PnPField -Identity $ColumnName -Force -ErrorAction Stop
Write-host "Site Column '$ColumnName' Removed From the site Successfully!" -f Green
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
请注意,如果网站栏是任何内容类型的成员,则在从内容类型中删除该栏之前,您无法删除它!
