Saturday, August 20, 2016

SharePoint Online: Create a Folder using PowerShell-CSOM

Create folder in SharePoint Online document library
Folders are used to organize files in SharePoint, similar to what we do in our computer. You can add a folder to SharePoint online list or library by following below steps.

How to Create a folder in SharePoint Online?
To create a folder in SharePoint online, Follow these steps:

  • Login to your SharePoint online site, Navigate to your document library in which you want to create a folder.
  • On the ribbon, click on Files tab (Items tab, if its a list, instead of library)
  • Click New Folder
    create new folder sharepoint online
  • In the Create A New Folder window, enter the folder name and click Save!
    sharepoint online create folder powershell
Now you can see your new folder in the Document Library. Please note, creating a folder is less preferred compared with adding a meta-data column to classify data in SharePoint!

How to Enable the "New Folder" option?
What if the New Folder option is grayed out? If the New Folder button isn't available, you can enable it.
  • Navigate to the List or Library setting >> click Advanced settings.
  • In the Folder section, Click "Yes" option to Make "New Folder" option available.
  • Click OK to save your changes.
Now, you should get "New folder" in the SharePoint list 

SharePoint Online: Create Folder using PowerShell-CSOM: 
Lets create folder in SharePoint document library using 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"
  
##Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/"
$ListName="Shared Documents"
$FolderName="Reports"
$UserName="salaudeen@crescent.com"
$Password ="Password Goes here"
 
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))

Try { 
    #Set up the context
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl) 
    $Context.Credentials = $credentials
  
    #Get the List Root Folder
    $ParentFolder=$Context.web.GetFolderByServerRelativeUrl($ListName)

    #Create New Folder
    $Folder = $ParentFolder.Folders.Add($FolderName)
    $ParentFolder.Context.ExecuteQuery()

    Write-host "New Folder Created Successfully!" -ForegroundColor Green
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
} 

Create Sub-Folder at given path using PowerShell:
We can add a folder or sub-folder to any existing library or folder using PowerShell.
    #Set up the context
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl) 
    $Context.Credentials = $credentials

    #Create New Sub-Folder
    $Folder=$Context.Web.Folders.Add("Shared Documents/Reports/V2")
    $Context.ExecuteQuery()

    Write-host "Folder Created at: " $Folder.ServerRelativeUrl -ForegroundColor Green



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...