Wednesday, March 26, 2008

Unable to add selected Webpart(s)...

I got the error above in the following situation:

I developed a custom masterpage and on that masterpage I added a Label control. Afterwards, the label is filled up with some list data (in my case: information from a page library) . Maybe I could work with a webpart, but I don't think it's evident to put a webpart on a masterpage.

Everything worked fine until I saw that I coudn't add any webpart anymore. Believe me, it was hard to find out the cause of the problem. After excluding many possibilities I discovered that it was something in my masterpage. And guess what ... my innocent label was the cause of the whole thing!

Solution:
Because of the fact that my code works fine in DisplayMode of a page, I surrounded my code with the following condition:

if (SPContext.Current.FormContext.FormMode.Equals(SPControlMode.Display))
{
//Populate the "innocent" label
}

The condition will check whether you are in DisplayMode are not... If so... populate the label.

3 comments:

Saurabh said...

Hi

m doing the same thing.....i have created our own Custom MasterPage....in my masterpage...i put one Content Placeholder name "CPH"

now m adding an Aspx page by using Features......i put WebpartZone in that ContentPlaceHolder "CPH"

It shows fine in Edit Mode ....then i create a simple "Hello World" webpart ....now i want to add this webpart in my WebpartZone ....when i click on link "Add a Web Part" of WebPartZone....i select hello world webpart...then ok

it shows an Exeption
"Unable to add selected web part(s).
HelloWorld: Exception occured. (Exception from HRESULT: 0x8002009 (DISP_E_EXCEPTION))



whats wrong in this.....is there m doing wrong??? plz help me out....

Kind Regards
Saurabh

bountykiller said...

I had the same error too. The reason was that I used the "using" keywords when retrieving my current web.
The things is that it seems that we must NOT use the "using" keyword when retriving webs or sites from the SPContext (using SPContext.Current.Site or SPContext.Current.Web).

Hope this helps.

Saurabh said...

But i did not use "Using" keyword...
i am done SPSite site=SPContext.Current.Site;

what happend to it.......??