Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>public class WebPartWrapper : System.Web.UI.WebControls.WebParts.WebPart { protected override void CreateChildControls() { Panel pnl = new Panel(); this.Controls.Add(pnl); var factory = new WebPartFactory() WebPart dynamicPart = factory.CreateWebPart("RSSViewer", this.Guid); pnl.Controls.Add(dynamicPart); } } 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 | BindingFlags.NonPublic; 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); } } </code></pre> <p>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:</p> <ol> <li><p>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.</p></li> <li><p>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 <a href="http://cid-3a746453fc8ad1ba.skydrive.live.com/embedphoto.aspx/Offentlig/xml.PNG" rel="nofollow noreferrer">this</a>..</p></li> </ol> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload