SharePoint Online and CSOM: A quick reminder to read error messages properly!


 

This blog post is going to describe what I found to be a pretty confusing error message, that you might also encounter when trying to manage SharePoint Online sites with CSOM in PowerShell.

 

Background

I had encountered this issue while creating a script for a client that would standardize site collection audit settings across all personal sites, so that users would be able to have direct access to audit data for their OneDrive for Business libraries, without having to open a ticket so that somebody with access to the Security and Compliance center can provide them with the requested data.

 

Approach

The first thing I did was make sure that I had the SharePoint Online Client Component SDK installed.  This would make sure that the appropriate assemblies are installed on my system and registered in the GAC.  There are other ways to get access to the assemblies, but this is my preferred method.

The next thing I did was launch PowerShell ISE as Administrator and load the assemblies that I wanted to use in this particular script

[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint.Client”)
[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint.Client.Runtime”)

Next, I defined all of the variables that I was going to use, such as the Site URL, and the credentials to be used, etc. before finally trying to create the necessary context

$ClientContext = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)

This resulted in an error message that appeared very helpful at first

New-Object : A constructor was not found. Cannot find an appropriate constructor for type Microsoft.SharePoint.Client.ClientContext.
At line:1 char:18
+ … ntContext = New-Object Microsoft.SharePoint.Client.ClientContext($Sit …
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand

 

I was very certain that I had loaded all of the necessary assemblies.  First I checked over some of my previous similar scripts –  they had no problem creating the context when only these two assemblies were loaded.

 

Solution

This was actually the result of a typo.  Not in the object namespace, but in the name of my variable name (not the value) when it was defined.  So basically, it was because I was passing in a null value where a string was expected.  See ClientContext members for more details.  If it had actually been because the assembly was not loaded, the message would have been more like this

New-Object : Cannot find type [Microsoft.SharePoint.Client.BigClientContext]: verify that the assembly containing this type is
loaded.
At line:1 char:18
+ … ntContext = New-Object Microsoft.SharePoint.Client.BigClientContext($ …
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

 

Feedback

As always, if you do have any feedback or suggestions, I’d love to hear from you.

You can also follow me on twitter @RCormier_MSFT


Comments (0)

Skip to main content