Search code examples
powershellsharepointoffice365

Cannot load Microsoft.SharePoint.Client.dll in powershell script


im trying to connect to an offce365 sharepoint site using a powershell script. However, none of commands I've tried to load the sharepoint assemblies are working.

The error message (sorry, it's German) looks like this:

Add-Type : Die Datei oder Assembly "file:///'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll" oder eine Abhängigkeit davon wurde nicht gefunden. Die Assembly wird von einer Laufzeit erstellt, die aktueller als die derzeit geladene Laufzeit ist, und kann nicht geladen werden.
Bei C:\Users\****\Desktop\onfb\odfb.ps1:6 Zeichen:9
+ Add-Type <<<<  -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'
    + CategoryInfo          : NotSpecified: (:) [Add-Type], BadImageFormatException
    + FullyQualifiedErrorId : System.BadImageFormatException,Microsoft.PowerShell.Commands.AddTypeCommand

It says, the the component (or a dependency) could either not be found, or it is created by a newer version than the one I'm using.

First I tried loading the inital files with

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

After that I tried the following command:

Add-Type -Path ([System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client").location)

I also tried copying the files to another folder, and changed the reference accordingly.

I also set the

<runtime>
    <loadfromremotesources enabled="true"/>
</runtime>

in the powershell.exe.config file. Which wasn't there, so I created it.

I'm running the powershell in elevated mode and also tried with an unrestricted security.

Does anyone know how to solve this?

Thanks!


Solution

  • The precise error message in English would be:

    This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.

    Most likely it occurs since you are using PowerShell version 2 but SharePoint Online Client Components SDK is targeting .Net Framework v4.0 enter image description here

    How to determine installed PowerShell version:

    PS C:\> $PSVersionTable.PSVersion
    

    The .NET 4 CLR can load .NET 2 assemblies , but not vice versa. So, you have to upgrade PowerShell.