Saturday, 13 October 2012

"No parameterless constructor defined for this object" exception while creating page from custom page layout


Hello All,

I faced one wired problem (parameter less constructor defined for this object.) while creating page from my custom page layout.  Here I am discussing this wired problem and its possible solutions.

I was getting following exception:

 Exception information:
    Exception type: MissingMethodException
    Exception message: No parameterless constructor defined for this object.

Stack trace:   
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache)
   at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at Microsoft.SharePoint.WebPartPages.SPWebPartReflectionHelper.GetDefaultControl(Type controlType)
   at Microsoft.SharePoint.WebPartPages.BinaryWebPartSerializer.GetDefaultControl()
   at Microsoft.SharePoint.WebPartPages.BinaryWebPartSerializer.Serialize(PersonalizationScope scope)
   at Microsoft.SharePoint.WebPartPages.BinaryWebPartSerializer.get_Links()
   at Microsoft.SharePoint.WebPartPages.SPWebPartManager.AddWebPartToStore(WebPart webPart, Int32 viewId, String viewGuid)
   at Microsoft.SharePoint.WebPartPages.SPWebPartManager.AddWebPartInternal(SPSupersetWebPart superset, Boolean throwIfLocked)
   at Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager.AddWebPartInternal(WebPart webPart, String zoneId, Int32 zoneIndex, Boolean throwIfLocked)
   at Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager.AddWebPart(WebPart webPart, String zoneId, Int32 zoneIndex)
   at Microsoft.SharePoint.Publishing.PublishingPage.CopyAllWebParts(String destinationPageUrlServerRelative, SPWeb destinationPageWeb, String sourcePageUrlServerRelative, SPWeb sourcePageWeb, Boolean shouldOverwriteDestinationWebParts)
   at Microsoft.SharePoint.Publishing.PublishingWeb.<>c__DisplayClass1c.<AddPublishingPage>b__17()
   at Microsoft.Office.Server.Diagnostics.FirstChanceHandler.ExceptionFilter(Boolean fRethrowException, TryBlock tryBlock, FilterBlock filter, CatchBlock catchBlock, FinallyBlock finallyBlock)
   at Microsoft.Office.Server.Diagnostics.ULS.SendWatsonOnExceptionTag(UInt32 tagID, ULSCatBase categoryID, String output, Boolean fRethrowException, TryBlock tryBlock, CatchBlock catchBlock, FinallyBlock finallyBlock)
   at Microsoft.SharePoint.Publishing.PublishingWeb.AddPublishingPage(String name, PageLayout layout, SPFolder folder)
   at Microsoft.SharePoint.Publishing.Internal.CodeBehind.CreatePagePage.NewPageItemSave(String pageName, PageLayout pageLayout, String folderUrl)
   at Microsoft.SharePoint.Publishing.Internal.CodeBehind.CreatePagePage.CreateStandardPage(String pageName)
   at Microsoft.SharePoint.Publishing.Internal.CodeBehind.CreatePagePage.ButtonCreatePage_Click(Object sender, EventArgs e)
   at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
   at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Solution:
After doing lots of research it seems that there were multiple possible causes for this exception. At least I found two solutions.

Solution1:
One of the reason for this type of error is wrong casing of <asp:content> control tag. This control tag should be always like <asp:Content> and should be closed in the same way like </asp:Content>.
Also case for opening and closing tags must be matched.

Solution2:
In Elements.xml file of the feature which deploys the page layouts in master page gallery, for the page layout which throwing an exception, I have added empty <AllUsersWebPart> tags and no web part definitions in between the <AllUsersWebPart></AllUsersWebPart> tags. I forgot to add the webpart definitions. And that why the above error was coming when I create new page using this page layout.
I added the webpart definition and it worked like charm J

Thanks!