Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
added 2 characters in body
Source Link
Robert Lindgren
  • 24.5k
  • 12
  • 53
  • 79

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.

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?

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?

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?

Source Link

SharePoint Online - Powershell - "The remote server returned an error: (403) Forbidden."

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?