Generating Columns For An SPGridView With LINQ (Extension Method)

More a small development note for myself since I generally end up using all the columns in a list when developing with SPGridView’s.

[csharp]
private static void GenerateColumns(this SPGridView gridView, SPList list )
{
gridView.AutoGenerateColumns = false;
foreach( var column in from string fieldname in list.DefaultView.ViewFields
select list.Fields.GetFieldByInternalName( fieldname )
into field
select new BoundField
{
DataField = field.StaticName,
HeaderText = field.Title
} )
{
gridView.Columns.Add( column );
}
}

[/csharp]

Share

Enumerating All SPWebs In SPFarm.Local Into Strongly Typed Collection

So when enumerating the SPWebs within a SPFarm to build a strongly typed SPWeb collection for whatever purpose your enumeration might look like this:

[csharp]

public static List WebsPreppedForIteration()
{
var collection = new List();
foreach (SPSite x in SPFarm.Local.Services.OfType().SelectMany
(svc => ((svc).WebApplications.Where
(webApp => !webApp.Properties.ContainsKey(“Microsoft.Office.Server.SharedResourceProvider”)).SelectMany
(webApp => webApp.Sites.Cast()))).Where
(x => !Equals(x.RootWeb.Title, “Central Administration”)))
{
collection.AddRange(x.RootWeb.Webs.Cast());
}
return collection;
}

[/csharp]

I saw this in a code review today. The part I am wondering about is the SPWebApplication property bag to query the key for WCAM as opposed to do a clunky string SPWeb.Title comparison. Putting the keys out to standard output hasn’t yielded anything particularly evident, and I’m getting frustrated with the under-the-hood, unnecessary foreach loop with a fancy shirt on (the second LINQ query against the Title property(,

Does anyone know the key for WCAM?

Share

Use LINQ To Get Central Administration Web Applications

I needed a quick and dirty way to go through the SPWebService.AdministrationService to get a collection SPWebService.WebApplications and then test whether the SPWebApplication is the Central Administration application. To do this you need to use the SPWebApplication.IsAdministrationWebApplication boolean property. To keep things succinct you can just chain a LINQ query to return the representative SPSite of the desired administrative SPWebApplication. Consider the following RetrieveAdminApplication method that is consuming a SPContext object as a parameter:

[csharp]
public static SPSite RetrieveAdminApplication(SPContext context)
{
return SPWebService.AdministrationService.WebApplications.Cast().
Where(application => application.IsAdministrationWebApplication).FirstOrDefault().
Sites.Cast().FirstOrDefault();
}
[/csharp]

This is probably not ideal, but will work!

Share