Handling ExecuteQuery 403 Error in Powershell script

%3CLINGO-SUB%20id%3D%22lingo-sub-1131182%22%20slang%3D%22en-US%22%3EHandling%20ExecuteQuery%20403%20Error%20in%20Powershell%20script%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1131182%22%20slang%3D%22en-US%22%3E%3CP%3EHey%20everyone%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20a%20rookie%20for%20PowerShell%20and%20currently%20working%20on%20a%20script%20to%20build%20up%20a%20connection%20from%20a%20CSV%20file%20to%20SharePoint%20list.%20I%20get%20this%20error%20below%20even%20it%20runs%20ExecuteQuery()%20when%20I%20have%20already%20included%20microsoft.sharepoint.client.dll%20and%26nbsp%3BMicrosoft.SharePoint.Client.Runtime.dll.%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F167536i3AE453A4FD0899CF%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20alt%3D%22clipboard_image_0.png%22%20title%3D%22clipboard_image_0.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere%20is%20my%20code%20snippet%2C%20any%20input%20would%20help%20%3A)%3C%2Fimg%3E%20Thank%20you%20all%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%23CSOM%0AAdd-Type%20-path%20'C%3A%5CProgram%20Files%5CCommon%20Files%5CMicrosoft%20Shared%5CWeb%20Server%20Extensions%5C16%5CISAPI%5CMicrosoft.SharePoint.Client.Runtime.dll'%0AAdd-Type%20-path%20'C%3A%5CProgram%20Files%5CCommon%20Files%5CMicrosoft%20Shared%5CWeb%20Server%20Extensions%5C16%5CISAPI%5CMicrosoft.SharePoint.Client.dll'%0A%23connect%20to%20Sharepoint%0A%24credentials%20%3D%20Get-Credential%20-Message%20%22Please%20Enter%20SharePoint%20Online%20credentials%22%3B%0A%24Site%20%3D%20%22https%3A%2F%2Fxyz.sharepoint.com%2F%22%3B%0AConnect-PnPOnline%20-Url%20%24Site%20-Credentials%20%24credentials%3B%0A%24listName%20%3D%20%22test%22%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%23import%20csv%0A%24csv%3DImport-CSV%20%22C%3A%5CUsers%5CLiLin-aretoconsultin%5CDesktop%5Cpowerapps%5Ccsv%5Ctest.csv%22%3B%0A%3CBR%20%2F%3E%23site%20contents%20access%0A%24cxt%20%3D%20New-Object%20Microsoft.SharePoint.Client.ClientContext(%24Site)%0A%24web%20%3D%20%24cxt.Web%0A%24List%20%3D%24web.Lists.GetByTitle(%24listName)%0A%24import%20%3D%20%5BMicrosoft.SharePoint.Client.CamlQuery%5D%3A%3ACreateAllItemsQuery()%0A%24item%20%3D%20%24List.GetItems(%24import)%0A%0A%24cxt.Load(%24web)%0A%24cxt.load(%24item)%0A%24cxt.ExecuteQuery()%26nbsp%3B%23%26nbsp%3B%26lt%3B-%26nbsp%3Bthis%26nbsp%3Bgives%26nbsp%3Berror%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1131182%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESharePoint%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Highlighted
Occasional Visitor

Hey everyone,

 

I am a rookie for PowerShell and currently working on a script to build up a connection from a CSV file to SharePoint list. I get this error below even it runs ExecuteQuery() when I have already included microsoft.sharepoint.client.dll and Microsoft.SharePoint.Client.Runtime.dll. 

clipboard_image_0.png

 

Here is my code snippet, any input would help :) Thank you all

 

#CSOM
Add-Type -path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
Add-Type -path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'
#connect to Sharepoint
$credentials = Get-Credential -Message "Please Enter SharePoint Online credentials";
$Site = "https://xyz.sharepoint.com/";
Connect-PnPOnline -Url $Site -Credentials $credentials;
$listName = "test";

#import csv $csv=Import-CSV "C:\Users\LiLin-aretoconsultin\Desktop\powerapps\csv\test.csv";
#site contents access $cxt = New-Object Microsoft.SharePoint.Client.ClientContext($Site) $web = $cxt.Web $List =$web.Lists.GetByTitle($listName) $import = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery() $item = $List.GetItems($import) $cxt.Load($web) $cxt.load($item) $cxt.ExecuteQuery() # <- this gives error

 

1 Reply
Highlighted

@manofiron068 Try modifying your script as follows. 

 

#site contents access
$cxt = New-Object Microsoft.SharePoint.Client.ClientContext($Site)

$cxt.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credentials.UserName,$credentials.Password) #creds for the context to avoid 403 permissions error

$web = $cxt.Web