Aelys
Aelys

Reputation: 241

Determine programmatically if we're connecting to SharePoint Online or on-premise with CSOM API

I would like to know if there is a way to determine if we're about to connect to a SharePoint Online server or an on-premise one (as the Credentials object type differs). I'm using the CSOM API in C# with a SharePoint 2013 and a SharePoint Online server.

So far, I haven't found anything useful in the ClientContext object itself so I'm thinking to just check the login that the user to see if it's login is like "DOMAIN\ACCOUNT" or "[email protected]" but I don't know if it's possible to have a Domain\Account login type on SharePoint Online or something else.

If it's impossible to do, I'll just ask the user to tell what type of server it is.

Upvotes: 1

Views: 1749

Answers (1)

dstarkowski
dstarkowski

Reputation: 337

There is a MicrosoftSharePointTemaServices header in HTTP response sent back from SharePoint. It contains version number. 2013 will return 15.0.0.xxxx and O365 16.0.0.xxxx (at least now, in future they may bump the version).

You can retrieve it like this:

var request = WebRequest.Create("https://sharepoint.contoso.com");
try
{
    request.GetResponse(); //fails with 401
}
catch (WebException ex)
{
    var version = ex.Response.Headers["MicrosoftSharePointTeamServices"];
}

Upvotes: 1

Related Questions