Create List On SharePoint From Azure Runbook Using PnP PowerShell

Introduction
 
In this article, you will see, how we can automate SharePoint list creation mechanism, using Azure Runbook jobs with PnP PowerShell.
 
Steps Involved
  1. Creating Azure Automation Account.
  2. Importing PnP PowerShell references as module.
  3. Adding credentials.
  4. Create Runbook with the code.
  5. Test/Schedule.
  6. View the job results.
Note- Creation of an automation account, importing modules, adding credentials are explained in the links of articles, given below-
Here, I will explain more about implementing the logic to create a list. 
 
The snapshot, given below, shows the credentials added to the Azure automation portal.
 
 
 
The snapshot, given below, shows the required PnP PowerShell module imported to the gallery.
 
 
 
Create Runbook and Schedule
  1. First, we will create a Runbook, using PowerShell Workflow type. PowerShell Workflow type is used to implement the complex logic. The inputs can be passed through the parameters, using PowerShell Workflow type.



  2. Once created, we can view the script editor of Runbook.



  3. For creating a list, we need the necessary input parameters set. The required input parameters can be-

    • Stored Credentials name.
    • Target SharePoint Site URL. 
    • List Name.
    • List Template Name.

      These parameters should be defined at the start of the Workflow method inside the Param() component. The code snippet, given below, shows the parameters defined.
      1. Param(      
      2.     [Parameter(Mandatory=$true)][string]      
      3.     $CredentialsName,      
      4.     [Parameter(Mandatory=$true)][string]      
      5.     $SiteUrl,      
      6.     [Parameter(Mandatory=$true)][String]      
      7.     $ListName,      
      8.     [Parameter(Mandatory=$true)][String]      
      9.     $ListTemplate                     
      10. )      
  4. Following the parameters component, the necessary code snippet should be written to create a list.

    • Get the credentials stored on the automation account.
    • Connect to SharePoint online portal, using the input parameters site URL and credentials.
    • Create a new list with the list name and list template type.

      The code snippet, given below, helps to create a list on SharePoint Online from Azure Runbook. The logic is very similar to that of normal PowerShell execution.

      1. $SPCredentials = Get-AutomationPSCredential -Name $CredentialsName      
      2. Connect-SPOnline -Url $SiteUrl -Credential $SPCredentials      
      3. # Creates new list        
      4. Write-Output "Creating List"      
      5. New-SPOList -Title $ListName -Template $ListTemplate      
      6. Write-Output "Task Completed"      
      The snapshot, given below, shows the editor with the code snippet added-


       
  5. In this case, I will test Runbook, using test pane. Click the test pane under the editor. Provide the necessary credentials. Run the book by clicking start under test pane.

     

  6. After sometimes, the test will complete. The results can be monitored from the test pane.

     

  7. Now, navigate to SharePoint online site to check the created list. The snapshot, given below, shows the specified custom list created on the site.

     

  8. If required, you can publish Runbook and create necessary schedules. Also, based on the requirements, you can call or run the book at the specified schedules. The article's link, given below, explains about publishing Runbook and scheduling jobs for Runbook.

Note- Runbook can be made as Webhooks, which will result in accessing Runbook, using http calls.  
 
Summary
 
Thus, you have learned, how to create a list on SharePoint online sites, using Azure automation Runbook. Using created Runbook, the necessary schedules can be created and the jobs can be monitored from Runbook.

Up Next
    Ebook Download
    View all
    Learn
    View all