Code fails with “A Web Part or Web Form Control on this Page cannot be displayed or imported. The type could not be found or it is not registered as safe”

One of my customer’s came up with a peculiar issue. They were having a number of custom web parts on their SharePoint site and everything was working great from the browser. But they had also created a timer job that enumerates web parts and it used to fail with these errors:

“A Web Part or Web Form Control on this Page cannot be displayed or imported. The type could not be found or it is not registered as safe.”  

-and-

“No Parameterless constructor is defined for this object.”

 

All the web.config entries were proper and also the website was working properly from web browser. After some troubleshooting, found that customer is having the web part assemblies in the BIN folder of the web application (under <drive>:\Inetpub\wwwroot\wss\VirtualDirectories\<port>\bin). They had a dependency on a 3rd party assembly which was not signed, therefore cannot put their assemblies in the GAC.

Now how does this translates to error from the custom timer job?

OWSTIMER is where the timer job is running, and when it accesses the WebPartManager, it tries to instantiate the web parts, and the web parts were deployed to the BIN directory of the web app. Since OWSTIMER is not a web application and does not know where is the BIN folder hosted so eventually doesn't have access to the web part and get this error.

You can read more about Assembly Placement at https://msdn.microsoft.com/en-us/library/2h3sywsc(VS.85).aspx.

 

So what was the solution in this case?

  1. Deploy the web parts to the GAC. This will require strong naming the assemblies, which was not possible in this situation.
  2. Copy the web part assemblies in question to the 12 directory. This will provide access to the web part assemblies to OWSTIMER. (Reference: Assembly Placement)

 

Hope this helps you save sometime while troubleshooting… as always… Happy Coding Smile