Wednesday, 21 August 2013

SPSiteDataQuery Share Point 2010 example

http://thesharepoint2010.wordpress.com/2012/10/01/spsitedataquery-share-point-example/

SPSiteDataQuery is used to retrieve data from list or from all list in the site collection ( i.e. used for cross-site and cross-list queries). SPWeb.GetSiteData is used to retrieve the data.
SPSiteDataQuery.Webs property will be used to specify from where the data will be retrieved
Following properties will be used
1
2
3
dataQuery.Webs = "<Webs Scope=\"Recursive\">";
dataQuery.Webs = "<Webs Scope=\"SiteCollection\">";
dataQuery.Webs = "<Webs Scope=\"\">";
Following example illustrate that the data is retrieve from the Document library (ServerTemplate for Document library is 101).In this example document No is used in the where clause .SPSiteDataQuery object is passed to the GetSiteData and then result is bind to the Grid view.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
string siteStr = SPContext.Current.Web.Url;
 
            using (SPSite site = new SPSite(siteStr))
            {
                using (SPWeb web = site.RootWeb)
                {
                    SPSiteDataQuery dataQuery = new SPSiteDataQuery();
                    dataQuery.Webs = "<Webs Scope=\"Recursive\">";
                    dataQuery.Lists = "<Lists ServerTemplate=\"101\" />";
                    dataQuery.ViewFields = "<FieldRef Name=\"DocumentNo\"/>" + "<FieldRef Name=\"Title\" />" + "<FieldRef Name=\"Tags\" />" + "<FieldRef Name=\"FileRef\" />";
                    string where = "";
 
                    where = "<Where>";
                     
                    if (txtDocumentNo.Text != "")
                    {
                        where += "<Eq>";
                        where += "<FieldRef Name=\"DocumentNo\"/>";
                        where += "<Value Type='Text'>" + txtDocumentNo.Text + "</Value>";
                        where += "</Eq>";
                    }
 
 
                    where += "</Where>";
                    dataQuery.Query = where;
                    DataTable dt = web.GetSiteData(dataQuery);
                    DataView dv = new DataView(dt);
                    gvResult.DataSource = dv;
                    gvResult.DataBind();
                     
                }

No comments:

Post a Comment