Home > .Net, ASP.Net, C#, SharePoint, WebParts > Dynamically adding WebParts using WebPartManagerInternals class

Dynamically adding WebParts using WebPartManagerInternals class

I’m currently working on a project the requires the use of WebParts to allow a user to construct custom reports by dragging and dropping WebParts into WebPartZones. I needed to dynamically create these WebParts on the fly and add them to the page programmatically. I tried to do this by using the standard AddWebPart() method of the WebPartManager control but found that using this method the WebPart doesn’t seem to be added to the page correctly which seems to be because the AddWebPart() methods gives the WebPart a random ID therefore it cannot participate in ViewState management. Furthermore because of the need to add the WebPartZones within the Page OnInit event the WebParts were getting recreated each time the page did a postback.

I searched around the web and found lots of people having the same problem but very little in the way of solutions to the problem. Eventually I stumbled across a forum post that mentioned the WebPartManagerInternals class. The documentation fails to mention exactly how to use this class but contained the following text:

“Isolates into a separate class methods that are used by the WebPartManager control and can be overridden by developers who extend the control, but are rarely needed by page developers.”

It also mentions that the class is a sealed class meaning you cannot derive from it and a quick look in Reflector showed me that it’s contructors were internal. This meant I needed to access the class from within the WebPartManager class.

I created a new DynamicWebPartManager class that derived from WebPartManager and in this class I created a method called AddDynamicWebPart(WebPart webPart, WebPartZone zone) which as you can see accepts a WebPart and a WebPartZone. In this method I called two methods on the Internals property (beware this Internals property DOE’S NOT show up in Intellisense) AddWebPart() which accepts a WebPart and SetZoneID() which tells it which WebPartZone to add the WebPart to.

public class DynamicWebPartManager : WebPartManager
{
public void AddDynamicWebPart(WebPart webPart, WebPartZone zone)
{
Internals.AddWebPart(webPart);
Internals.SetZoneID(webPart, zone.ID);
}
}

Now by using this DynamicWebPartManager class on your page instead of the out of the box WebPartManager class you can add WebParts by calling the AddDynamicWebPart() method.

WebPartZone reportZone = new WebPartZone();
reportZone.ID = “reportZone1″
reportZone.HeaderText = “My Report”;
ToolBoxPanel.Controls.Add(reportZone);

TestTableWebPart webPart = new TestTableWebPart();
webPart.ID = “webPart1″
webPart.Title = “My Report – Table”;
ReportBuilderWebPartManager.AddDynamicWebPart(webPart, reportZone);

kick it on DotNetKicks.com

