SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is the current scenario.

I have a central site called: XXX.

I have many subsites called with the year name: 2012, 2011, 2010, etc.

In the central site I have a list called Beneficiaries.

In ALL year sites I have a list called Request, every one of those lists have a lookup column to the Beneficiaries list in the central site.

I need to create one webpart in the central site to show all the Requests that a Benefeciary has in all sub sites. (year sites). I wonder if the contentquery webpart will do the work because I think I will have to use spsitedataquery not use spquery.

This is the code I have so far.

/// <summary>
        /// Update dashboard beneficieries.
        /// </summary>
        /// <param name="currentUnsafeWeb"></param>
        private void UpdateDashboardBeneficiaries(SPWeb currentUnsafeWeb)
            Logger.LogDebug("NLSubsidiesSiteConfigSubsidyCentralEventReceiver", "UpdateDashboardBeneficiaries(SPWeb currentUnsafeWeb)", "BEGIN");

            SPFile page = null;
                page = currentUnsafeWeb.GetFile("beneficiaries.aspx");

                //Add Content Query WebPart or something else to show all request in all yearsubsites that are related to the beneficiaries list.


            catch (Exception)
                if (page != null) page.UndoCheckOut();

            Logger.LogDebug("NLSubsidiesSiteConfigSubsidyCentralEventReceiver", "ConfigureDashboardBeneficiaries(SPWeb currentWeb)", "END");
share|improve this question
up vote 1 down vote accepted

As mentioned you can use SPLimitedWebPartManager to add webparts programtically. The sample code for SPSiteDataQuery looks like this: SPSiteDataQuery query = new SPSiteDataQuery();

                    //Specify the fields which need to be shown.
                    query.ViewFields = "<FieldRef Name=\"Title\" />" +
                                        "<FieldRef Name=\"Type\" />" +
                                       "<FieldRef Name=\"AssignedTo\" />" +
                                       "<FieldRef Name=\"Status\" />" +
                                       "<FieldRef Name=\"DueDate\" />";

                    query.Lists = "<Lists BaseType='0'/>";
                    query.Query = "<Where><Lt><FieldRef Name='ID' /><Value Type='Counter'>0</Value></Lt></Where>";

                    // Query all Web sites in this site collection.
                    query.Webs = "<Webs Scope=\"SiteCollection\" />";
                    DataTable table = web.GetSiteData(query);
share|improve this answer
thanks, this pointed me I think in the right direction, I posted another question related to this one. if could check it please.… – Esteban V Aug 17 '12 at 12:37

To add webpart programatically you should use SPLimitedWebPartManager

For an example (including ContentByQueryWebPart) see How to create Web Part instances on pages using Features - FeatureReceiver - SPLimitedWebPartManager

share|improve this answer
that part I know how to do it, the big concern here is how to set the query that the webpart should use specially becase its a query across subsites in the same site collection. – Esteban V Aug 17 '12 at 10:08
The ContentByQueryWebpart uses a SPSiteDataQuery to query all subsites of the WebUrl property. Start by configuring it in the UI, export and use those properties. You'll not be able to specify the Request(s) lists by name or url, but hopefully it has a unique list type and/or content type. – Per Jakobsen Aug 17 '12 at 10:23

Sample code for doing the same if given below.

ContentEditorWebPart wp = new ContentEditorWebPart();
//Add the webpart to the page
SPLimitedWebPartManager pageMgr = web.GetLimitedWebPartManager(pageURL, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
pageMgr.AddWebPart(wp, "Main", 0);

//Save the page with the changes
share|improve this answer
thanks, this pointed me in a good direction, however I have another question here:… – Esteban V Aug 17 '12 at 12:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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