0

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;
            try
            {
                page = currentUnsafeWeb.GetFile("beneficiaries.aspx");
                page.CheckOut();

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


                page.CheckIn(string.Empty);

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

            Logger.LogDebug("NLSubsidiesSiteConfigSubsidyCentralEventReceiver", "ConfigureDashboardBeneficiaries(SPWeb currentWeb)", "END");
         }
1

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);
2

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

  • 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. – Luis Valencia Aug 17 '12 at 10:08
  • 1
    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
1

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
pageMgr.SaveChanges(wp);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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