Cannot Convert Type Microsoft.SharePoint.WebControls.CSSRegistration to System.Web.UI.IAttributeAccessor – Beware of this in custom master page for custom publishing site definition

I went through an interesting case last week. One of my customer is trying to create a Custom publishing Definition and wants to integrate the definition with his custom Master page. He has created the custom master page by copying the blueband.master and heavily customized it through the SharePoint designer to enrich the look and feel. Ok, the custom master page is done and ready to provision to the master page gallery. A feature has been created to provision the master page file to the master page gallery and successfully installed.

Following is the snippet of feature.xml and the elements.xml file which is not only picture perfect they are absolutely fine J

<Feature Id="EC3777F3-49C1-4e5e-A86E-9ED13DA6DC2A"

Title="SamplePublishing Custom Master Page"

Scope="Web"

xmlns="https://schemas.microsoft.com/sharepoint/" >

<ElementManifests>

<ElementManifest Location="ElementManifest.xml" />

</ElementManifests>

</Feature>

Elements.xml :

<Elements xmlns="https://schemas.microsoft.com/sharepoint/">

<Module Name="SampleSTSMasterPage" List="116" Url="_catalogs/masterpage">

<File Url="agent1.master" Type="GhostableInLibrary">

<Property Name="ContentType" Value="$Resources:cmscore,contenttype_masterpage_name;" />

<Property Name="PublishingPreviewImage" Value="~SiteCollection/_catalogs/masterpage/$Resources:core,Culture;/Preview Images/BlueBand.png, ~SiteCollection/_catalogs/masterpage/$Resources:core,Culture;/Preview Images/BlueBand.png" />

<Property Name="MasterPageDescription" Value="$Resources:cmscore,MasterPage_BlueBand_Description;" />

</File>

</Module>

</Elements>

A custom publishing Site definition was created with all the required publishing features and along with the above mention feature so that the feature stapling will provision the custom master page to the master page gallery. The MasterUrl (For System Pages) and the CustomMasterURL (For Site pages) properties are properly mentioned in the Onet.xml file.

Following is the snippet of Onet.xml and the WebTemp.xml file :

<?xml version="1.0" encoding="utf-8" ?>

<!-- _lcid="1033" _version="12.0.2220" _dal="1" -->

<!-- _LocalBinding -->

<Project Revision="3" Title="Publishing Site" ListDir="Lists" xmlns:ows="Microsoft SharePoint">

<NavBars>

<NavBar Name="SharePoint Top Navbar" ID="1002">

</NavBar>

</NavBars>

<ListTemplates>

</ListTemplates>

<DocumentTemplates>

<DocumentTemplate Path="STS" Name="" DisplayName="$Resources:core,doctemp_None;" Type="100" Default="FALSE" Description="$Resources:core,doctemp_None_Desc;" />

<DocumentTemplate Path="STS" DisplayName="$Resources:core,doctemp_Word97;" Type="101" Default="TRUE" Description="$Resources:core,doctemp_Word97_Desc;">

<DocumentTemplateFiles>

<DocumentTemplateFile Name="doctemp\word\wdtmpl.doc" TargetName="Forms/template.doc" Default="TRUE"/>

</DocumentTemplateFiles>

</DocumentTemplate>

<DocumentTemplate Path="STS" DisplayName="$Resources:core,doctemp_Excel97;" Type="103" Description="$Resources:core,doctemp_Excel97_Desc;">

<DocumentTemplateFiles>

<DocumentTemplateFile Name="doctemp\xl\xltmpl.xls" TargetName="Forms/template.xls" Default="TRUE"/>

</DocumentTemplateFiles>

</DocumentTemplate>

<DocumentTemplate Path="STS" DisplayName="$Resources:core,doctemp_Powerpoint97;" Type="104" Description="$Resources:core,doctemp_Powerpoint97_Desc;">

<DocumentTemplateFiles>

<DocumentTemplateFile Name="doctemp\ppt\pptmpl.pot" TargetName="Forms/template.pot" Default="TRUE"/>

</DocumentTemplateFiles>

</DocumentTemplate>

<DocumentTemplate Path="STS" DisplayName="$Resources:core,doctemp_Word;" Type="121" Default="TRUE" Description="$Resources:core,doctemp_Word_Desc;">

<DocumentTemplateFiles>

<DocumentTemplateFile Name="doctemp\word\wdtmpl.dotx" TargetName="Forms/template.dotx" Default="TRUE"/>

</DocumentTemplateFiles>

</DocumentTemplate>

<DocumentTemplate Path="STS" DisplayName="$Resources:core,doctemp_Excel;" Type="122" Description="$Resources:core,doctemp_Excel_Desc;">

<DocumentTemplateFiles>

<DocumentTemplateFile Name="doctemp\xl\xltmpl.xlsx" TargetName="Forms/template.xlsx" Default="TRUE"/>

</DocumentTemplateFiles>

</DocumentTemplate>

<DocumentTemplate Path="STS" DisplayName="$Resources:core,doctemp_Powerpoint;" Type="123" Description="$Resources:core,doctemp_Powerpoint_Desc;">

