Overview

This article will give you the basic idea about how to get data from a SharePoint List using Azure HTTP PowerShell Function App with the help of userid and password.

Steps

  1. If you set up a new Azure Environment, then first we need to install SharePointPnPPowerShellOnline
    • Open cloud shell

      Azure HTTP PowerShell Function App To Get Data Using Credentials
    • Run command “Install-Module -Name SharePointPnPPowerShellOnline”

      Azure HTTP PowerShell Function App To Get Data Using Credentials
  2. Go to Function App, if function is already Available, we can use the same or we can create a new one. To create a new Function within Function App or new Function App refer here.
  3. Go to Platform Features of Function App, scroll down and click on Advanced Tools (Kudu)

    Azure HTTP PowerShell Function App To Get Data Using Credentials
  4. Click on Debug console and select CMD

    Azure HTTP PowerShell Function App To Get Data Using Credentials
  5. Select Site ==> wwwroot ==> click on Function Name folder ==> Add new folder [modules] ==> Add new folder [SharePointPnPPowerShellOnline]
    Under this folder Upload All PnP PowerShell related files.
    Note
    You can download package from the below URL

    https://www.powershellgallery.com/packages/SharePointPnPPowerShellOnline/3.21.2005.2
    Azure HTTP PowerShell Function App To Get Data Using CredentialsAzure HTTP PowerShell Function App To Get Data Using Credentials
  6. Go to the Function GetData and add the below code.
    1. using namespace System.Net
    2. Import-Module D:\home\site\wwwroot\GetData\modules\SharePointPnPPowerShellOnline\Newtonsoft.Json.dll'
    3. Write-Host "Powershell http trigger"
    4. $requestBody = Get-Content $req -Raw | ConvertFrom-Json
    5. $serviceaccount = $env:Userid
    6. $serviceaccountpassword = $env:Password
    7. $listname = "[listname]"
    8. $siteurl = "https://[tenantname].sharepoint.com/sites/[siteurl]"
    9. $secpassword = ConvertTo-SecureString -String $serviceaccountpassword -AsPlainText -Force
    10. $cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $serviceaccount, $secpassword
    11. Connect-PnPOnline -Url $siteurl -Credentials $cred
    12. $web = Get-PnPWeb
    13. $title = $web.Title
    14. $listitems = Get-PnPListItem -List $listname
    15. $listArray = New-Object System.Collections.Generic.List[System.Object]
    16. foreach ($item in $listitems)
    17. {
    18. $listArray.Add([hashtable]@{
    19. DisplayTitle=$item["Title"];
    20. Id= $item["ID"]
    21. }
    22. )
    23. }
    24. $json = $listArray | ConvertTo-Json
    25. Out-File -Encoding Ascii -FilePath $res -inputObject $json
Note
Please update tenantname, siteurl, listname

Sumamry

Using Azure Function App we can use SharePoint Data to Open source, any HTML or other system.
Note
This code will work if multifactor authentication is not enabled on specified Userid.
If multifactor authentication is enabled then refer to the next blog.