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 folder in document library in SharePoint 2013, use this PowerShell script:

$folder = $list.Folders.Add("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, "New")
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!
create folder in sharepoint document library powershell

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

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")
  #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")
Create Folder using CSOM in SharePoint Online: 
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/"
$ListURL="Shared Documents"
$FolderName="New Folder"
$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

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

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.

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