Thursday 29 May 2014

SPSiteDataQuery on Lists

I had a requirement to perform SPSiteDataQuery on Workflow History lists.
I had to query the data for a certain time period.

After struggling for a long time ,I realized that I have to provide field ID instead of the field name.

Kindly find the code snippet below which query last 7 days items from workflow history list and stores it in a Data table.


SPSiteDataQuery workflowHistoryQuery = new SPSiteDataQuery();
workflowHistoryQuery.Lists = "<Lists ServerTemplate='140'  Hidden='TRUE'/>";
workflowHistoryQuery.ViewFields = "<FieldRef Name='Description' Nullable='TRUE'/><FieldRef                Name='List' Nullable='TRUE'/><FieldRef Name='Item' Nullable='TRUE'/><FieldRef Name='Created'        Type='DateTime'/>";
 workflowHistoryQuery.Webs = "<Webs Scope='SiteCollection' />";
string createdDateID = SPBuiltInFieldId.Created.ToString("B");
string endDate = DateTime.Today.AddDays(-7).ToString("yyyy-MM-dd");
workflowHistoryQuery.Query = "<Where><Gt>";
workflowHistoryQuery.Query += "<FieldRef ID=\"" + createdDateID + "\" />";
workflowHistoryQuery.Query += "<Value Type=\"DateTime\">" + endDate + " </Value>";
workflowHistoryQuery.Query += "</Gt></Where>";
DataTable workflowResult = SPContext.Current.Site.RootWeb.GetSiteData(workflowHistoryQuery);



Reference SPSiteDataQuery

Hope it Helps.

No comments:

Post a Comment