I am using the following code to get the Web Application object in SharePoint 2013. But this is causing some performance issues. Does anyone know of a better approach? Please help.

SPWebApplication spWebApp = SPWebService.ContentService.WebApplications.SingleOrDefault(wa => wa.Name == SPConstants.SPWEBAPP_NAME); // Assuming Web Application on Port 80 will have the same name across environments
  • Where is your code running? In timer job/event reciever/ webpart? – Unnie Oct 14 '15 at 12:05
  • It is running in a class library and being called from an ASP.Net MVC site. – Ven Oct 15 '15 at 11:19
  • So you are running this code outside of SharePoint farm in a Provider hosetd app? – Unnie Oct 15 '15 at 11:59
  • No app at all. It is called from an ASP.Net MVC Single Page application. – Ven Oct 15 '15 at 15:43
  • Is that Mvc application running out side of sharepoint farm? – Unnie Oct 15 '15 at 17:04

You may be able to use SPWebApplication.Lookup method that finds the Web application that is located at the specified URL.

  • I cannot use the URL at all. It is a multi-tenant environment. I only get a Tenant Name from the UI layer (non SharePoint). Based on the that, I need to get the SPSite instance. For that, I need to get the SPWebApplication and for that also, I won't have the URL. All I know is, I have to look up the web application at Port 80 (since I know it will be always at Port 80). So, my requirement is to be able to create SPSite using only Tenant Name (Alias). If this is achievable, please let me know the code. If not, I need to be able to get WebApp object from Port 80. Hope the requirement is clear. – Ven Oct 15 '15 at 5:09

You can use,

SPWebApplication web = SPContext.Current.Site.WebApplication;

SPWebApplication web = new SPSite(“http://myserver/site”).WebApplication;


  • Please read my comment above. I won't have the URL and I won't have the context either. I even tried with the URL hard coded with Lookup method, but even that is very slow. – Ven Oct 15 '15 at 11:19

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.