In my code, I am trying to load “web” after creating client context. Below is my code:
$ SiteUrl = "...."; #My site URL goes here# $ uname =$ xmlFile.Account.Trim() $ pwd = $ xmlFile.Password.Trim() Add-Type -Path $ path"\DLLs\Microsoft.SharePoint.Client.dll" Add-Type -Path $ path"\DLLs\Microsoft.SharePoint.Client.Runtime.dll" $ securePassword=New-Object SecureString foreach($ c in $ pwd.ToCharArray()) { $ securePassword.AppendChar($ c) } # Getting ClientContext object.. $ clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($ SiteUrl) $ credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($ uname, $ pwd) $ clientContext.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($ credentials) if (!$ clientContext.ServerObjectIsNull.Value) { try{ $ web = $ clientContext.Web; $ clientContext.Load($ web); $ clientContext.ExecuteQuery(); write-host $ web } catch { } }
I am getting error:
System.Management.Automation.MethodInvocationException: Exception calling “ExecuteQuery” with “0” argument(s): “The remote server returned an error: (403) Forbidden.” —> System.Net.WebException: The remote server returned an error: (403) Forbidden.
Any suggestions?