Replace the Page Load event with the following code:
privatestatic Dictionary<string,string> _projectUrl;
protectedvoid Page_Load(object sender, EventArgs e)
{
_projectUrl =new Dictionary<string,string>();
_projectUrl.Add("Project 1", "/projectsite1");
_projectUrl.Add("Project 2", "/projectsite2");
// Create UI controls for Projects
foreach (string project in _projectUrl.Keys)
{
Button button =new Button();
button.Text = project;
button.Click += button_Click;
this.Controls.Add(button);
}
}
Add the following method for the button click event handler.
privatevoid button_Click(object sender, EventArgs e)
{
string url = _projectUrl[(senderas Button).Text];
SPSite site = SPContext.Current.Site;
SPWeb web = site.RootWeb;
web.AllowUnsafeUpdates = true;
SPLimitedWebPartManager manager = web.GetLimitedWebPartManager("SitePages/Home.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
foreach (System.Web.UI.WebControls.WebParts.WebPart w in manager.WebParts)
if (w is Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart)
{
Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart cwp = (was Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart);
SPWeb targetWeb = site.OpenWeb(url);
cwp.WebUrl = targetWeb.ServerRelativeUrl;
//set the guid of the list
cwp.ListGuid = targetWeb.Lists["Tasks"].ID.ToString();
PublishingWeb pweb = PublishingWeb.GetPublishingWeb(targetWeb);
cwp.Update(pweb);
manager.SaveChanges(w);
}
ClientScriptManager cs = Page.ClientScript;
String csName ="ReloadClientScript";
Type csType =this.GetType();
if (!cs.IsStartupScriptRegistered(csType, csName))
{
StringBuilder csText = new StringBuilder();
csText.Append("<script type=\"text/javascript\">");
csText.Append("this.location.reload(); </");
csText.Append("script>");
cs.RegisterStartupScript(csType, csName, csText.ToString());
}
}