1

I have an issue with the files needed for running PowerShell in relation to SharePoint online. I am using the following files to create the connection:

Add-Type -Path "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint.Client\v4.0_15.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.Client.dll" 
Add-Type -Path "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint.Client.Runtime\v4.0_15.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.Client.Runtime.dll" 

And this works on one PC, but not on others. Now the issue is that I know I need to set up the PC correctly first, but I cannot remember how I did this the first time around.

I tried copying the .dlls over to the new machine, but it does not work. I tried to install this: http://www.microsoft.com/en-us/download/details.aspx?id=35585, but that does not work either.

I am sure that I am running everything in Admin-mode, but it still does not work.

My guess is that it is some sort of dependencies that are missing, but I do not know which or how to identify if that is the problem.

Error:

Exception calling "LoadFrom" with "1" argument(s): "Could not load file or assembly 'file:///C:\Program Files\Common Fi les\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll' or one of its dependencies. The sy stem cannot find the file specified." At C:\PowerShell\Builds\GrundfosFunctions.ps1:1549 char:2 + [Reflection.Assembly]::LoadFrom("$env:CommonProgramFiles\Microsoft Shared\Web S ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : FileNotFoundException

And

Exception calling "LoadFrom" with "1" argument(s): "Could not load file or assembly 'file:///C:\Program Files\Common Fi les\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll' or one of its dependencies . The system cannot find the file specified." At C:\PowerShell\Builds\GrundfosFunctions.ps1:1550 char:5 + [Reflection.Assembly]::LoadFrom("$env:CommonProgramFiles\Microsoft Shared\We ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : FileNotFoundException

  • you are referencing the dlls from the GAC... how about you copy the 2 dlls to c:\temp and reference them as c:\temp\Microsoft.SharePoint.Client.dll and c:\temp\Microsoft.SharePoint.Client.Runtime.dll. otherwise you might have to run gacutil – Tiago Duarte Sep 4 '15 at 8:34
  • Tried referencing the dlls from c:\temp, but it still does not work. – JMBarlach Sep 4 '15 at 8:48
  • perhaps it would be helpful to post the output of the cmdlet – Tiago Duarte Sep 4 '15 at 9:00
  • Of cause, should have done that from the beginning. Sorry. I will add it to the original post. – JMBarlach Sep 4 '15 at 9:23
  • your cmdlet says c:\windows, the code says program files. in order to narrow down the possible causes, I would recommend to use the c:\temp approach and describe the results, as it appears to clearly say "file not found". also the link you sent is for SP2013 but you are referencing version 16. make sure you are using the right sdk – Tiago Duarte Sep 4 '15 at 9:42
6

Note: The way you have tried to add SharePoint Client Side Namespaces is certainly not the correct or the best way. Also your provided sdk link is for SharePoint 2013(15 hive) and not for SPO(16 Hive).

If you want to connect to SharePoint Online via CSOM PowerShell, then first you need to get installed SharePoint Online Client Components SDK. This installation will get all the required dlls at 16 hive location.

Note: Using SharePoint Server 2013 Client Components SDK you can do your PowerShell development, but SPO Client Components SDK in addition to SharePoint Server 2013 gives extra methods which can be very handy.

Here is the location where you find all the dlls

C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\

And you add these in your ps code like below:

Add-Type -Path 'C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'
Add-Type -Path 'C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'    

Refer below code which access SharePoint Online Site, Also read this very helpful article on Using CSOM in PowerShell scripts with Office 365 by Chris O'Brien.

# replace these details (also consider using Get-Credential to enter password securely as script runs).. 
$username = "SomeUser@SomeOrg.onmicrosoft.com" 
$password = "SomePassword" 
$url = "https://SomeSite.sharepoint.com"

$securePassword = ConvertTo-SecureString $Password -AsPlainText -Force 

# the path here may need to change if you used e.g. C:\Lib.. 
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" 
# note that you might need some other references (depending on what your script does) for example:
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll" 

# connect/authenticate to SharePoint Online and get ClientContext object.. 
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url) 
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword) 
$clientContext.Credentials = $credentials 

if (!$clientContext.ServerObjectIsNull.Value) 
{ 
    Write-Host "Connected to SharePoint Online site: '$Url'" -ForegroundColor Green 
} 
  • 1
    You sir are a champ. Of cause it was something stupid like not installing the correct .dlls. And as a added bonus I get my code cleaned up. Thank a million. – JMBarlach Sep 7 '15 at 10:37
  • @JMBarlach Thanks for your kind words :) . I am glad that it worked for you.. – Asad Refai Sep 7 '15 at 10:38

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.