Microsoft.SharePoint.Client.ClientRuntimeContext.Load: type argument 'Microsoft.SharePoint.Client.Web' violates the constraint of type parameter 'T'

A provider-hosted app began to display this error when any CSOM call was made to SharePoint Online:

'Microsoft.SharePoint.Client.ClientRuntimeContext.Load: type argument 'Microsoft.SharePoint.Client.Web' violates the constraint of type parameter 'T'

This was thrown when any call was made using clientContext.Load(), regardless of the object being loaded, and despite the calls being wrapped in a try/catch block.

The root cause of this was that earlier in the day the project's assembly references in Visual Studio became corrupted, and building the project started throwing errors regarding invalid assemblies, for any member of Microsoft.SharePoint.Client (why this happened is still a mystery).

Removing the reference and re-adding it via Project > References made those build errors go away....


.... but that resulted in the nasty constraint violation error.

The reason for that was that the Microsoft.SharePoint.Client assembly references were originally added to the project by adding the NuGet package (Microsoft.SharePointOnline.CSOM). Although these DLLs have the same name as the ones you see in Project > References, they are different (or at least have different version numbers). Hence the request was asking for a slightly different data type than that returned by SharePoint Online.

You can solve this problem by uninstalling the NuGet package from the project, and reinstalling it. To remove the package you need to open the package manager console (Tools > NuGet Package Manager > Package Manager Console), type Uninstall-Package Microsoft.SharePointOnline.CSOM and hit Enter.

Once the package is removed you can then re-add it via the NuGet UI, or just type Install-Package Microsoft.SharePointOnline.CSOM to reinstall it. Use the -project switch if you don't want the package deployed to all the projects in your solution.

Developing Office & SharePoint solutions in Visual Studio 2015

Visual Studio 2015 was launched on 20th July 2015. This is great news for developers who have been using the Preview for the last few months, and represents a big step forward for Visual Studio, particularly for those developing solutions which leverage Azure, with great integration right in the IDE interface. And the upgrade from the Preview version is quick and seamless - all hail the new Microsoft!

There are too many great new features and innovations to list here - take a look at the keynote from the launch to get a flavour.


One thing that might not be immediately apparent is how to get started with SharePoint and Office 365 development in Visual Studio 2015 - this wasn't possible in the Preview/RC version as there was no corresponding Preview version of the Office Developer Tools (VSTO) 2015. Without the Office Developer Tools installed, you won't see any Office/SharePoint project templates available when you click New > Project.

A new version of the Office Developer Tools was promised for launch along with the RTM version of Visual Studio 2015, but Microsoft did a pretty good job of hiding it. But rejoice - Office Developer Tools 2015 was indeed launched, and can be installed by clicking the "Get Office Developer Tools" link here

After installing the Office Tools, the project templates are available and you're all set for SharePoint, Office and Office 365 development in Visual Studio 2015.