I’ve decided to do a little series on SharePoint Tips for Developers, which will focus on common tasks that aren’t immediately obvious, hopefully saving people some time in their day to day work.
Our first tip is about enumerating the objects in your farm. Specifically, taking an SPFarm object, finding all the web applications in the farm, all the sites, and all the pages within them. Some portion of that enumeration is likely to come up quite often in your work.
First, Getting all Web Applications in a farm is probably one of the lesser known tasks:
1 |
<blockquote><p> SPWebService webService = farm.Services.GetValue<SPWebService>(<span class="str">""</span>);<br /> <span class="kwrd">foreach</span> (SPWebApplication webApp <span class="kwrd">in</span> webService.WebApplications)<br /> {<br /> <span class="rem">//Do Something with Web Application here</span><br /> }</p></blockquote> |
In the above example, the variable farm is an SPFarm object, which you can get in a variety of ways, depending where your code is running. In a timer job you might just do “this.Farm” — in a web part you can get the farm from the site object in SPContext.Current.
Now we can go through all the sites and webs. Probably the simplest way to do this is to loop through the Sites collection of your web app, and call a recursive helper function on each web, which will enumerate through all child webs regardless of depth.
1 |
<span class="kwrd">foreach</span> (SPSite site <span class="kwrd">in</span> webApp.Sites)<br /> {<br /> <span class="rem">//Pass web to helper function to enumerate</span><br /> GetWeb(site.RootWeb);<br /><br /> } |
1 |
<span class="kwrd">private</span> <span class="kwrd">void</span> GetWeb(SPWeb web)<br /> {<br /> <span class="rem">//TODO: Do something with the web object</span><br /><br /> <span class="rem">//Process the sub webs recursively</span><br /> <span class="kwrd">foreach</span> (SPWeb subweb <span class="kwrd">in</span> web.Webs)<br /> {<br /> GetWeb(subweb);<br /> }<br /> } |
Once you have the web, there are a variety of ways you might find the pages you are interested in. For example, if it is a publishing site you might want to find all the Pages libraries and get only the published pages — You could do this with the web object you have available in GetWeb(…):
1 |
<span class="kwrd">foreach</span> (SPList list <span class="kwrd">in</span> web.Lists)<br /> {<br /> <span class="kwrd">if</span>(list.Title == <span class="str">"Pages"</span>)<br /> {<br /> <span class="kwrd">foreach</span> (SPListItem item <span class="kwrd">in</span> list.Items)<br /> { <br /> <span class="kwrd">if</span> (item.File.Exists && <br /> item.File.Url.EndsWith(<span class="str">".aspx"</span>) && <br /> item.Versions[0].Level == SPFileLevel.Published)<br /> {<br /> <span class="rem">//Do something clever with the page which is item.File.Url</span><br /> }<br /> }<br /> } |
Note that above we check if the item is published by looking at the level attribute of the version.