Upload to SharePoint using PowerShell in Build and Release Pipelines

This task will enable you to upload file(s) to a SharePoint Document library from a Build or a Release Pipeline.

Pre-requisite

  1. Azure DevOps Pipeline
  2. PowerShell Task
  3. Azure Key Vault
  4. SharePoint Site

Setup

  1. Create the following Variables for your pipeline

    Variable Description
    $SPUsername The user account using which you will connect to SharePoint. It is recommended to use Variable Groups connected to Azure Key Vault to store credentials.
    $SPPassword You may need to use App Passwords instead of password for Multi-factor authentication (MFA). Generate App password using this link. It is recommended to use Variable Groups connected to Azure Key Vault to store credentials.
    $SPURL The SharePoint site URL
    $SPFolder SharePoint Library folder where you want to upload the files to
    $Folder Folder from which you want to upload files. This can be the release or build staging directory also.
  2. Add a new PowerShell task to your Pipeline

    Classic

    Add PowerShell task

    YAML

    Add PowerShell task YAML

  3. You can use this script as an Inline Script or from a File Path. Use one of the following script depending on your selection

    1. Inline Script: script
    2. File Path: script
  4. Call the script

    Inline

    Replace the variables enclosed in curly braces { VariableName } with your variables

    Add-FilesToSP -Version "Online" -Username { SPUserName } -Password { SPPassword } -URL { SPURL } -SPFolder { SPFolder } -Folder { Folder }
    

    It should look something like this

    Add-FilesToSP -Version "Online" -Username $SPUsername -Password $SPPassword -URL $SPURL -Folder $Folder
    

    File Path

    Pass the variables as arguments after selecting the script file

    -SPVersion "Online" -SPUsername $SPUsername -SPPassword $SPPassword -SPURL $SPURL -SPFolder $SPFolder -Folder $Folder
    

    Classic

    Add Script Classic

    YAML

    - task: PowerShell@2
      inputs:
        filePath: '.\uploadToSharePointInFilePath.ps1'
        arguments: '-SPVersion "Online" -SPUsername $SPUsername -SPPassword $SPPassword -SPURL $SPURL -SPFolder $SPFolder -Folder $Folder'
    

    Add Script YAML

For Version parameter Online is for SharePoint Online, 2013 for SharePoint 2013, 2016 for SharePoint 2016 and 2019 for SharePoint 2019

Output

Pipeline Logs

logs

Uploaded File

file

Feel free to customize the script to suit your needs, the script provided here serves only as template.