0

First of all I'd like to say that I'm very new at working with Visual Studio 2013 and with C# as well.

I want to make a certain web part in Visual Studio 2013 for a SharePoint 2013 test environment. The web part needs to fetch list items from different announcement lists, these announcement lists can be on different sites. Basically I need to be able to show the content of all those announcement lists into one custom list.

Here's an example of the scenario:

Subsite A: has a list named "List X"
Subsite B: has a list named "List Y"
Subsite C: has a custom web part that shows items from "List X" and "List Y"

So I want to make a custom web part in Visual Studio that will be placed on subsite C, which will show items from other lists located on other subsites.

I can deploy to our test environment and I can add a custom web part to a page so I got that going for me which is nice, but I just need a little push.
Some suggestions on what I might need (from the toolbox perhaps) or how I can get data from those SharePoint lists (suggestions, links, anything), that would all be very welcome.

Your help and time is greatly appreciated!

1
  • When starting out you might want to get a very basic web part working that shows items from one list. Then add the second and so on... Commented Mar 27, 2014 at 19:11

1 Answer 1

2

You can use SPSiteDataQuery for getting List data from multiple sub sites of same site collection.

Below is the basic code to start with ,bywhich you can use to get all announcement list items from the different sub sites:

using (SPSite oSPsite = new SPSite(SPContext.Current.Web.Url))
        {
            using (SPWeb oSPWeb = oSPsite.OpenWeb())
            {
                // Fetch using SPSiteDataQuery
                SPSiteDataQuery query = new SPSiteDataQuery();
                query.Lists = "<Lists ServerTemplate=\"104\" />";//104=List template ID for "Anouncements"
                query.ViewFields = "<FieldRef Name=\"Title\" />";//Add other fields which you want to get.
                query.Query = "";//you can add your caml query if you want to filter the list items returned
                query.Webs = "<Webs Scope=\"SiteCollection\" />";//Scope is set to site collection to get data from all anouncement lists in that site collection.
                DataTable dataTable = oSPWeb.GetSiteData(query);
            }
        }
1
  • 1
    I managed to make a list with list data from multiple sub sites, but I'm stuck at changing the column headers so that the user can sort the content (ascending/descending). I thought "AllowSorting" (see link you provided) would do this but alas. Anyway still many thanks for your reply, I'm very glad I managed to get it to work and without you I'd probably still be stuck at nothing. :)
    – Magali
    Commented Apr 2, 2014 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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