Saturday, April 19, 2008

Sharepoint 2007: Creating page from layout with “No parameterless constructor defined for this object” exception


After creating page from layout “No parameterless constructor defined for this object” exception was thrown.


I have created site definition using webtemp.xml. Inside site definition I have had few features activated and one of them created layouts for publishing template. Inside some I have added section for automatically adding default webparts using AllUsersWebPart element.

On development environment manually activating features worked perfect, but when I followed steps below:
• Create site based on custom site definition
• Create paged based on custom layout
I saw error like this:

No parameterless constructor defined for this object. 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 System.Activator.CreateInstance(Type type)
at Microsoft.SharePoint.WebPartPages.SPWebPartSerializer.get_DefaultControl()
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)


I found that removing template from “Master page and layout gallery” and adding it manually again fixed that problem. Of course this solution may be acceptable in development environment, but cannot be used in production environment with a lot of templates.


After few hours of investigation I found that site definition didn’t register web parts as safe controls. It wasn’t easy to discover and description of error didn’t make it easier.
Now, you may ask why semi solution worked? After removing layout from gallery AllUsersWebPart section has been reseted and there were no default webparts on page.


Anonymous said...

FYI: The missing entry in SafeControls causes same error on SharePoint 2010.
And....thanks a million...I doubt I would have found the solution in any forseeable future.

Jiggsoft said...

Thanks for the solution :)