Categories: .Net, ASP.Net, C#, SharePoint, WebParts
  1. Jonathan
    February 15, 2008 at 3:59 pm | #1

    Hi…

    How do I to instance the class WebPartManagerInternals ?

    Because in my class thorw the excepion that my Internals is null

    thanks

  2. February 17, 2008 at 9:13 am | #2

    thanks!

  3. Jonathan
    February 19, 2008 at 1:53 pm | #3

    As I make to instance the class WebPartManagerInternals ?

    Thanks

    ps:sorry, I don’t speak english, but I need so much know this

  4. February 20, 2008 at 9:44 am | #4

    Hi Jonathan,

    I think you need to re-read the article a bit more in depth as it mentions this.

    “It also mentions that the class is a sealed class meaning you cannot derive from it and a quick look in Reflector showed me that it’s contructors were internal. This meant I needed to access the class from within the WebPartManager class.”

    You cannot create an instance of the class as it’s sealed. However it is exposed within the WebPartManager class. Hope that helps.

  5. bensoy
    April 3, 2008 at 2:19 am | #5

    Hi Lee,

    What is ReportBuilderWebPartManager? is it the name of the namespace of the class? another question is that how can you grad the id of the dynamically added webparts in the zone? how can i get the webparts id in the Page?

    Waiting for your replies..

    Thanks and Regards,

  6. May 8, 2008 at 9:35 am | #6

    bud, just wanted to post to thank you for this f***ing awesome article, it worked perfectly and made my day definitely since i have been for more than a working day trying to fix this problem…

    BIG THANKS

  7. Thao
    May 8, 2008 at 6:23 pm | #7

    I had the same problem, I had looked for it through many sites but they don’t have a right solution. Now by your suggestion I’ve finished with it.

    Thank you very much !

  8. June 4, 2008 at 12:52 pm | #8

    Nice one!!, i was exactly looking for this.
    I actually created my own editors within my parts, so i have inline editing but since i do not wont to use the AJAX framework i decided to use Jquery to achieve drag and drop for all browsers. now that i found out of this Internals my life will be easier, it even solve my next feauture… part templated page with part properties loaded. so a user can save a page as a templated page and use it over again.
    Anyways,
    thanks.

  9. mkhan
    June 4, 2008 at 2:08 pm | #9

    Hi Lee,
    I have done exactly what you have described in this article. But the web part is not added in the zone. I am instantiating DynamicWebPartManager calling AddDynamicWebPart() from Init(). I am not getting any error though. It just displays empty zone. Am I missing something?

    Thanks.

  10. August 18, 2008 at 1:57 pm | #10

    Nice blog. Helped me! Thanks. Marc

  11. chithra
    August 28, 2008 at 9:55 am | #11

    I also did code described in this article. But the web part is not added in the zone.

    DynamicWebPartManager WebPartobj = new DynamicWebPartManager();
    Microsoft.Web.Preview.UI.Controls.WebParts.WebPartZone zone1 = new Microsoft.Web.Preview.UI.Controls.WebParts.WebPartZone();
    zone1.ID = “zone1″;
    zone1.HeaderText = “zone1″;
    MyZone.Controls.Add(zone1);
    Control UserControl1 = LoadControl(“WebUserControl1.ascx”);
    UserControl1.ID = “WebUserControl11″;
    WebPart myWebPart1 = WebPartManager1.CreateWebPart(UserControl1);
    myWebPart.ID = “myWebPart1″;

    WebPartobj.AddDynamicWebPart(myWebPart, zone1);

    It just displays empty zone. Please Help me to do this.

  12. Mahendiran
    September 25, 2008 at 6:38 am | #12

    chithra ,
    i am also getting same issue. can u tel me how to fix this…

  13. Mahendiran
    October 14, 2008 at 7:45 am | #13

    hi,
    pls any one send me the whole working code for Dynamically adding WebParts….

  14. Dravid
    October 16, 2008 at 10:39 am | #14

    Hi Lee,

    In your article everything is fine, but i can’t understand TestTableWebPart webpart = new TestTableWebPart(); how to i use this.
    please give me the solution. last two days i am working this issue.

    Regrads,
    Dravid

  15. Jack
    October 17, 2008 at 10:02 am | #15

    hi,

    Could you please tell me what is TestTableWebPart? you have created one object for the TestTableWebPart, but you didnt mention any thing about that. please explain me.

    Thanks
    JACK

  16. Brian
    October 24, 2008 at 4:02 am | #16

    Kick ass! You saved me from a potential roadblock Lee. Thanks for shedding the light on this.
    thanks-
    -BR

  17. jamie
    June 16, 2009 at 5:04 am | #17

    I also am having the problem that the Web Part isn’t being created after the methods are called – anyone find a solution to this? I’ve tried loading both a user control, as well as a simple Calendar control just to get it to work and it doesn’t create the Web Part on the page atall.

  18. July 27, 2009 at 4:38 pm | #18

    Hi,

    I am in great touble, please help me out.

    I tried your code is working 100% correctly. now i will tell you my scenario.

    i am dynamically adding usercontrol as webpart. Inside this usercontrol i have content that should be update as user saves the content by pressing saving button inside this usercontrol. Problem arises when postback even of that save button fired, two time creation of webparts methods are called, because our creating webparts should be called whenever any server side event occurs and another time i am manually calling it so that content is updated.

    If you got any question in understanding the problem please do reply me.

    My email id is sumanmodi@gmail.com

    Waiting to hear from you.

  19. Manish Prajapati
    August 6, 2009 at 10:48 am | #19

    Hi Lee Dale,

    Can you please provide me the code for the same…?

    It is very urgent.

    Regards,
    Manish

  20. Abhijit
    August 20, 2009 at 6:46 am | #20

    I created webpart site, It run through VS 2008, but I make virtual directory in IIS 6.0 but it not run the site , mean it not add the webpart dymanicaly

  1. No trackbacks yet.