Skip to main content

Adding SharePoint Folders Programmatically Online via PowerShell

By March 28, 2017September 16th, 2020Blog, Office 365, Office 365, SharePoint
Share Point Logo

As with most businesses, with the new year comes documentation and internal paperwork that refers to 2017, instead of 2016. Part of this process requires us to create new 2017 SharePoint folders to keep track of client proposals, quotes, and contracts signed in the new year.

This is the first time we have had to deal with this situation since we moved to SharePoint Online last year. While the transition to SharePoint Online was painful, to say the least, I was assured by our Microsoft representatives that SharePoint Online would be a vast improvement, not the least of which is due to the extensive API provided by the Client-Side Object Model (CSOM).

While the CSOM is not as intuitive to navigate as I’d hoped, after a few weeks of dithering with various methods available in the API, I was able to successfully add 2017 folders in all the necessary document libraries for all our clients. Since we have hundreds of clients, doing this procedure by hand would have been impossible.

Adding a folder to a SharePoint document library is broken down into a few simple steps:

[NOTE: Before proceeding make sure you are a global administrator for your SharePoint online site collection]

  1. Connect to the SharePoint Online site
  2. Find the document library you want to alter
  3. Add your new folder

1)  Connecting to SharePoint Online

Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking

$Username = Read-Host -Prompt “Please enter your username”

$Password = Read-Host -Prompt “Please enter your password” -AsSecureString

$Site = “https://sharepoint_site.com/subsite”

$Context = New-Object Microsoft.SharePoint.Client.ClientContext($Site)

$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username,$Password)

$Context.Credentials = $Creds

——-> at this point you have connected to the sharepoint site located at https://sharepoint_site.com/subsite (subsites are optional if you’re trying to work on the root of the site collection)

2)  Find the Right Document Library

$Web = $Context.Web

$Context.Load($Web)

$Context.ExecuteQuery()

——-> the “web” object is the top of the data hierarchy of this subsite, all my clients are stored as subsites underneath this one, so I have to fetch the “webs” object which allows me to get to each client individually

$Webs = $Web.Webs

$Context.Load($Webs)

$Context.ExecuteQuery()

——-> now I have the “webs” object which contains all the subsites of my client site (which is itself a subsite of the main site collection)

foreach ($ClientSite in $Webs) {

$Context.Load($ClientSite)

$Context.ExecuteQuery()

——-> this loads the client site

$ClientRootFolder = $ClientSite.RootFolder

$Context.Load($ClientRootFolder)

$Context.ExecuteQuery()

——-> this loads the root of the folder tree for the client site, and will allow me to get access to folders in the Sales document library

$ClientFolders = $ClientRootFolder.Folders

$Context.Load($ClientFolders)

$Context.ExecuteQuery()

——-> this loads all the document libraries under the client, so I can find the one called “Sales”

foreach ($Folder in $ClientFolders) {

$Context.Load($Folder)

$Context.ExecuteQuery()

if ($Folder.Name -like “Sales”) {

$SalesFolders = $Folder.Folders

$Context.Load($SalesFolders)

$Context.ExecuteQuery()

——-> once I find the folder called “Sales” I can load all the Sales folders which will allow me to create a new one

3)  Create a New Folder

$NewSalesFolder = $SalesFolders.Add(“2017”)

$Context.Load($NewSalesFolder)

$Context.ExecuteQuery()

——-> this final command executes the action to create the new folder, and now we’re in business!  

}

}

}

This is a simple PowerShell script to do a very simple task. SharePoint Online CSOM can be tricky at times, but if you stick with it this can be a powerful tool to programmatically manipulate the large data sets that many companies store in their SharePoint Online site collections.

I hope this trick helps you solve some internal scripting challenges of your own!

Allison Sousa, Director of Managed Services, PEI

 

 

Leave a Reply