SharePoint Online: PowerShell to Create Document Library

Requirement: Create a Document Library in SharePoint Online using PowerShell.

SharePoint Online: PowerShell to Create Document Library:
The document library is the most commonly used type of library in SharePoint Online used to store and collaborate Office documents such as Word, Excel, PowerPoint, etc. SharePoint Online document libraries comes with default metadata properties: "Type", "Name", "Modified" and "Modified By" and can be extended based on our requirements. Alright, How to Create document library in SharePoint online 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/Sites/Sales"
$LoginName ="Salaudeen@Crescent.OnMicrosoft.com"
$LoginPassword ="Password" 

#Get the Client Context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)

#supply Login Credentials
$SecurePWD = ConvertTo-SecureString $LoginPassword –asplaintext –force  
$Credential = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($LoginName,$SecurePWD)
$Context.Credentials = $Credential

#Create new document library
$ListInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$ListInfo.Title = "Project Docs"
$ListInfo.TemplateType = 101 #Document Library
$List = $Context.Web.Lists.Add($ListInfo)
$List.Description = "Repository to store project artifacts"
$List.Update()
$Context.ExecuteQuery()

write-host "New Document Library has been created!"
and the result:
sharepoint online powershell create document library
Lets add some error handling to the above code

How to Create a Document Library in SharePoint Online using PowerShell?
Here is the PowerShell for SharePoint Online to create a document library 
#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"
  
Function Create-DocumentLibrary()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $DocLibraryName
    )    
    Try {
    #Setup Credentials to connect
    $Cred = Get-Credential
    $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

    #Get All Lists from the web
    $Lists = $Ctx.Web.Lists
    $Ctx.Load($Lists)
    $Ctx.ExecuteQuery()

    #Check if Library name doesn't exists already and create document library
    if(!($Lists.Title -contains $DocLibraryName))
    { 
        #sharepoint online powershell create list
        $ListInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
        $ListInfo.Title = $DocLibraryName
        $ListInfo.TemplateType = 101 #Document Library
        $List = $Ctx.Web.Lists.Add($ListInfo)
        $List.Update()
        $Ctx.ExecuteQuery()
 
        write-host  -f Green "New Document Library has been created!"
    }
    else
    {
        Write-Host -f Yellow "List or Library '$DocLibraryName' already exist!" 
    }
}
Catch {
    write-host -f Red "Error Creating Document Library!" $_.Exception.Message
}
}

#Set Parameters
$SiteURL= "https://crescent.sharepoint.com/"
$DocLibraryName="Project Docs"

#Call the function to create document library
Create-DocumentLibrary -siteURL $SiteURL -DocLibraryName $DocLibraryName
Related post: How to create a list in SharePoint Online using PowerShell?
SharePoint Online: PowerShell to Create Document Library SharePoint Online: PowerShell to Create Document Library Reviewed by Salaudeen Rajack on November 20, 2016 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.