Geeks With Blogs
David Jacobus SharePoint Consultant

I had added some code behind to a SharePoint 2010 master page and it worked beautifully! I followed Andrew Connell’s blog and article on MSDN. I had actually created two more master pages with an identical code behind as they each needed the same bits. I had each inherit from the same code behind file. Everything worked great until we were having some problems with one of our developers opening a css file form the VPN. So I thought I would just change the master page in SPD temporarily to use a css file from a different folder which he had permissions to. Easy Huh! I did that and lo and behold my site crashed with an error the said: My Master page was not registered as safe! I did some digging and found some references on MSDN discussing pages: MSDN BLOG which discussed safe control entries and Page Parser Paths. I saw that this was an identical problem as I was having with the Master Page. It works until it is edited, even as simply as a user editing with SharePoint designer. To fix the issue, I followed the steps:

1. Added a safe control entry on the Master Page Module

<SafeControl Name="SafeControlEntry2" Assembly="$SharePoint.Project.AssemblyFullName$" Namespace="Your Master Page NameSpace" TypeName="*" IsSafe="true" IsSafeAgainstScript="false" />

This can be done by setting the safe control entry by highlighting the module ad looking at the properties window see (MSDN BLOG):

clip_image002[6]

clip_image004[5]

2. I added a web Configuration Feature Receiver which added my master page(s) PageParserPath in the web configuration file:

public override void FeatureActivated(SPFeatureReceiverProperties properties)

{

SPWebApplication webApplication = properties.Feature.Parent as SPWebApplication;

SPSecurity.RunWithElevatedPrivileges(delegate()

{

SPWebConfigModification mod = new SPWebConfigModification();

mod.Path = "configuration/SharePoint/SafeMode/PageParserPaths";

mod.Name = "PageParserPath[@VirtualPath='/_catalogs/masterpage/1.master']";

mod.Owner = "NameMaster";

mod.Sequence = 0;

mod.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;

mod.Value = "<PageParserPath VirtualPath='/_catalogs/masterpage/1.master’ CompilationMode='Always' AllowServerSideScript='true' />";

webApplication.WebConfigModifications.Add(mod);

SPWebConfigModification mod2 = new SPWebConfigModification();

mod2.Path = "configuration/SharePoint/SafeMode/PageParserPaths";

mod2.Name = "PageParserPath[@VirtualPath='/_catalogs/masterpage/2.master']";

mod2.Owner = "NameMaster";

mod2.Sequence = 0;

mod2.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;

mod2.Value = "<PageParserPath VirtualPath='/_catalogs/masterpage/2.master' CompilationMode='Always' AllowServerSideScript='true' />";

webApplication.WebConfigModifications.Add(mod2);

SPWebConfigModification mod3 = new SPWebConfigModification();

mod3.Path = "configuration/SharePoint/SafeMode/PageParserPaths";

mod3.Name = "PageParserPath[@VirtualPath='/_catalogs/masterpage/3.master']";

mod3.Owner = "NameMaster";

mod3.Sequence = 0;

mod3.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;

mod3.Value = "<PageParserPath VirtualPath='/_catalogs/masterpage/3.master' CompilationMode='Always' AllowServerSideScript='true' />";

webApplication.WebConfigModifications.Add(mod3);

WebApplication.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();

webApplication.Update();

});

}

// Uncomment the method below to handle the event raised before a feature is deactivated.

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)

{

SPWebApplication webApplication = properties.Feature.Parent as SPWebApplication;

SPSecurity.RunWithElevatedPrivileges(delegate()

{

Collection<SPWebConfigModification> mods = webApplication.WebConfigModifications;

int initialModificationsCount = mods.Count;

for (int i = initialModificationsCount - 1; i >= 0; i--)

{

if (mods[i].Owner == "NameMaster")

{

SPWebConfigModification modToRemove = mods[i];

mods.Remove(modToRemove);

}

}

if (initialModificationsCount > mods.Count)

{

webApplication.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();

webApplication.Update();

}

});

}

No, I don’t name my master pages 1.master, 2.master, and 3.master! I just removed the actual names to protect my client.

The result of these changes in the web.config are:

1. PageParserPaths’s

<SafeMode MaxControls="200" CallStack="false" DirectFileDependencies="10" TotalFileDependencies="50" AllowPageLevelTrace="false">

<PageParserPaths>

<PageParserPath VirtualPath="/_catalogs/masterpage/1.master" CompilationMode="Always" AllowServerSideScript="true" />

<PageParserPath VirtualPath="/_catalogs/masterpage/2.master" CompilationMode="Always" AllowServerSideScript="true" />

<PageParserPath VirtualPath="/_catalogs/masterpage/3.master" CompilationMode="Always" AllowServerSideScript="true" />

</PageParserPaths>

</SafeMode>

2. SafeControl

<SafeControl Assembly="NameSpace.MasterPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2d6526e879238f35" Namespace="NameSpace.MasterPages" TypeName="*" Safe="True" SafeAgainstScript="False" />

I checked and was able to edit the page in SPD and fixed the issue of crashing the site! SharePoint is Tribal in many areas, the platform is so vast there just isn’t anywhere that’s has all the answers!

 

 

 

 

 

Posted on Saturday, August 20, 2011 1:39 AM SharePoint | Back to top


Comments on this post: SharePoint 2010 Master Page with Code behind

# re: SharePoint 2010 Master Page with Code behind
Requesting Gravatar...
Could you please explain this line in the code:
mod.Owner = "NameMaster";

what is NameMaster here in this case?

Please let me know. Thanks.
Left by ss on May 30, 2012 12:10 AM

Your comment:
 (will show your gravatar)


Copyright © David Jacobus | Powered by: GeeksWithBlogs.net