Use LINQ to Find All Web Applications that Use a Specific Service Application in SharePoint 2010

I was working on some code today when I had a bit of an unusual problem.  I had a SearchServiceApplicationProxy and I wanted to find out all of the web applications that were using it.  There were a few different routes I could take, most of them which would require churning through some number of collections in a way I usually hate.  I decided instead to use LINQ to write a query to find the web apps and it actually worked the first time (a minor miracle in my world).  So here’s the code that I used; especially in SharePoint 2010 with so many collections of things all over the place, and this thing has some relationship to that thing, I find I’m using LINQ a lot more when writing code.

//get my service app proxy

SearchServiceApplicationProxy p = myCodeThatGetsTheServiceAppProxy();


//get a reference to the web service to snag the list of web apps from

SPWebService ws = SPWebService.ContentService;


//write a LINQ query to get all the web applications that use

//this service application proxy

var waps = from SPWebApplication wp in ws.WebApplications

where wp.ServiceApplicationProxyGroup.Proxies.Contains((SPServiceApplicationProxy)p)

select wp;


//do something with the results

foreach (SPWebApplication wap in waps)


//do something with the results



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s