Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.
 protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

SPWeb web = SPContext.Current.Web;

        SPSiteDataQuery query = new SPSiteDataQuery();
        query.Lists = "<Lists MaxListLimit=\"3\">" +
  "<List ID=" + web.Lists.TryGetList("StaffNews").ID + " />" +
  "<List ID=" + web.Lists.TryGetList("CompanyNews").ID + " />" +
  "<List ID=" + web.Lists.TryGetList("ManagmentNews").ID + " />" +

"";

        string.Format("<OrderBy><FieldRef Name='Modified' Ascending='FALSE'></FieldRef></OrderBy>");
        query.Webs = "<Webs Scope=\"SiteCollection\" />";

        query.RowLimit = 10;


        DataTable dataTable = web.GetSiteData(query);

        Repeater1.DataSource = dataTable;

        Repeater1.DataBind();
share|improve this question
1  
What are you trying to do? –  Alexander Jan 29 '13 at 10:49
    
@Alexander I am trying to get from 3 lists, as i have described –  Martin Jan 29 '13 at 10:50
    
Do you want to get information from several lists or you want to get list instances? –  Ilia Sokolov Jan 29 '13 at 10:53
    
@Ilia Sokolov i want to get information from serveral lists –  Martin Jan 29 '13 at 10:55

2 Answers 2

You can retrieve information from several lists by SPSiteDataQuery. See this post for more details.

In this example the data is retrieved by this code.

using (SPSite site = new SPSite(ServerText.Text))
{
    using (SPWeb web = site.OpenWeb())
    {
        // Fetch using SPSiteDataQuery
        SPSiteDataQuery query = new SPSiteDataQuery();
        query.Lists = "<Lists ServerTemplate=\"105\" />";
        query.ViewFields = "<FieldRef Name=\"Title\" />" +  /* Title is LastName column */
                "<FieldRef Name=\"FirstName\" Nullable=\"TRUE\" Type=\"Text\"/>";
        query.Webs = "<Webs Scope=\"SiteCollection\" />";

        DataTable dataTable = web.GetSiteData(query);
        dataGridView3.DataSource = dataTable;
    }
}

The main line is

query.Lists = "<Lists ServerTemplate=\"105\" />";

How can you see this query is applied to the all lists that has "105" template id, this template is template for Contacts list, if you look at this line

web.Lists.Add("Contact List 2", "A contact list", SPListTemplateType.Contacts);

you see that Paul created the lists from Contacts template, thus values are retrieved from lists that are created before.

Try to google how to use query.Lists property in your case.

share|improve this answer
    
on that site that u send me. Were to he use c1 and c2 // Create Lists try { var c1 = web.Lists["Contact List 1"]; } catch { web.Lists.Add("Contact List 1", "A contact list", SPListTemplateType.Contacts); } try { var c2 = web.Lists["Contact List 2"]; } catch { web.Lists.Add("Contact List 2", "A contact list", SPListTemplateType.Contacts); } –  Martin Jan 29 '13 at 11:34
    
I updated my answer –  Alexander Jan 29 '13 at 11:41
    
i am using a repeater and not a grinview DataTable dataTable = web.GetSiteData(query); dataGridView3.DataSource = dataTable; how should i write if for an repeater? and which Id to " A List Definition from Content Type" –  Martin Jan 29 '13 at 11:49
    
It's a basic operation. try to do research before asking this question. –  Alexander Jan 29 '13 at 11:52
    
I have updated my code above please se it dont now whats wrong –  Martin Jan 29 '13 at 11:53

Building on Alexander's example, the <Lists> element actually supports a <List> element where you can specify the GUIDS of the lists you want to query.

using (SPSite site = new SPSite(ServerText.Text))
{
    using (SPWeb web = site.OpenWeb())
    {
        // Fetch using SPSiteDataQuery
        SPSiteDataQuery query = new SPSiteDataQuery();

        query.Lists = "<Lists MaxListLimit=\"3\">" +
              "<List ID="+web.Lists.TryGetList("StaffNews").ID+" />" +
              "<List ID="+web.Lists.TryGetList("CompanyNews").ID+" />" +
              "<List ID="+web.Lists.TryGetList("ManagmentNews").ID+" />" +
           "</Lists>";

        query.ViewFields = "<FieldRef Name=\"Title\" />" +  /* Title is LastName column */
                "<FieldRef Name=\"FirstName\" Nullable=\"TRUE\" Type=\"Text\"/>";
        query.Webs = "<Webs Scope=\"SiteCollection\" />";

        DataTable dataTable = web.GetSiteData(query);
        dataGridView3.DataSource = dataTable;
    }
}

More details here:

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsitedataquery.lists.aspx

share|improve this answer
    
` query.Lists = "<Lists MaxListLimit="3"> <List ID="7A9FDBE6-0841-430a-8D9A-53355801B5D5" /> <List ID="3D18F506-FCA1-451e-B645-2D720DC84FD8" /> </Lists>";` // cant get this line to work –  Martin Jan 29 '13 at 13:27
    
You will need the GUIDs of your lists. please see the updated code. –  Vardhaman Deshpande Jan 29 '13 at 13:38
    
is have updated my code but i dont get it to work, should i have the code like have it above or is something wrong i want to bind it to the repeater –  Martin Jan 29 '13 at 13:52

Your Answer

 
discard

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.