Create new document library in SharePoint Online with PowerShell

This is part 7 of 10 where we will be creating a new document library in SharePoint Online. This is part of the following series:

We will be collecting all available web templates in part 1 so we can use this to create a new site in part 2. In part 3 we will be creating a web for the newly created site. We then want to create a couple of site columns in part 4 which we will combine to a content type in part 5. This content type will be added (part 8) to our newly created document library in part 7 using a list template from part 6. After everything is set we will be setting the view in part 9 for this list to show the added columns we got from adding the content type. We only want to set permissions for myself so I’ll will be breaking the inheritance and setting permissions in part 10.

 Create new document library in SharePoint Online

This script will create a new document library in SharePoint Online using where we will using a list template from part 6. We will first start by opening the SharePoint Online Management Shell as administrator which can be downloaded at https://www.microsoft.com/en-us/download/details.aspx?id=35588.

image

You will need to change the first variables to match your Office 365 tenant and copy this bit to PowerShell.

function new-SPOnlineList {
    #variables that needs to be set before starting the script
    $siteURL = "https://spfire.sharepoint.com/sites/BlogDemo"
    $adminUrl = "https://spfire-admin.sharepoint.com"
    $userName = "mpadmin@spfire.onmicrosoft.com"
    $listTitle = "Finance"
    $listDescription = "Finance documents"
    $listTemplate = 101
    
    # Let the user fill in their password in the PowerShell window
    $password = Read-Host "Please enter the password for $($userName)" -AsSecureString
    
    # set SharePoint Online credentials
    $SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $password)
        
    # Creating client context object
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
    $context.credentials = $SPOCredentials
    
    #create list using ListCreationInformation object (lci)
    $lci = New-Object Microsoft.SharePoint.Client.ListCreationInformation
    $lci.title = $listTitle
    $lci.description = $listDescription
    $lci.TemplateType = $listTemplate
    $list = $context.web.lists.add($lci)
    $context.load($list)
    #send the request containing all operations to the server
    try{
        $context.executeQuery()
        write-host "info: Created $($listTitle)" -foregroundcolor green
    }
    catch{
        write-host "info: $($_.Exception.Message)" -foregroundcolor red
    }  
}
new-SPOnlineList

image

You will be asked to enter the password and press enter

image

The list / library has been created in SharePoint Online so we can navigate to the web and verify if the creation was succesfull

image

Tips

You can find more properties for the ListCreationInformation class at https://msdn.microsoft.com/EN-US/library/office/microsoft.sharepoint.client.listcreationinformation_members.aspx.

13 thoughts on “Create new document library in SharePoint Online with PowerShell

  1. Thanks for post,
    I want to create list with list name no space .For example my list created is “Project Risk” then url will be “/Project%20Documents/Forms/AllItems.aspx” . How to set list name like this “ProjectRisk” with no space when create but keep Title = “Project Risk”
    Thanks

  2. I would like to create multiple document libraries, each with a different names, could You modify your script?

    Kind regards,
    Artur

    • Hello Artur,

      You can create a .CSV file with the following properties
      Title;Description;TemplateType

      You can then add the following bit above #create list using ListCreationInformation object (lci)

      $sites = Import-Csv < .CSV location> -Delimiter ;

      and then a for-each loop

      foreach($site in $sites){

      #create list using ListCreationInformation object (lci)
      $lci = New-Object Microsoft.SharePoint.Client.ListCreationInformation
      $lci.title = $site.title
      $lci.description = $site.Description
      $lci.TemplateType = $site.TemplateType
      $list = $context.web.lists.add($lci)
      $context.load($list)
      #send the request containing all operations to the server
      try{
      $context.executeQuery()
      write-host “info: Created $($listTitle)” -foregroundcolor green
      }
      catch{
      write-host “info: $($_.Exception.Message)” -foregroundcolor red
      }
      }

      Hope this helps,

      Kind regards,
      Maarten

  3. Thank You for help and sorry for my english.

    I merged two parts of your code:

    #############################
    function new-SPOnlineList {
    #variables that needs to be set before starting the script
    $siteURL = “https://my.sharepoint.com/sites/name”
    $adminUrl = “https://my-admin.sharepoint.com”
    $userName = “MySharepoint@address”
    # Let the user fill in their password in the PowerShell window
    $password = Read-Host “Please enter the password for $($userName)” -AsSecureString
    # set SharePoint Online credentials
    $SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $password)
    # Creating client context object
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
    $context.credentials = $SPOCredentials
    $sites = Import-Csv -Delimiter ;
    foreach($site in $sites){
    #create list using ListCreationInformation object (lci)
    $lci = New-Object Microsoft.SharePoint.Client.ListCreationInformation
    $lci.title = $site.title
    $lci.description = $site.Description
    $lci.TemplateType = $site.TemplateType
    $list = $context.web.lists.add($lci)
    $context.load($list)
    #send the request containing all operations to the server
    try{
    $context.executeQuery()
    write-host “info: Created $($listTitle)” -foregroundcolor green
    }
    catch{
    write-host “info: $($_.Exception.Message)” -foregroundcolor red
    }
    }
    }
    new-SPOnlineList
    #############################

    Unfortunately the script did not work, I get the following error:

    At C:\powershell\SPsubsite\create-bulk-SP-subsites.ps1:13 char:21
    + $sites = Import-Csv -Delimiter ;
    + ~
    The ‘<' operator is reserved for future use.
    + CategoryInfo : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : RedirectionNotSupported

  4. Sorry, I do not know why this line (csv) above is pasted wrong here – probably after execute script I try to change something in this line…and pasted here with wrong changes.
    but when this error occurred I ran above script with correct line:
    $sites = Import-Csv -Delimiter ;
    When I ran the basic script, without creating csv – everything works well.

    with csv file:

    At C:\powershell\SPsubsite\create-bulk-SP-subsites.ps1:13 char:21
    + $sites = Import-Csv -Delimiter
    + ~
    The ‘<' operator is reserved for future use.
    + CategoryInfo : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : RedirectionNotSupported

  5. I see that this line change here automaticaly always shorten after I click Post Comment.
    $sites = Import-Csv -Delimiter ;

  6. Hi,
    I think this script can save me so much time, especially with the import CSV option.
    Can you indicate how to adjust the script to use a Library Template?

    Thank you so much for sharing your expertise

    • Hi Eric,

      Thank you for your question.
      It is possible to get the custom list templates by changing the code from http://www.sharepointfire.com/2016/01/get-sharepoint-online-list-templates-powershell/ .
      Change the code below “Creating client context object” to:

      # Creating client context object
      $context = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
      $context.credentials = $SPOCredentials

      $site = $context.site
      $context.load($site)
      $context.executequery()

      $web = $context.web
      $context.load($web)
      $context.executequery()

      $listTemplates = $site.getcustomlisttemplates($web)
      $context.load($listTemplates)
      $context.executeQuery()

      #List available templates
      $listTemplates

      It is unfortunately not yet possible to create a list based on this custom template. You will just get a normal list using the featureID and the ListTemplate from the custom template.
      If the template is not that complex you can for example add content types, change views etc using other CSOM commands after the list has been created.

      Kind regards,
      Maarten

Leave a Reply

Your email address will not be published. Required fields are marked *