Share via

What could be causing the 401 Unauthorized error when executing the Get-PnPList command?

Anonymous
2024-11-05T07:44:13+00:00

I am using the powershell script shared below.
However, I am encountering a 401 Unauthorized error when executing Get-PnPList command.

This error is causing the script to stop rather execution.

Error: Get-PnPList cmd is not connect, showing the (401) Unauthorized error and then script is stopped without excuting the other below code.

please give the solution to solving this query, it will be very helpful to us.

Connect to the SharePoint site using Client ID and Client Secret

$siteUrl = "https://.sharepoint.com/sites"
$clientId = "................................."
$clientSecret = "...................................."

Authenticate with SharePoint Online

Connect-PnPOnline -Url $siteUrl -ClientId $clientId -ClientSecret $clientSecret

SharePoint List and Field Names

$listName = "BlueCurrent_10_items"
$addressField = "field_13" # Internal name of the address field
$duplicateAddressField = "DuplicateAddress" # Internal name of the flag field (True/False)

# Check if the list exists $list = Get-PnPList -Identity $listName -ErrorAction SilentlyContinue if (-not $list) {     Write-Host "The list '$listName' does not exist at the site '$siteUrl'." }

Retrieve all items in the list (in batches if more than 5000 items exist)

$batchSize = 500
$allItems = @()
$hasMoreItems = $true

Get all list items in batches to handle large lists

while ($hasMoreItems) {
    $items = Get-PnPListItem -List $listName -PageSize $batchSize -Fields $addressField, $duplicateAddressField
    $allItems += $items

    # Check if the number of items retrieved is less than the batch size
    if ($items.Count -lt $batchSize) {
        $hasMoreItems = $false
    }
}

Group items by Address (trimmed, case-insensitive) to find duplicates

$groupedByAddress = $allItems | Group-Object -Property {
    ($_.FieldValues[$addressField] -as [string]).Trim().ToLower()
}

Debug: Show groups to check duplicates

foreach ($group in $groupedByAddress) {
    Write-Host "Group: Address = $($group.Name), Count = $($group.Count)"
}

Loop through each group and check for duplicates

foreach ($group in $groupedByAddress) {
    if ($group.Count -gt 1) {
        Write-Host "Duplicates found for address: $($group.Name) - Count: $($group.Count)"

        # Mark **all** items in the group as duplicates
        foreach ($item in $group.Group) {
            # Update the DuplicateAddress field to True for all duplicate items
            Set-PnPListItem -List $listName -Identity $item.Id -Values @{$duplicateAddressField = $true}
            Write-Host "Updated item with ID $($item.Id) - DuplicateAddress set to True"
        }
    }
}

Write-Host "Duplicate Address check complete."

Thanks.

Microsoft 365 and Office | SharePoint | Other | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

4 answers

Sort by: Most helpful
  1. Anonymous
    2024-11-05T13:58:21+00:00

    Get-PnPList uses CSOM under the hood, which is the same as saying SharePoint API. When using an Entra ID app-only to connect to SharePoint API you need to authenticate using a Certificate. The Secret ID is not supported, even though you can authenticate and get the access token the SPO API will throw 401 error.

    You can learn more here: Granting access via Azure AD App-Only | Microsoft Learn

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2024-11-06T13:25:46+00:00

    Hi Jesika,

    Thank you for your patience. Since you’ve tried all the suggestions and are still encountering the 401 Unauthorized error, I recommend posting your query in the PnP PowerShell community. The experts there can provide more specialized support and might have encountered similar issues before.

    You can post your question in the PnP PowerShell GitHub repository or the SharePoint developer community forum.

    Apologies for redirecting you to different community as the members in the category posted focus on the users with the Microsoft 365 concern and have limited knowledge on the PnP powershell, so to get the fast and better assistance, we have redirected you in the correct path.

    Appreciate your patience and understanding. Have a great day!

    Best regards

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2024-11-06T06:10:10+00:00

    Thanks for reply,

    I have try all the alternatives given by you, and still the same error is coming.

    can you please guide as with any suggestion or another code.

    This will be really appreciating and helpful for us.

    Thanks!

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2024-11-05T12:37:50+00:00

    Dear Jesika Maurya,

    Thank you for reaching out and providing the details of the issue you're encountering with the Get-PnPList command. While we do not support the PnP PowerShell module directly, we would suggest you posting your query under PnP powershell commnity to have better support. However, since you already posted here, I can offer some suggestions that may help resolve the 401 Unauthorized error

    The 401 Unauthorized error typically indicates that there is an issue with the authentication process. Below are some common reasons and potential solutions that might help resolve the issue:

    Incorrect Credentials or Permissions

    • Ensure that the Client ID and Client Secret you're using are correct and valid.
    • Double-check that the App Registration in Azure AD (associated with the Client ID and Secret) has been granted the correct permissions to access the SharePoint site.
      • You may need the Sites.Read.All permission at the tenant level for the application to read data from SharePoint.
      • Verify that the app is granted consent to these permissions, either by an admin or through the Azure portal.

    Client ID and Secret Format

    • Ensure there are no extra spaces or special characters at the beginning or end of the Client ID or Secret.
    • Try manually re-generating the Client Secret in Azure AD, and update the script with the new one.

    Ensure Correct SharePoint URL

    • Confirm that the $siteUrl you're using is accurate. Double-check that the URL matches the exact site where your list is located. For example:bash $siteUrl = "https://contoso.sharepoint.com/sites/yoursite"

    Token Expiry or Cache Issues

    • If you have previously authenticated using the same ClientId and ClientSecret, the token might have expired, or there could be caching issues. Try running the script after clearing any cached tokens:powershell Clear-PnPConnection

    Check for Multi-Factor Authentication (MFA)

    • If the SharePoint site requires MFA for any user actions, ensure that the application registration is not affected by MFA requirements. Service accounts and apps used in automation should typically be excluded from MFA, or else you might encounter this error.

    Test Authentication Manually

    • You could try testing authentication separately using:powershell Connect-PnPOnline -Url $siteUrl -ClientId $clientId -ClientSecret $clientSecret
    • This helps isolate whether the issue is with the connection or the Get-PnPList command specifically.

    Verbose Logging for Debugging

    • Use the -Verbose flag when running Connect-PnPOnline to get more detailed output and see if it provides additional insights into where the issue is occurring.powershell Connect-PnPOnline -Url $siteUrl -ClientId $clientId -ClientSecret $clientSecret -Verbose

    Testing with Alternative Authentication Methods

    • If you're still facing issues, consider testing with Interactive Authentication (for troubleshooting purposes):powershell Connect-PnPOnline -Url $siteUrl -Interactive

    Once you've checked these areas, try rerunning your script to see if the issue persists.

    At last, if above suggestions did not resolve the problem, please post in PnP community as we discussed before.

    Best regards,

    Sophia

    Was this answer helpful?

    0 comments No comments