locked
LINQ + SiteCollection RRS feed

  • Question

  • Hi All,

    I have different lists in different sites under a sitecollection. (for eg.,list1 with field 'sitename' in all subsites)

    Now I need to fetch the data from this list1 from all subsites based on the condition.(I need 'sitename' and some other fields info)

    Let me know if you have any queries.

    Thanks & Regards,
    Kumar.


    Tuesday, November 29, 2011 9:55 AM

Answers

  • You can simply use SPSiteDataQuery to do this and If you need to query the list with certain conditions to all sub sites then set the scope to recursive

    SPSiteDataQuery query = new SPSiteDataQuery();
    
    //query custom list.
    query.Lists = "<Lists ServerTemplate='100' />";
    
    
    //You can add your fields as well 
    query.ViewFields = "<FieldRef Name='Title' /><FieldRef Name='sitename' />";
    
    query.Query = "<Where><Contains>" +
    	"<FieldRef Name='Title' /><Value Type='text'>SomeText</Value>"+
    	"</Contains></Where>";
    
    query.Webs = "<Webs Scope='SiteCollection' />";
    
    DataTable dt = SPContext.Current.Web.GetSiteData(query);
    
    

     

    Reference Link : - http://sharepoint-snippets.com/spsitedataquery-examples-for-sharepoint-2010/

    Also take a look at - http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsitedataquery.aspx

    http://www.sharepointkings.com/2008/06/using-spsitedataquery-class-to-execute.html

    Hope this helps you


    Bhushan | http://www.passionatetechie.blogspot.com | http://twitter.com/BhushanGawale
    Tuesday, November 29, 2011 10:05 AM
  • Yes, you can also use LINQ in SharePoint 2010 to retrive the result.

    But CAML is the recommended for accessing the items. Because LINQ query conevrts to CAML and then it process the query. So it's all affects on performance.

    Have a look on Using Linq on SharePoint, http://msdn.microsoft.com/en-us/library/ee535491.aspx

    Do you want to Condition for URL field? or based on what condition you want to get the items.

    Just modify the Bhusan code, to apply the condition for URL field,

    SPSiteDataQuery query = new SPSiteDataQuery();
    
    //query custom list.
    query.Lists = "<Lists ServerTemplate='100' />";
    
    //You can add your fields as well 
    query.ViewFields = "<FieldRef Name='Title' /><FieldRef Name='sitename' />";
    
    query.Query = "<Where><Eq>" +
    	"<FieldRef Name='sitename' /><Value Type='Url'>http://www.google.com</Value>"+
    	"</Eq></Where>";
    
    query.Webs = "<Webs Scope='SiteCollection' />";
    
    DataTable dt = SPContext.Current.Web.GetSiteData(query);
    

     


    Regards,
    Shantha Kumar .T | MCPD - SharePoint Developer 2010 | MCITP - SharePoint Administrator 2010
    (B) Shantha Kumar's Blog | (T)Follow me
    • Edited by Shantha Kumar T Tuesday, November 29, 2011 11:00 AM
    • Marked as answer by Kumar9999 Tuesday, November 29, 2011 12:44 PM
    Tuesday, November 29, 2011 10:53 AM

All replies

  • You can simply use SPSiteDataQuery to do this and If you need to query the list with certain conditions to all sub sites then set the scope to recursive

    SPSiteDataQuery query = new SPSiteDataQuery();
    
    //query custom list.
    query.Lists = "<Lists ServerTemplate='100' />";
    
    
    //You can add your fields as well 
    query.ViewFields = "<FieldRef Name='Title' /><FieldRef Name='sitename' />";
    
    query.Query = "<Where><Contains>" +
    	"<FieldRef Name='Title' /><Value Type='text'>SomeText</Value>"+
    	"</Contains></Where>";
    
    query.Webs = "<Webs Scope='SiteCollection' />";
    
    DataTable dt = SPContext.Current.Web.GetSiteData(query);
    
    

     

    Reference Link : - http://sharepoint-snippets.com/spsitedataquery-examples-for-sharepoint-2010/

    Also take a look at - http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsitedataquery.aspx

    http://www.sharepointkings.com/2008/06/using-spsitedataquery-class-to-execute.html

    Hope this helps you


    Bhushan | http://www.passionatetechie.blogspot.com | http://twitter.com/BhushanGawale
    Tuesday, November 29, 2011 10:05 AM
  • I am storing sitenames as Url's(Hyperlink field).

    Can you help me for this in "ViewFields" and "query" statements.

    Need one clarification. why can't I use LINQ in 2010 instead of CAML?

     

    Thanks & Regards,

    Kumar.

    Tuesday, November 29, 2011 10:44 AM
  • Yes, you can also use LINQ in SharePoint 2010 to retrive the result.

    But CAML is the recommended for accessing the items. Because LINQ query conevrts to CAML and then it process the query. So it's all affects on performance.

    Have a look on Using Linq on SharePoint, http://msdn.microsoft.com/en-us/library/ee535491.aspx

    Do you want to Condition for URL field? or based on what condition you want to get the items.

    Just modify the Bhusan code, to apply the condition for URL field,

    SPSiteDataQuery query = new SPSiteDataQuery();
    
    //query custom list.
    query.Lists = "<Lists ServerTemplate='100' />";
    
    //You can add your fields as well 
    query.ViewFields = "<FieldRef Name='Title' /><FieldRef Name='sitename' />";
    
    query.Query = "<Where><Eq>" +
    	"<FieldRef Name='sitename' /><Value Type='Url'>http://www.google.com</Value>"+
    	"</Eq></Where>";
    
    query.Webs = "<Webs Scope='SiteCollection' />";
    
    DataTable dt = SPContext.Current.Web.GetSiteData(query);
    

     


    Regards,
    Shantha Kumar .T | MCPD - SharePoint Developer 2010 | MCITP - SharePoint Administrator 2010
    (B) Shantha Kumar's Blog | (T)Follow me
    • Edited by Shantha Kumar T Tuesday, November 29, 2011 11:00 AM
    • Marked as answer by Kumar9999 Tuesday, November 29, 2011 12:44 PM
    Tuesday, November 29, 2011 10:53 AM