2

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?

1

Change the way you load the CSOM DLLs from this:

Add-Type -Path $path"\DLLs\Microsoft.SharePoint.Client.dll"
Add-Type -Path $path"\DLLs\Microsoft.SharePoint.Client.Runtime.dll"

to this:

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'

Substituting whatever the proper path to your DLLs is.

I don't understand exactly what is going on, but in my environment I have to use the full path when running the script on my server, but I don't have to on my workstation. Otherwise I get the same (403) Forbidden error that you do

0

I think this lines are correct (compare with yours):

$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($uname, $pwd) 
$clientContext.Credentials = $credentials 
  • Tried your suggestion as well. No luck! – Learner Life Jan 20 '17 at 11:24
  • Please check in browser that your user\pass are valid. – Sergei Sergeev Jan 20 '17 at 11:31
  • Yes, its correct. Also,this same code works from one machine. But from different machine(server) its throwing error. – Learner Life Jan 20 '17 at 11:39
  • Can you confirm there is no proxy configuration for this machine? – Sergei Sergeev Jan 20 '17 at 12:20
0

Can you try below example and see how it works on your system.


Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"  
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"   

$siteUrl = "site url" 
$username = "username" 
$password = Read-Host -Prompt "Enter password" -AsSecureString  

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)  
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)  
$ctx.Credentials = $credentials 

$rootWeb = $ctx.Web  
$ctx.Load($rootWeb) 
$ctx.ExecuteQuery()

Write-Host $rootWeb.Title  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.