<DocumentTemplateFiles>

<DocumentTemplateFile Name="doctemp\ppt\pptmpl.pptx" TargetName="Forms/template.pptx" Default="TRUE"/>

</DocumentTemplateFiles>

</DocumentTemplate>

<DocumentTemplate Path="STS" DisplayName="$Resources:core,doctemp_OneNote;" Type="111" Description="$Resources:core,doctemp_OneNote_Desc;">

<DocumentTemplateFiles>

<DocumentTemplateFile Name="doctemp\onenote\template.one" TargetName="Forms/template.one" Default="TRUE"/>

</DocumentTemplateFiles>

</DocumentTemplate>

<DocumentTemplate Path="STS" DisplayName="$Resources:core,doctemp_FP;" Type="102" Description="$Resources:core,doctemp_FP_Desc;">

<DocumentTemplateFiles>

<DocumentTemplateFile Name="doctemp\fp\fptmpl.htm" TargetName="Forms/template.htm" Default="TRUE"/>

</DocumentTemplateFiles>

</DocumentTemplate>

<DocumentTemplate Path="STS" DisplayName="$Resources:core,doctemp_BasicPage;" Type="105" Description="$Resources:core,doctemp_BasicPage_Desc;">

<DocumentTemplateFiles>

<DocumentTemplateFile Name="doctemp\blankpgs\_basicpage.htm" TargetName="Forms/_basicpage.htm" Default="TRUE"/>

</DocumentTemplateFiles>

</DocumentTemplate>

<DocumentTemplate Path="STS" DisplayName="$Resources:core,doctemp_WebPartPage;" Type="106" Description="$Resources:core,doctemp_WebPartPage_Desc;">

<DocumentTemplateFiles>

<DocumentTemplateFile Name="doctemp\smartpgs\_webpartpage.htm" TargetName="Forms/_webpartpage.htm" Default="TRUE"/>

</DocumentTemplateFiles>

</DocumentTemplate>

<DocumentTemplate XMLForm="TRUE" Path="STS" DisplayName="$Resources:core,doctemp_BlankForm;" Type="1000" Default="TRUE" Description="$Resources:core,doctemp_BlankForm_Desc;">

<DocumentTemplateFiles>

<DocumentTemplateFile Name="doctemp\xmlforms\blank\template.xml" TargetName="Forms/template.xml" Default="TRUE" />

</DocumentTemplateFiles>

</DocumentTemplate>

</DocumentTemplates>

<BaseTypes>

</BaseTypes>

<Configurations>

<Configuration ID="-1" Name="NewWeb"/>

<Configuration ID="0" Name="Publishing" CustomMasterUrl="_catalogs/masterpage/Agent1.master" MasterUrl="_catalogs/masterpage/Agent1.master">

<Lists>

</Lists>

<SiteFeatures>

<!-- Publishing Prerequisites-->

<Feature ID="A392DA98-270B-4e85-9769-04C0FDE267AA" />

<!-- Office Sharepoint Server Publishing -->

<Feature ID="F6924D36-2FA8-4f0b-B16D-06B7250180FA" />

</SiteFeatures>

<WebFeatures>

<!-- Include the common WSSListTemplateFeatures used by CMS -->

<Feature ID="00BFEA71-DE22-43B2-A848-C05709900100" > </Feature> <Feature ID="00BFEA71-E717-4E80-AA17-D0C71B360101" > </Feature> <Feature ID="00BFEA71-52D4-45B3-B544-B1C71B620109" > </Feature> <Feature ID="00BFEA71-A83E-497E-9BA0-7A5C597D0107" > </Feature> <Feature ID="00BFEA71-4EA5-48D4-A4AD-305CF7030140" > </Feature>

<Feature ID="00BFEA71-4EA5-48D4-A4AD-7EA5C011ABE5">

</Feature>

<Feature ID="22A9EF51-737B-4ff2-9346-694633FE4416">

<Properties xmlns="https://schemas.microsoft.com/sharepoint/">

<Property Key="ChromeMasterUrl" Value=""/>

<Property Key="WelcomePageUrl" Value="$Resources:cmscore,List_Pages_UrlName;/default.aspx"/>

<Property Key="PagesListUrl" Value=""/>

<Property Key="AvailableWebTemplates" Value=""/>

<Property Key="AvailablePageLayouts" Value=""/>

<Property Key="SimplePublishing" Value="true" />

</Properties>

</Feature>

<Feature ID="541F5F57-C847-4e16-B59A-B31E90E6F9EA">

<Properties xmlns="https://schemas.microsoft.com/sharepoint/">

<Property Key="InheritGlobalNavigation" Value="true"/>

<Property Key="ShowSiblings" Value="true"/>

<Property Key="IncludeSubSites" Value="true"/>

</Properties>

</Feature>

<Feature ID="94C94CA6-B32F-4da9-A9E3-1F3D343D7ECB">

<!-- Office SharePoint Server Publishing -->

</Feature>

<Feature ID="EC3777F3-49C1-4e5e-A86E-9ED13DA6DC2A"/>

