kwizcom banner advertisement

SharePoint Online: Delete Site Column using Powershell

Requirement: Delete Site Column in SharePoint online using PowerShell

How to delete a site column in SharePoint online?
To remove a SharePoint online site column, follow these steps:
  • Go to Site Settings by clicking Site settings gear and then "Site settings" from the menu.
  • Click on "Site Columns" link from Web Designer Galleries group >> Select the site column to delete.
  • Scroll down and click on "Delete" button and confirm the prompt to delete.
    sharepoint online powershell delete site column
This removes a site column in SharePoint online.

SharePoint online PowerShell to delete site column:
#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 To Delete All Site Columns in a Group
#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
SharePoint Online: Delete Site Column using Powershell SharePoint Online: Delete Site Column using Powershell Reviewed by Salaudeen Rajack on October 22, 2016 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.