Every once in a while when you create a web part or upgrade web part, or do some of things developers do when developing web parts, there comes the chilling moment, when you see an error message such as the one below:
So the question is what do we do in this case? Before you hit the discussion boards, or worse, start pulling your hair, here are couple of tips you can use to troubleshoot the issue:
- Make sure the control is registered as safe in the web.config (duh...actually the error says what it means, right)
- Make sure the assembly is accessible and in the [port]bin folder. (obvious, but worth mentioning)
- Make sure the assembly name in the *.webpart definition file, matches the assembly name in the safe control element in web.config
- Make sure you don't have more than one *.webpart file for the same web part in the web part catalog. This may happen if you changed the name of the *.webpart file.
- Restart IIS to start clean. Attach the debugger to the w3wp.exe process and try to load the page with the rogue web part. This way you can determine the exact location of the assembly you are loading.
- Check if the web part class exists by opening the assembly with reflector. This might sound funny, but in a bigger team, when different versions of assemblies are flying around it is very easy to overlook something and to use the wrong version, which so happens does not contain the web part class at all.
- If you have other tips or suggestions, please add them as comments.
Phew, I think I dodged that one... It turned out I got an older version of the assembly and my web part class was not even there.
Dovizhdane!
Unable to add selected webpart(s). 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.
Comments
Often you start developing a blank web part, and everything looks fine, but as you add code that demands higher CAS privileges the code might fail when you remove and re-add the web part.
I am also getting this type of error .....but my error is something different from this....
"Unable to add selected web part(s).
HelloWorld: Exception occured. (Exception from HRESULT: 0x8002009 (DISP_E_EXCEPTION))
this is my error....when i m adding a simple Hello Webpart it shows me this exception
I have checked the tips u have given........but its all fine at my side.....
what i have done....is I am adding two webparts in PageLayouts in some aspx page.....and i create connection between them in same aspx page.....my connection works fine.....but the Webpart Menu is not showing up ("Minimize","Close").....and if i want to add any other Webpart by Clicking on Add Webpart in Webpartzone then it show an error.....Unable to Add Selected webpart.This webpart is deleted by other user.
i can't figure out what's the problem is.......
I was going nuts trying to get this working, just to find out I had a minor typo in the namespace in the .webpart file.
"Check that you accidentially forgot to declare the web part as public"
Thanks man. Can't believe I missed that.
Note: I use STSDEV for webpart development.
I have see an old copy of the .webpart file get stuck in there. if you've change the assembly reference it might be picking it up from this old copy.
thx
Ha. I was pulling my hair out, and I realized I forgot to declare it public... Thanks for the tip!
@hamidi:
Locate your "web.config" file for your site. Open the file and search for the tag "SafeControl", this is where you add the entry. Add your entry at the bottom of this list. It would be safe to follow the same format as the ones above if you need basic permissions.
This post by MSDN briefly goes over how to create a Web Part, including how to set the security level and configuring the web.config file:
Walkthrough: Creating a Custom Enterprise Search Web Part
If you follow along from the top, it walks you through on how to create a webpart that searches for Authors of documents in document libraries.
Microsoft.SharePoint.WebPartPages.WebPart
web design Perth
I figured that I was using a Chart inside this webpart and once I removed the reference of the chart, it worked. So, the Chart control has to be also marked as typesafe.
Make sure all the references in the webpart are type safe.
This is security problem, So When you adding a web part it should be given CAS Permission.
So while adding a project in VS 2008, it will ask for BIN or GAC Deployment. So if you select GAC Deployment then , It will work without a problem.
Hari Gillala
My .cs file declares a namespace of TestWebPart, but the Namespace attribute should match the value of the type name in the .webpart file (which in my example was TestWebPart.TestWebPart).
Mikhail, why do you say the web part should not inherit from Microsoft.SharePoint.WebPartPages.WebPart? As far as I know, if you want a SharePoint web part to have custom properties, you must inherit from Microsoft.SharePoint.WebPartPages.WebPart. (See example at Creating a Web Part with Custom Properties