<!-- Feature which provisions the custom master page to master page gallery -->

</WebFeatures>

<Modules>

<Module Name="Home" />

</Modules>

</Configuration>

</Configurations>

<Modules>

<Module Name="Home" Url="$Resources:cmscore,List_Pages_UrlName;" Path="">

<File Url="default.aspx" Type="GhostableInLibrary" Level="Draft" >

<Property Name="Title" Value="$Resources:cmscore,IPPT_HomeWelcomePage_Title;" />

<Property Name="PublishingPageLayout" Value="~SiteCollection/_catalogs/masterpage/WelcomeLinks.aspx, ~SiteCollection/_catalogs/masterpage/WelcomeLinks.aspx" />

<Property Name="ContentType" Value="$Resources:cmscore,contenttype_welcomepage_name;" />

</File>

</Module>

</Modules>

</Project>

WebTempCustom.xml

<?xml version="1.0" encoding="utf-8"?>

<!-- _lcid="1033" _version="12.0.4518" _dal="1" -->

<!-- _LocalBinding -->

<Templates xmlns:ows="Microsoft SharePoint">

<Template Name="MyPublishingSample" ID="20010">

<Configuration ID="0" Title="MyPublishingSample" Hidden="FALSE" ImageUrl="/_layouts/images/mwsprev.png" Description="A site to test publishing Definition." DisplayCategory="CustomPublishing" > </Configuration>

</Template>

</Templates>

The custom publishing site definition is created successfully. Browsed to the central admin and found the custom publishing site definition. Created a top level site collection successfully using the custom publishing site definition. So far so good !!!!

Now browed to the site which was created using the custom publishing site definition and it blown with the following error message in the home page :

“An error occurred during the process of <custom master page name>.Error Message : CS0030 Cannot convert type Microsoft.SharePoint.WebControls.CSSRegistration to System.Web.UI.IAttributeAccessor.”

What went wrong when everything is perfect ? Does the custom master page get corrupt ? No, If I upload the document manually to the master page gallery and switch the site to use the custom master page then there are no issues and the master page applied successfully to the site. It tells me that the custom master page is not corrupted.

Does the file provisioning is wrong ? Not at all, the master page is available in the master page gallery ( I verified this by changing the system master page to OOB master page and browsed to the master page gallery)

Does the publishing site definition is wrong ? No, It’s perfect and the site was created successfully but unable to browse to the home page and it throws this weird type casting error.

Just gone mad for an hour !!!

Investigated the custom master page and found lot of “_designer” variables which have been added through the SPD. If I remove this variables then the master page applied successfully and the site is alive.

To test further, In the onet.xml file I changed the masterURL to use the OOB blueband.master for the system pages. Created a site collection again and the same issue. But now able to browse to the master page gallery as the system master page is OOB blueband.master. From continuous testing found that if I just edit the master page property from the master page gallery and save it without any updates then everything is working fine. Don’t know how the type casting error disappeared after the update !!

Decided to stick with this workaround and update the custom master page programmatically through the feature receiver. Made the changes to the feature.xml to the file provisioning feature as follows :

<Feature Id="EC3777F3-49C1-4e5e-A86E-9ED13DA6DC2A"

Title="SamplePublishing Custom Master Page"

Scope="Web"

ReceiverAssembly="MasterPageFeature,Version=1.0.0.0,Culture=neutral,PublicKeyToken=e2e43c90c0b598db"

ReceiverClass="MasterPageFeature.SetMasterPageNow"

xmlns="https://schemas.microsoft.com/sharepoint/" >

<Properties>

<Property Key="Name"

Value="agent1.master" />

</Properties>

<ElementManifests>

<ElementManifest Location="ElementManifest.xml" />

</ElementManifests>

</Feature>

Created a DLL for the feature receiver and deployed it in the GAC. The feature activated code is as follows :

public override void FeatureActivated(SPFeatureReceiverProperties properties)

{

// throw new Exception("The method or operation is not implemented.");

SPWeb oWeb = (SPWeb)properties.Feature.Parent;

oWeb.AllowUnsafeUpdates = true;

SPFeatureProperty oPropertyName= properties.Feature.Properties["Name"];

SPList oList = oWeb.Lists["Master Page Gallery"];

foreach (SPListItem oItem in oList.Items)

{

if (oItem["Name"].ToString().Trim().ToLower() == oPropertyName.Value.Trim().ToLower())

{

oItem.File.CheckOut();

oItem.Update();

oItem.File.CheckIn("Check in");

oItem.File.Publish("Published");

oItem.File.Approve("Approved");

}

}

oWeb.Update();

oWeb.AllowUnsafeUpdates = false;

}

Now everything is fine, Feature activated through feature stapling, Custom master page updated, Custom master page is applied successfully whenever you creates the site using the custom publishing site definition. Hope this blog walks you through the custom publishing site definition with custom master page and as well with the weird error my customer encountered.

So, If you encountered this weird error message ever in the custom master page just look for the “_designer” variables if they exists then update the master page through feature receiver after provisioning.

Good experience with master pages !!!