Since the title of this post doesn’t really do justice, let’s take a look at a few scenarios.
Scenario 1. You have a third party webpart and would like to alter it’s execution flow as much as it permits through public properties and methods.
Scenario 2. You have a legacy webpart and no source code; you would like to use a legacy webpart as a main part and add a few extra components and render them all as one webpart.
Since each SharePoint webpart inherits from System.Web.UI.WebControls.WebParts, requiring it to have a default constructor, you can render a webpart as part of another webpart in few simple steps.
We’ll start with creating a new Visual Studio 2010 SharePoint Solution and for template choose a Visual WebPart Project. Let’s call this project HostWebpart so it’s clear as we go along.
We’ll also need another webpart to which will be hosted within HostWebpart. Whether it’s one of your own webparts or a third party webpart, it doesn’t matter. You can download a free webpart you can use for testing from codeplex. This web part will be hosted within out HostWebpart. Well call it a Hosted Part.
Depending on the complexity of your Hosted Part, if it uses external resources such as CSS and controls, you will need to deploy the WSP first so all of the dependencies are in place.
Next, navigate to your portal and add a Hosted Part to the page. Once added, export the webpart declaration as shown below:

Save the file and open it in Notepad. The top part will show you the assembly name and the type we’ll reference later.
Here is the sample I have exported.
<webParts>
<webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
<metaData>
<type name="Microsoft.SharePoint.Publishing.
WebControls.ContentByQueryWebPart,
Microsoft.SharePoint.Publishing, Version=14.0.0.0,
Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
Since in my case, I’ve used out-of-the-box webpart, the Content by Query Webpart, my assembly is Microsoft.SharePoint.Publishing and the type name is Microsoft.SharePoint.Publishing. WebControls.ContentByQueryWebPart. In your case, remember those items, we’ll reference them in just a bit.
In your HostWebpart Visual Studio project, reference the assembly in the DLL References. Even though our HostWebpart is already deployed, we need a DLL reference so we can create an instance of it in the HostWebpart.
Now we can create an instance of the Hosted Part right within our Visual WebPart project (the HostWebpart). Open the code behind of the user control for your Visual WebPart (VisualWebPart1UserControl.ascx).
In the Page_Load event, create an instance of your Hosted Part by calling it by type you’ve recorded earlier, in my case I would define my Hosted Part like this:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace VisualWebPartProject1.VisualWebPart1
{
public partial class VisualWebPart1UserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart cqwp =
new Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart();
cqwp.ChromeType = PartChromeType.None;
// any other public properties or methods can be called in here
this.Controls.Add(cqwp);
}
}
}
Notice how I set the ChromeType property of the Content by Query Webpart. Well, this will ensure the border around it is not displayed and it will appear as it’s part of the HostWebpart.
In the actual ASCX control representing the Visual Webpart, you can add any elements you require, they will all render along with the Content by Query Webpart just as they were a single webpart.
Notice how we added our Content by Query Webpart to the main set controls:
this.Controls.Add(cqwp);
Well, if you like, you can add it to another control such as a panel or a repeater etc. This way you can control it’s rendering if required. Just add the panel or another component to your Visual Werbpart and then you can start adding Controls to it.
Of course, in this approach you’re limited to public properties of the Hosted Part in terms of altering its execution flow; but in many scenarios that’s all you need.
Try it out with a third party webpart!