Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand

Posts Tagged ‘content query web part’

Programmatically Refresh Content Query Web Part

Posted by Jean Paul on September 17, 2013

In this article we can explore a real life scenario of refreshing Content Query Web Part programmatically.


I hope the reader is having good knowledge of:

1. SharePoint page editing

2. Web Part insertion

3. Web Part creation using Visual Studio

4. Web Part Deployment


Following are the scenario items:

1. A home site with 2 web parts, project button web part & Content Query (CQ) web part

2. 2 project sites, each with Tasks list

3. On click of button, the particular project tasks should be shown in the CQ web part

How to achieve the same?


What is Content Query web part?

Content Query web part allows list querying & it is a part of Enterprise Content Management (ECM) functionality. You can add the web part from the Content Query category. You need to enable the Publishing feature to access the web part. Within code, the web part is called Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart

Step 1: Create Project Sites

Create 2 project sites of template Team Site & add some tasks into it. Make sure you remember the URLS.

The Project 1 Tasks list:


The Project 2 Tasks list:


Step 2: Design Project Buttons web part

Open Visual Studio & Perform the following:

1. Create a new Visual Web Part project of type Farm Solution

2. Add reference to Microsoft.SharePoint.Publishing namespace

3. Delete the existing web part inside the project

4. Create a new Visual Web Part & name it as ProjectButtonsWebPart

5. Replace the Page Load event with following code.

private static Dictionary<string, string> _projectUrl;

protected void 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;




6. Add the following method for button click event handler.

private void button_Click(object sender, EventArgs e)


string url = _projectUrl[(sender as 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 = (w as 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);




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(); </”);


cs.RegisterStartupScript(csType, csName, csText.ToString());



7. Ensure you have the following namespaces included.

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Publishing;

using Microsoft.SharePoint.WebPartPages;

8. Resolve any namespace issues like System.Web.UI.WebControls.WebParts.WebPart

9. Build the project

10. Deploy the project

11. Insert the Project Buttons web part to the home page. It looks like below.


Step 3: Insert the Content Query web part

Now we can insert the Content Query web part into the home page. Open page in edit mode & insert web part from the Content Rollup group as shown below.


Edit the web part & make the Presentation > Link property as empty.


Now the page looks like:


Step 4: Test the buttons

Now click on the Project 1 button & you should see the Content Query web part updated with tasks.


Now click on the Project 2 button & you should see the tasks changed with Project Site 2.


To summarize the activities, please note the following:

1. The Project Buttons web part have 2 hard coded paths inside it

2. On click of the button, the Content Query web part is refreshed by changing the List ID property

3. After the button click is processed, a client side startup script is registered to reload the page

4. Now on clicking the button, the content query web part will display tasks list from the corresponding project site.



In this article we have explored a typical real world scenario of refreshing a web part from another web part. I hope this will be useful to you in SharePoint development.

The source code can be downloaded here.

Posted in SharePoint, SharePoint 2010 | Tagged: , , , , , , , | Leave a Comment »