I had to develop a web part where I had to query and display all the document items which the file extensions are “docx” under a particular site collection including its sub sites. I used SPSiteDataQuery class in order to achieve.
Solution
using (SPSite spSite = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb spWeb = spSite.OpenWeb())
{
SPSiteDataQuery spSiteDataQuery = new SPSiteDataQuery();
spSiteDataQuery.Query =
@"<Where>
<Eq>
<FieldRef Name='DocIcon' />
<Value Type='Computed'>docx</Value>
</Eq>
</Where>";
spSiteDataQuery.ViewFields = "<FieldRef Name='FileLeafRef' /><FieldRef Name='Editor' /><FieldRef Name='ContentType' />";
spSiteDataQuery.Lists = "<Lists ServerTemplate='101'/>"; //Set the list template by providing its template type id
spSiteDataQuery.Webs = "<Webs Scope='Recursive'/>";//Recursive: Current site and any subsite
//Scope='SiteCollection': all webs in the current site collection
//Get the results into a datatable
DataTable resultsTable = spWeb.GetSiteData(spSiteDataQuery);
//Bind the results in to a asp grid view
GridView spGridView = new GridView();
spGridView.AutoGenerateColumns = true;
spGridView.DataSource = resultsTable;
spGridView.DataBind();
Controls.Add(spGridView);
}
}
Conclusion The results will look something as below.
I have posted the my sln file here. Please download in case if you need
https://drive.google.com/file/d/0ByEnOE8DAdvhSTlnMDBuWDRuWTA/edit?usp=sharing
Reference: http://msdn.microsoft.com/en-us/library/ms409088.aspx
No comments:
Post a Comment