Tuesday, January 20, 2015

SharePoint 2013: PowerShell to Create a Folder in Document Library

How to add folder in SharePoint Document Library using PowerShell:
To create folders inside document library, Use:

    $folder = $list.Folders.Add("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, $FolderName)
    $folder.Update(); 

The above codes doesn't provide much fault tolerance, isn't it?
  • What if the particular library we are looking for doesn't exists?
  • What if the folder we are trying to add already exists on the library?
Error! So lets re-write the code in PowerShell with little fault tolerance!

How to Create Folders and Sub-Folders in SharePoint List using PowerShell?

Here is the PowerShell code snippet for creating Folders and Sub-Folders inside SharePoint List or Library:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Get the Web
$web=Get-SPWeb "http://sharepoint.company.com"
#Get the List/Library
$list=$web.Lists.TryGetList("SalesList")

if($list)  #Check If List exists!
 {
  # Create a Folder "Sales Documents"
  # Check Folder Doesn't exists in the Library!
  $folder = $list.ParentWeb.GetFolder($list.RootFolder.Url + "/" +"Sales List");
  #sharepoint powershell check if folder exists
  if ($folder.Exists -eq $false)
   {
    #Create a Folder
    $folder = $list.AddItem("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "Sales Sub-Item")
    $folder.Update();
   }
   
  #Create a Sub-Folder "APAC Sales Documents" inside "Sales Documents"
  #Check if sub-folder doesn't exists already
  $Subfolder = $list.ParentWeb.GetFolder($folder.URL + "/" + "APAC Sales Documents");
  if ($Subfolder.Exists -eq $false)
   {
    #Create a Sub-Folder Inside "Sales Documents"
    $Subfolder = $list.AddItem($folder.URL, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "APAC Sales Documents")
    $Subfolder.Update();
   }
 }
Create Folder using CSOM in SharePoint Online:
#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/"
$ListURL="Shared Documents"
$FolderName="New Folder"
$UserName="Admin@crescent.com"
$Password ="Password goes here"
 
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
 
#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.Load($Folder)
$ParentFolder.Context.ExecuteQuery()

Write-host "New Folder Created Successfully!" 

Tags:sharepoint powershell folders add, sharepoint 2013 powershell add folder to document library, sharepoint 2013 powershell add folder, sharepoint powershell create folder,  sharepoint powershell create subfolder, sharepoint powershell create folder in document library, sharepoint 2010 powershell create folder document library, sharepoint create folder in list powershell, sharepoint powershell new folder



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...