I'm writing web service which will be part of my sharepoint solution. This service will save and read some data from custom db which will reside on the same mssql instance as sharepoint. I'd like my service to be reusable, so I'm looking for a way to get mssql instance name from sharepoint and use this to connect to DB. Have You ever done something like this before? Or maybe there is a better approach?

2 Answers 2


In your web service, you can start beginning with "using" the SharePoint object model,

using Microsoft.SharePoint;

And then, get the Database instance properties using the SPDatabaseServiceInstance object.

Sample code might look like this

SPSite siteCollection = SPContext.Current.Site;
SPContentDatabase contentDB = siteCollection.ContentDatabase;
SPDatabaseServiceInstance contentDBInstance = contentDB.ServiceInstance;

There is a chance that service will not have access to SPContext and using SPFarm.Local will make more sense. You could use solution proposed in how to find the sqlserver name(data source) used by sharepoint?.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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