Forum Discussion
Get-PnpListItem from Sharepoint Online list giving 401 error
I have a Powershell script that has worked in the past, but I am now having unexplained problems with. About a month ago was the last time I used it successfully. The relevant section failing is:
Connect-AzAccount
$token = Get-AzAccessToken -ResourceUrl "https://contoso.sharepoint.com"
Connect-PnPOnline -AccessToken $token.Token -Url "https://contoso.sharepoint.com/sites/Team/"
Get-PnpListItem -List DIDs -Query "<View><ViewFields><FieldRef Name='ID'/><FieldRef Name='field_DID'/></ViewFields><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>Open</Value></Eq></Where></Query></View>" | Sele
The list called DIDs does exist and the account I am authenicating with has full control on this list. I can pull it up in a web browser with no issues. When I try to open it in Powershell using the Get-PnpListItem, I get the following:
Get-PnpListItem : The remote server returned an error: (401) Unauthorized.
At line:4 char:1
+ Get-PnpListItem -List DIDs -Query "<View><ViewFields><FieldRef Name=' ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (:) [Get-PnPListItem], WebException
+ FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Lists.GetListItem
Tried different accounts with access and got the same result. Not sure what is wrong or changed.
- LeonPavesicSilver Contributor
Hi jlentz927,
The 401 error typically is there when there is an issue with authentication or authorization. You can troubleshoot the error following these stepsVerify Azure AD authentication: check if the Azure AD authentication setup is accurate. Check the validity of the Azure AD application registration and permissions. You can also try to re-register the application and configure the necessary permissions to ensure they have not expired or been modified.
Check SharePoint Online permissions: check that the account used for authentication has appropriate permissions in SharePoint Online. Verify that it has at least read access to the relevant site and list where the Get-PnpListItem cmdlet is being used.
Validate SharePoint Online URL: Double-check the accuracy of the URL used in the Connect-PnPOnline cmdlet. Ensure it correctly points to the intended SharePoint Online site and subsite where the DIDs list is located. Alao, ensure the URL is accessible and not restricted by network or firewall.
Test different authentication methods: Explore alternative authentication methods if you are currently using the Az module. For example, you can use the SharePoint Online Management Shell and the Connect-SPOService cmdlet to connect to SharePoint Online instead.
- Update PnP PowerShell module: Keep your PnP PowerShell module up to date.
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic