I'm using the SharePoint Online CSOM library to access different SharePoint servers. I want to be able to determine whether the server I am connecting to is SharePoint Online or an on-prem server.

I have seen some answers suggesting to check the web response header where the SharePoint version is returned, but I don't think that is a good solution since SharePoint 2016 on-prem is now released.

I've also seen a suggestion to check the tenantAppVersion in the _spPageContextInfo object, but I'm using C# and have not found a way to get access to anything like that.

Some have also suggested to check if ".sharepoint.com" is part of the domain name, but that doesn't work for custom domain names. So, how can this be done programmatically?

2 Answers 2


You could check the url in CSOM to determine if it is Online or On Premise:

ClientContext cc = new ClientContext(siteUrl);

if (siteUrl.Contains("sharepoint.com"))
    // this is for SPO, use SharePointOnlineCredentials class
    cc.Credentials = new SharePointOnlineCredentials(username, password);
    // this is for On-Premise domain login 
    cc.Credentials = new NetworkCredential(username, password);
  • That doesn't work for custom domain names where there is no ".sharepoint.com" in the name.
    – Steve
    Commented Aug 9, 2018 at 10:15
  • Why not? This is the correct answer. If it is SPO, it is cloud SharePoint and contains 'sharepoint.com' as part of the FQDN. Anything else is on-prem SharePoint, regardless if it is hosted in-house, in Azure/AWS/etc. or by a 3rd party company.
    – user6024
    Commented Aug 9, 2018 at 14:52
  • See my comment above. Custom SharePoint Online domain names do not contain ".sharepoint.com".
    – Steve
    Commented Aug 9, 2018 at 15:18
  • The only place you'll find custom SPO domains is with SPO-D. Are you working with SPO-D customers?
    – user6024
    Commented Aug 9, 2018 at 18:30
  • Not sure what plan they have, but I work with customers that have a custom domain for SharePoint.
    – Steve
    Commented Aug 9, 2018 at 20:10

Maybe below line of code after ExeceuteQuery can help you :




From the number returned above (example: the most important part is the first number.

12 = SharePoint 2007 (WSS or MOSS)

14 = SharePoint 2010

15 = SharePoint 2013

16 = SharePoint 2016, and generally SharePoint Online / Office 365

  • That doesn't solve my problem. I need to know if the SharePoint server is hosted on-premise or in the cloud (Office 365). Using the ServerVersion might return 16 which could be either.
    – Steve
    Commented Aug 9, 2018 at 14:37
  • I cannot find any other solution. Maybe in that case you might want to check the range of build numbers(example: of on-premise and the cloud (Office 365) returned by using ServerVersion .
    – Ravi
    Commented Aug 10, 2018 at 6:14
  • I think any solution which uses build numbers is prone to breaking with future SharePoint releases. There needs to be a flag that indicates this is a cloud-hosted server. Does anyone know if such a flag already exists? If so, how can I access it via C#?
    – Steve
    Commented Aug 10, 2018 at 13:30
  • I don't think Microsoft has come up with anything to distinguish between On-Prem 2016 and cloud 2016 programmatically (in context of server version) apart from the fact that Cloud 2016 will have higher Minor build number than the On-Prem 2016 Minor build number. Can you check from your end as well, If this is true then this might help you. Thanks
    – Ravi
    Commented Aug 10, 2018 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.