Add new column(s) to all Site Collection Subsites

Rob Smith 1 Reputation point
2020-10-07T16:10:03.91+00:00

Hi Guys

Is there a way using PowerShell, to add new columns (potentially a few) to all sub sites? Or maybe list the URLs I need to add them to? By bulk anyway.

The lists and columns already in place are just local ones in the subsites, not site level (old build, but alot of data)

Thanks!

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,567 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Allen Xu_MSFT 13,821 Reputation points
    2020-10-08T09:43:09.817+00:00

    Hi @Rob Smith ,

    Firstly, I want to confirm what do you want to list, all site collections and subsites in the whole SharePoint tenant or the subsites under a site collection?

    If you want to create a list of all site collections and subsites in your whole SharePoint tenant, Using SharePoint Online Management Shell that contains Get-SPOSite cmdlet you could retrieve information about site collections as demonstrated below for SharePoint Online (SPO):

    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"   
      
    $AdminUrl = "https://tenant-admin.sharepoint.com/"  
    $UserName = "username@tenant.onmicrosoft.com"  
    $Password = "password"  
    $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force  
    $Credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $SecurePassword  
    $SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)  
      
    function Get-SPOWebs(){  
    param(  
       $Url = $(throw "Please provide a Site Collection Url"),  
       $Credential = $(throw "Please provide a Credentials")  
    )  
        
      $context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)    
      $context.Credentials = $Credential   
      $web = $context.Web  
      $context.Load($web)  
      $context.Load($web.Webs)  
      $context.ExecuteQuery()  
      foreach($web in $web.Webs)  
      {  
           Get-SPOWebs -Url $web.Url -Credential $Credential   
           $web  
      }  
    }  
      
    #Retrieve all site collection infos  
    Connect-SPOService -Url $AdminUrl -Credential $Credentials  
    $sites = Get-SPOSite   
      
    #Retrieve and print all sites  
    foreach ($site in $sites)  
    {  
        Write-Host 'Site collection:' $site.Url       
        $AllWebs = Get-SPOWebs -Url $site.Url -Credential $SPOCredentials  
        $AllWebs | %{ Write-Host $_.Title }     
        Write-Host '-----------------------------'   
    }   
      
    $AllWebs = Get-SPOWebs -Url 'https://tenant.sharepoint.com' -Credential $SPOCredentials  
    $AllWebs | %{ Write-Host $_.Title }  
    

    Note: please update the $AdminUrl, $UserName, $Password variables with your SharePoint online environment.

    After Successful execution of the scripts we will get the message like below:
    30933-1.png

    If you want to list all subsites under a site collection, you can refer to this article:
    Get All Lists details in a site collection Using Power shell in SharePoint Online

    Then, you can create site column(s) in a site collection level and all of his subsites will inherit the site column(s).
    About how to bulk creation of SharePoint Site Columns via Powershell, you can refer to the scripts below:

    #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"  
        
    #Parameters  
    $SiteURL="https://xxxx.sharepoint.com"  
    $ColumnName="xxxx"  
    $IsRequired = "TRUE"  
    $ColumnGroup="Custom Columns"  
       
    Try {  
        $Cred= Get-Credential  
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)  
       
        #Setup the context  
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)  
        $Ctx.Credentials = $Credentials  
       
        #Get all Site columns from the site  
        $Fields = $Ctx.web.Fields  
        $Ctx.Load($Fields)  
        $Ctx.executeQuery()  
       
        #Check if the column name exists  
        $NewField = $Fields | where {$_.Title -eq $ColumnName}  
        if($NewField -ne $NULL)   
        {  
            Write-host "Site Column $ColumnName already exists!" -f Yellow  
        }  
        else  
        {  
            #Define XML for Field Schema  
            $FieldSchema = "<Field Type='Text' DisplayName='$ColumnName' Name='$ColumnName' required='$IsRequired' Group='$ColumnGroup'/>"  
            $NewField = $Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)  
            $Ctx.ExecuteQuery()     
       
            Write-host "Site Column Created Successfully!" -ForegroundColor Green   
        }  
    }  
    Catch {  
        write-host -f Red "Error Creating Site Column!" $_.Exception.Message  
    }  
    

    Note: please update the $SiteURL, $ColumnName, $IsRequired,$ColumnGroup variables with your SharePoint online environment.

    If you are still confused about this, please feel free to let me know.


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.