Tuesday, March 04, 2008

Unable to add selected web part(s).

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:

  1. Make sure the control is registered as safe in the web.config (duh...actually the error says what it means, right)
  2. Make sure the assembly is accessible and in the [port]bin folder. (obvious, but worth mentioning)
  3. Make sure the assembly name in the *.webpart definition file, matches the assembly name in the safe control element in web.config
  4. 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.
  5. 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.
  6. 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.
  7. 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.

30 comments:

Anonymous said...

Check that you accidentially forgot to declare the web part as public ;-)

Anonymous said...

Check that the code you run has enough rights as defined in the CAS policy / trust level.
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.

Saurabh said...

Hi Mikhail....

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

Mikhail Dikov said...

Did you try any of the above tips? Do they work?

Saurabh said...

Hi Mikahil

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.......

Mikhail Dikov said...

Where do you store your page. Do you provision it in the web site, do you create it in a document library, or you use an aspx page in the _layouts folder?

Unknown said...

I have same problem with MOSS 2007 on my server machine. “Unable to add selected web part(s)”. Strange thing is that I cannot add web part when I access my page over my public IP, and from local that’s no problem. Another strange thing is that this is the case with Microsoft build in web parts and mine custom ones, but not with KWizCom web parts. If this is permission problem I’m not sure where to look next. I think I have checked everything possibly regarding permissions. One more thing, installation is completely new, server 2003, sql 2005 and moss 2007 all just few days old .

Mikhail Dikov said...

Are you sure your web part does not require specific CAS configuration? One quick way to see if that's the issue is to change the trust level to Full and run your web parts. If that's the case analyze your assembly and create proper CAS policies or use WSPBuilder, which does that for you.

Unknown said...

Well the problem is that neither custom nor build-in web parts are able to be added on page. It’s completely new installation at the moment, with no custom web parts deployed. Just those build-in by Microsoft as part of MOSS 2007. The strange thing is that everything was working fine until one day, then no more web parts couldn’t be added over internet. Now we have followed previous procedure of installation. New HDD, formatted, with new windows 2003 installation, all required installations have been made, but now we can’t add web parts from start. Also, at this time only administrator is active user on MOSS on that machine…so first I would like to know is it normal that as administrator, with all full permissions I’m not able to add web parts from start.

Mikhail Dikov said...

nebojsa, why don't you try to post this to one of the SharePoint usergroups. There are many people that track these and that can give good suggestions. From your description it could be an issue with the way you setup your service accounts, but it's hard to tell.

Anonymous said...

Make sure that the type listed in th .webpart file is exactly the same as the class file. For instance, my webpart type was "Company.OrderCheckIn.OrderCheckInWebPart" and my .webpart file referenced "Company.OrderCheckIn.OrderCheckinWebPart". Notice the lowercase "I" in th e.webpart file. that was what was killing me.

Anonymous said...

Thanks for the tips!

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.

Anonymous said...

One stupid mistake I made was to forget to make the web part class public.

Unknown said...

Ok, we did not manage to get those web parts on our page, but when i extended web app to extranet zone, and set host header for that web app web parts are working, they can be inserted to a page. So that worked for us, not sure what i’m missing still…

Anonymous said...

Hey, guy who said:

"Check that you accidentially forgot to declare the web part as public"

Thanks man. Can't believe I missed that.

Anonymous said...

Make sure when you change the signing file "*.snk" you build the solution and get the new PublicKeyToken of it with "sn.exe -T yourwepart.dll". Then change the PublicKeyToken in manifest.xml, solutionConfig.xml and yourwebpart.webpart.

Note: I use STSDEV for webpart development.

Tom said...

My troubleshooting today ended at step 1 of 7. Thanks, Mikhail!

Ian Campbell said...

Check the site collections webpart gallery!.

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.

Unknown said...

i'm too newbie in SharePoint. plz let me know how can i add an entry in web.config to introduce my webpart as safe for it.
thx

Unknown said...

@AndersR:
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.

Matthew Robinson said...

I have the exact same problem as Nebojsa. I have pulled my hair out already. Works fine internally but we have one custom webpart that wont work or be added from a public IP. Not registered as safe is all I get.

Anonymous said...

If you have renamed your namespace, verify that the namespace name changed on all places! (for example: SharePointProjectItem.spdata)

G said...

Also make sure your Webpart is inheriting from the SharePoint webparts



Microsoft.SharePoint.WebPartPages.WebPart

Mikhail Dikov said...

G, this statement is not true. YOU SHOULD NOT inherit from the SharePoint web part namespace as you suggest, but from the ASP.NET library System.Web.UI.WebControls.WebParts.

Sam said...

I usually get that error. Is there a way for me to avoid it?

web design Perth

Anonymous said...

Ok , this may be very late but will be useful for the rest. I created a webpart and was getting the error "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."

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.

Hari Gillala said...

This can happen for the following reason.

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

Chris Herdt said...

The Namespace attribute of the web.config SafeControl entry can be confusing.

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

Mikhail Dikov said...

Chris, the example is for SharePoint 2003, in SharePoint 2007 and later the best practice for most cases is to use the ASP.NET namespace. Here is a bit more info http://tinyurl.com/3uvlz44

Johan said...

Good article, unfortunately it didn't help... :( still get the same error...