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.

Tuesday 6 May 2014

Sharepoint Sub Site BackUp

Here comes my second post related to deployment.

There are times when we have to take Back up of  environment.So what are the possible options for taking Back up.
Sharepoint Central Admin provides us the following two option to take Back up
1)Farm Back up
2)Granular Back up:This can be used if we need to take back up of specific site collection.All we have to do is select the web app and then select the required site collection.Provide the File location where the back up files can be created.
Pretty easy.Isn't it.

Problem comes when we have to take a back up one level down the site collection as well.
How can we take back up of Sub Sites???

Unluckily Sharepoint doesn't provide any UI interface to do it.But that doesn't mean that we can't do it.Thanks to Powershell for making our life easy.

So All we need to do is Run the following powershell command to take back up and restore it.

Export-spweb –identity http://site/subsite -path c:\subsitebkup\subsite.bak

Import-SPWeb –identity http://site/subsite -path c:\subsitebkup\subsite.bak