I created a webpart with many properties.

I want add it in EventReceiver and set properties.

How do use MyWebpart in EventReceiver?

  • You cannot use one webpart within another webpart. You can create a webpart connectivity which will transfer values from one webpart to another.
    – user2536
    Commented May 2, 2012 at 8:10
  • I want add a webpart to special page in special eventreciver. There is no solution؟!
    – ar.gorgin
    Commented May 2, 2012 at 8:16

2 Answers 2


You should use SPLimitedWebPartManager and it's AddWebPart method for this purpose.

To create the SPLimitedWebPartManager object targeted to a specific page, use SPWeb.GetLimitedWebPartManager method.

Code example:

var webpart = new MyWebPart();
webpart.Title = "This is my webpart!";
webpart.MyCustomProperty = "Hello";
using (var manager = web.GetLimitedWebPartManager("/site-relative/path/to/page.aspx"))
    manager.AddWebPart(webpart, "Main", 1);

Actually, this is a common way to add a webpart to a page in SharePoint. And I don't see why it would not work from within an event receiver.

P.S. Also I suspect, that depending from the kind (sync/async) of the event receiver, you may have to run this code with elevated privilegies.

  • Thanks, I have a wsp file , that active feature in server . when how do new MyWebPart in solution ?
    – ar.gorgin
    Commented May 3, 2012 at 3:38
  • Sorry I'm not sure I understand what you mean. Do you mean you have two separate wsps? In this case, you can add a reference to the assembly which stores MyWebPart class to your solution, and then, the corresponding using statement to the event receiver - this way you can use class identifier in your code. Commented May 3, 2012 at 8:01
  • yes, It is possible؟؟ . another quastion : if i use this code for add webpart , how do open edit webpart in page ?
    – ar.gorgin
    Commented May 3, 2012 at 8:11
  • This is another question. I would recommend you to post it separately. Though, a quick Google search led me to the following post, which describes how you can open a webpart tool pane, if you know the webpart's ID (you can grab it easily from the SPLimitedWebPartManager object): spdeveloper.net/2011/07/… Commented May 3, 2012 at 8:21

Please find the answer below:


I will reproduce the answer here (for convenience), It is not my code, I checked it and it should be OK.

public class WebPartWrapper : System.Web.UI.WebControls.WebParts.WebPart {
    protected override void CreateChildControls() {    
        Panel pnl = new Panel();
        var factory = new WebPartFactory()
        WebPart dynamicPart = factory.CreateWebPart("RSSViewer", this.Guid);

public class WebPartFactory {
    public WebPart CreateWebpart(string webpartName, Guid parentWebPartGuid)
        var config = ConfigurationFactory.LoadConfiguration(webpartName);

        Assembly webPartAssembly = Assembly.Load(config.Assembly);
        Type webPartType = webPartAssembly.GetType(config.Class);
        object actualWebPart = Activator.CreateInstance(webPartType);

        foreach (var item in config.Properties)
            PropertyInfo webPartProperty = webPartType.GetProperty(item.Name);
            object webPartPropertyValue = Convert.ChangeType(itemValue, Type.GetType(item.Type));
            if (!String.IsNullOrEmpty(item.Value))
                webPartProperty.SetValue(actualWebPart, webPartPropertyValue, null);

        RunMethod("set_StorageKeyInternal", actualWebPart, new object[] { parentWebPartGuid });
        return actualWebPart as WebPart;

    private void RunMethod(string methodName, object objectInstance, object[] methodParameters)
        BindingFlags flags = BindingFlags.Instance | BindingFlags.Public |

        Type t = objectInstance.GetType();
        MethodInfo m = GetMethod(t, methodName, flags);
        if (m != null)
            m.Invoke(objectInstance, methodParameters);

    private MethodInfo GetMethod(Type instanceType, string methodName, BindingFlags flags)
        MethodInfo m = instanceType.GetMethod(methodName, flags);
        if (m != null)
            return m;

        if (instanceType.GetType() == typeof(object) || instanceType.BaseType == null)
            return null;

        return GetMethod(instanceType.BaseType, methodName, flags);

This code needs some explaining... Please excuse me if it does not compile, I had to remove a fair bit of the original code, it was very implementation specific stuff. I've not shown the "config" class either, it's just a container for configuration of webparts, just a bunch of properties. There are 2 issues I'd like to discuss in more detail:

  1. parentWebPartGuid - This is the Guid (UniqueId?) of the hosting webpart. For some reason we have to set "StorageKeyInternal" to this value, using reflection (it's a private property). You can possibly get away with not setting it, but at least for the majority of webparts we had to set it.

  2. config.Properties - This is the config values (we set them in a custom .xml file, but feel free to get this from anywhere). It can look a little like this..

In our framework we also support stuff like dynamic property values etc., but that's for another day... Hope this all makes sense and can help somebody.

  • Thanks , but i don't understand your code. what is (ConfigurationFactory)?
    – ar.gorgin
    Commented May 2, 2012 at 8:56
  • It's explained. It's not my code but I manage to understand it. It's a place to store some values for eacht webpart. U can change this to a xmlfile, database, SPList, or a generic collection... This code is loaded a webpart based on its name, because we need to know its asembly and classname we are storing those values in "ConfigFactory" for easy accessing Commented May 2, 2012 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.