Can’t Use Resharper 4.5 With LINQ-To-XSD

EDIT: This also occurs in Resharper 5.0

Resharper 4.5 breaks the type references when using Linq-To-XSD, which is really lame. Really, really lame since I use it all the time. So, this is what it looks like when you first set your .XSD to a LinqToXsdSchema build action and reference the project (note: I am not talking about using the command line switches to externally generate the strongly typed DAO files but rather using the inherent VS.NET hooks).

This sucks obviously since you lose intellisense, however the freaking project will build correctly! The only way I have found around it is to disable Resharper when accessing your generated classes.

Then it automagically goes back to being able to reference the right types again…

Lame :(

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

Best Approach To Activate MasterPage for SharePoint Web

Going through a quarterly code review with a company today, we started to talk about the best way that a masterpage should be programmatically activated for a particular SharePoint web as the way being used by some components was fairly poor.

Below is what we decided on, which I don’t know how I feel about. I am leaving a lot of the exception handling out for brevity.

Firstly, the GenericMasterPage super class inherits from the SPFeatureReceiver concrete class, implementing all the obligatory abstract inherited members. This just contains references to the static application methods in the Builders class.

[csharp]
public class GenericMasterPage : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
Builders.IterateGalleryAndApplyTemplate(properties, Constants.GENERIC_MASTER_PAGE_NAME);
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
Builders.IterateGalleryAndApplyTemplate(properties, Constants.DEFAULT_MASTER_PAGE_NAME);
}
#region Unused Abstract Inherited Members
public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
// This abstract inherited member requires no behavior modification
}

public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{
// This abstract inherited member requires no behavior modification
}
#endregion
}
[/csharp]

Following, the Constants class, as the name implies, defines constants!

[csharp]
public class Constants
{
public const string DEFAULT_MASTER_PAGE_NAME = “default.master”;
public const string GALLERY_LIBRARY_NAME = “Master Page Gallery”;
public const string GENERIC_MASTER_PAGE_NAME = “lps.master”;
public const string PROPERTY_TO_QUERY = “Name”;
public const string MASTER_REL_LINK = “_catalogs/masterpage/”;
}
[/csharp]

Lastly, the Builders class contains the static IterateGalleryAndApplyTemplate method which will use a LINQ query to ensure that the master page to apply is available, and then apply it to the current SharePoint web.

[csharp]
public class Builders
{
public static void IterateGalleryAndApplyTemplate(SPFeatureReceiverProperties properties, string templateName)
{
SPSecurity.RunWithElevatedPrivileges(() =>
{
using (var web = (SPWeb) properties.Feature.Parent)
{
web.AllowUnsafeUpdates = true;
var ensureExistance = SPContext.Current.Web.Lists[Constants.GALLERY_LIBRARY_NAME].Items.Cast().
Where(item => Equals(item[Constants.PROPERTY_TO_QUERY].ToString(), templateName)).FirstOrDefault();
if (ensureExistance != null)
{
string masterUrl = new Uri(web.Url).GetComponents(UriComponents.Path, UriFormat.Unescaped);
if (!masterUrl.StartsWith(“/”)) masterUrl = String.Format(“/{0}”, masterUrl);
if (!masterUrl.EndsWith(“/”)) masterUrl += “/”;
masterUrl += Constants.MASTER_REL_LINK + templateName;
web.MasterUrl = masterUrl;
web.CustomMasterUrl = masterUrl;
}
web.Update();
web.AllowUnsafeUpdates = false;
}
});
}
}
[/csharp]

I wonder if there is a more efficient way of achieving this same requirement.

Share