using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;
namespace M_one_TimerJob_MoveData
{
class MoveData_OneList_Another : SPJobDefinition
{
SPWeb web;
string siteUrl;
public int CurrentPostId = 0;
public MoveData_OneList_Another()
: base()
{
}
public MoveData_OneList_Another(string jobName, SPService service)
: base(jobName, service, null, SPJobLockType.None)
{
this.Title = "Custom Timer Jobs Move Data";
}
public MoveData_OneList_Another(string jobName, SPWebApplication webapp)
: base(jobName, webapp, null, SPJobLockType.ContentDatabase)
{
this.Title = "Custom Timer Jobs Move Data";
}
string SiteUrl = string.Empty;
public override void Execute(Guid targetInstanceId)
{
SPWebApplication webApp = this.Parent as SPWebApplication;
SPSite site = webApp.Sites[0];
SPWeb web = site.OpenWeb();
siteUrl = web.Url;
MoveToListDbAwards(siteUrl, "db_Awards", "db_blogarea");
// MoveToListDbAwards(siteUrl, "smgdbmakepost", "db_Awards_Archive");
}
/* public void MoveToListDbAwards(string siteUrl, string Source, string Destination)
{
SPListItemCollection Srccollitem;
SPList destlib;
SPListItem item;
SPListItem targetItem;
try
{
using (SPSite rootSite = new SPSite(siteUrl)) //siteUrl is the URL of the site
{
rootSite.AllowUnsafeUpdates = true;
using (SPWeb spWeb = rootSite.OpenWeb())
{
spWeb.AllowUnsafeUpdates = true;
//The below query gets all the items whose Expiry Date column contains Date Today’s Date or previous date
DateTime dt = DateTime.Today;
DateTime sixmonthearlier = dt.AddMonths(-6);
SPQuery query = new SPQuery();
//query.Query = "<Where><And>" + "<Geq>" + "<FieldRef Name='Created'/>" + "<Value Type='DateTime' IncludeTimeValue='FALSE'>" + SPUtility.CreateISO8601DateTimeFromSystemDateTime(sixmonthearlier) + "</Value>" + "</Geq>" + "<Leq>" + "<FieldRef Name='Created'/>" +"<Value Type='DateTime' IncludeTimeValue='FALSE'>" + SPUtility.CreateISO8601DateTimeFromSystemDateTime(dt) + "</Value>" +"</Leq>" +"</And> </Where>";
query.Query = "<Where><Geq>" + "<FieldRef Name='Created'/>" + "<Value Type='DateTime' IncludeTimeValue='FALSE'>" + SPUtility.CreateISO8601DateTimeFromSystemDateTime(sixmonthearlier) + "</Value>" + "</Geq></Where>";
Srccollitem = spWeb.Lists[Source].GetItems(query);
///Get the Destination List
destlib = spWeb.Lists[Destination];
///Start of For Loop to move items one by one from Source List to Destination List
for (int i = Srccollitem.Count - 1; i >= 0; i--)
{
item = Srccollitem[i];
SPQuery plquery = new SPQuery();
CurrentPostId = Convert.ToInt32(item["PostID"]);
plquery.Query = "<Where><Eq><FieldRef Name='PostID'/>" + "<Value Type='Integer' >" + CurrentPostId + "</Value>" + "</Eq></Where>";
// plquery.Query = "<Where><Eq><FieldRef Name='PostID'/>" + "<Value Type='Integer' >" + Convert.ToInt32(164) + "</Value>" + "</Eq></Where>";
SPWebApplication webApp = this.Parent as SPWebApplication;
SPSite site = webApp.Sites[0];
SPWeb web = site.OpenWeb();
siteUrl = web.Url;
///First add item to the destination list
targetItem = destlib.Items.Add();
///Start of ForEach Loop to add value of each Field from Source to Destionation
foreach (SPField f in item.Fields)
{
//Copy all except attachments.
if (!f.ReadOnlyField && f.InternalName != "Attachments"
&& null != item[f.InternalName])
{
targetItem[f.InternalName] = item[f.InternalName];
}
}
///End of ForEach Loop
///Update The Destination List
targetItem.Update();
MoveToListPLAwards(siteUrl, "pl_Awards", "pl_Awards_Archive", plquery);
// MoveToListPLAwards(siteUrl, "pl_Awards_Archive", "pl_Awards", plquery);
// item.Delete();
}
spWeb.AllowUnsafeUpdates = false;
///End of For Loop.
}
}
}
catch (Exception ex)
{
ex.Message.ToString();
}
}*/
public void MoveToListDbAwards(string siteUrl, string Source, string Destination)
{
// SPListItemCollection Srccollitem;
// SPList destlib;
// SPListItem item;
// SPListItem targetItem;
try
{
using (SPSite rootSite = new SPSite(siteUrl)) //siteUrl is the URL of the site
{
rootSite.AllowUnsafeUpdates = true;
using (SPWeb spWeb = rootSite.OpenWeb())
{
spWeb.AllowUnsafeUpdates = true;
SPWebApplication webApp = this.Parent as SPWebApplication;
SPSite site = webApp.Sites[0];
SPWeb web = site.OpenWeb();
siteUrl = web.Url;
//The below query gets all the items whose Expiry Date column contains Date Today’s Date or previous date
DateTime dt = DateTime.Today;
DateTime sixmonthearlier = dt.AddMonths(-6);
SPQuery query = new SPQuery();
query.Query = "<Where><Leq>" + "<FieldRef Name='CustomCreated'/>" + "<Value Type='DateTime' IncludeTimeValue='FALSE'>" + SPUtility.CreateISO8601DateTimeFromSystemDateTime(sixmonthearlier) + "</Value>" + "</Leq></Where>";
//Get the Source List Instance
SPList _srclst = spWeb.Lists[Source];
string rootfolder = _srclst.RootFolder.Name.ToString();
//Get SourceList ItemsCollection instance
SPListItemCollection srcitemcollection = _srclst.GetItems(query);
SPList _destlist = spWeb.Lists[Destination];
//Check whether the list is of Discussion Board template
if (_srclst.BaseTemplate == SPListTemplateType.DiscussionBoard)
{
for (int i = srcitemcollection.Count - 1; i >= 0; i--)
{
try
{
//Read single item from item collection
SPListItem srcitem;
srcitem = srcitemcollection[i];
SPQuery plquery = new SPQuery();
CurrentPostId = Convert.ToInt32(srcitem["PostID"]);
plquery.Query = "<Where><Eq><FieldRef Name='PostID'/>" + "<Value Type='Integer' >" + CurrentPostId + "</Value>" + "</Eq></Where>";
SPListItem targetitem;
targetitem = _destlist.Items.Add();
//Get the name of the item which is stored as Folder in Discussion Board
SPFolder folderdiscussion = srcitem.Folder;
//Get destination list item collection
SPListItemCollection destitemcollection = _destlist.Items;
//create a new discussion item in the destination list with the title from source list
SPListItem discussion = SPUtility.CreateNewDiscussion(destitemcollection, srcitem[SPBuiltInFieldId.Title].ToString());
discussion[SPBuiltInFieldId.Body] = srcitem[SPBuiltInFieldId.Body];
//Copy the Author from source to destination
discussion[SPBuiltInFieldId.Author] = srcitem[SPBuiltInFieldId.Author];
discussion[SPBuiltInFieldId.Editor] = srcitem[SPBuiltInFieldId.Editor];
//Copy the name of Created By from source to destination
discussion[SPBuiltInFieldId.Created] = srcitem[SPBuiltInFieldId.Created];
discussion["LikesCount"] = srcitem["LikesCount"];
discussion["LikedBy"] = srcitem["LikedBy"];
discussion["CustomCreated"] = srcitem["CustomCreated"];
discussion["HeadlineText"] = srcitem["HeadlineText"];
discussion["PostID"] = srcitem["PostID"];
discussion["PostedBy"] = srcitem["PostedBy"];
discussion.Update();
//Now we need to copy all the replies of the selected discussion item
SPQuery q = new SPQuery();
q.Query = "<OrderBy><FieldRef Name='Title'/></OrderBy>";
//fire query on the specific discussion item i.e on the specific folder
//The discussion item is stored under Folder Content Type
q.Folder = folderdiscussion;
//Get collection of replies for specific discussion
SPListItemCollection folderlistitemscoll = _srclst.GetItems(q);
//iterate through each reply to copy them in destination list for that particular discussion
foreach (SPListItem li in folderlistitemscoll)
{
//Each reply is read as a list item as it is stored in the Message Content Type
//Item content type is the parent of Message content type
SPListItem reply = SPUtility.CreateNewDiscussionReply(discussion);
//Copy the content from reply/body column from source list to destination
reply[SPBuiltInFieldId.Body] = li[SPBuiltInFieldId.Body];
//Copy the author from source list to destination
reply["ImgPostedBy"] = li[SPBuiltInFieldId.Author];
reply["PostedBy"] = li[SPBuiltInFieldId.Editor];
reply[SPBuiltInFieldId.Author] = li[SPBuiltInFieldId.Author];
//Copy the editor i.e. Modified By from source list to destination
reply[SPBuiltInFieldId.Editor] = li[SPBuiltInFieldId.Editor];
//Copy the name of Created By from source to destination
reply[SPBuiltInFieldId.Created] = li[SPBuiltInFieldId.Created];
//Update the reply in the discussion item
reply.Update();
}
discussion.Update();
//Call Function For Picture Library to move pictures on the basis of PostID of db_Awards
// MoveToListPLAwards(siteUrl, "pl_Awards", "pl_Awards_Archive", plquery);
MoveToListPLAwards(siteUrl, "pl_Awards", "pl_blogarea", plquery);
// srcitem.Delete();
}
catch (Exception ex)
{
SPList _sourcelistlogs = spWeb.Lists["lst_Logs"];
SPListItem item = _sourcelistlogs.Items.Add();
item["LogContent"] = ex.Message;
item["Title"] = "DataMove";
item.Update();
_sourcelistlogs.Update();
}
}
spWeb.AllowUnsafeUpdates = false;
}
}
}
}
catch (Exception ex)
{
}
}
public void MoveToListPLAwards(string siteUrl, string source, string destination, SPQuery query)
{
SPListItemCollection Srccollectionitem;
SPList destnationlib;
SPListItem srclistitem;
SPListItem desttargetItem;
try
{
using (SPSite rootSite = new SPSite(siteUrl)) //siteUrl is the URL of the site
{
// rootSite.AllowUnsafeUpdates = true;
using (SPWeb spWeb = rootSite.OpenWeb())
{
//spWeb.AllowUnsafeUpdates = true;
Srccollectionitem = spWeb.Lists[source].GetItems(query);
///Get the Destination List
destnationlib = spWeb.Lists[destination];
///Start of For Loop to move items one by one from Source List to Destination List
for (int i = Srccollectionitem.Count-1 ; i >= 0; i--)
{
srclistitem = Srccollectionitem[i];
//Added by Robin to create SPFile object to add source item images into Source Library
byte[] imageData = null;
///First add item to the destination list
///
desttargetItem = destnationlib.Items.Add();
desttargetItem = srclistitem;
SPFile srcfile = srclistitem.File;
imageData = srcfile.OpenBinary();
string destUrl = destnationlib.RootFolder.Url + "/" + srcfile.Name;
SPFile file = destnationlib.RootFolder.Files.Add(destUrl, imageData, true);
SPListItem item = file.Item;
item["PostID"] = CurrentPostId;
item["Description"] = srclistitem["Description"];
item["LikesCount"] = srclistitem["LikesCount"];
item["LikedBy"] = srclistitem["LikedBy"];
// item["Image_Size"] = srclistitem["Image_Size"];
// item["File Size"] = srclistitem["File Size"];
// item["PostID"] = srclistitem["PostID"];
file.Update();
item.Update();
//srclistitem.Delete();
}
}
}
}
catch (Exception ex)
{
ex.Message.ToString();
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;
namespace M_one_TimerJob_MoveData
{
class MoveData_OneList_Another : SPJobDefinition
{
SPWeb web;
string siteUrl;
public int CurrentPostId = 0;
public MoveData_OneList_Another()
: base()
{
}
public MoveData_OneList_Another(string jobName, SPService service)
: base(jobName, service, null, SPJobLockType.None)
{
this.Title = "Custom Timer Jobs Move Data";
}
public MoveData_OneList_Another(string jobName, SPWebApplication webapp)
: base(jobName, webapp, null, SPJobLockType.ContentDatabase)
{
this.Title = "Custom Timer Jobs Move Data";
}
string SiteUrl = string.Empty;
public override void Execute(Guid targetInstanceId)
{
SPWebApplication webApp = this.Parent as SPWebApplication;
SPSite site = webApp.Sites[0];
SPWeb web = site.OpenWeb();
siteUrl = web.Url;
MoveToListDbAwards(siteUrl, "db_Awards", "db_blogarea");
// MoveToListDbAwards(siteUrl, "smgdbmakepost", "db_Awards_Archive");
}
/* public void MoveToListDbAwards(string siteUrl, string Source, string Destination)
{
SPListItemCollection Srccollitem;
SPList destlib;
SPListItem item;
SPListItem targetItem;
try
{
using (SPSite rootSite = new SPSite(siteUrl)) //siteUrl is the URL of the site
{
rootSite.AllowUnsafeUpdates = true;
using (SPWeb spWeb = rootSite.OpenWeb())
{
spWeb.AllowUnsafeUpdates = true;
//The below query gets all the items whose Expiry Date column contains Date Today’s Date or previous date
DateTime dt = DateTime.Today;
DateTime sixmonthearlier = dt.AddMonths(-6);
SPQuery query = new SPQuery();
//query.Query = "<Where><And>" + "<Geq>" + "<FieldRef Name='Created'/>" + "<Value Type='DateTime' IncludeTimeValue='FALSE'>" + SPUtility.CreateISO8601DateTimeFromSystemDateTime(sixmonthearlier) + "</Value>" + "</Geq>" + "<Leq>" + "<FieldRef Name='Created'/>" +"<Value Type='DateTime' IncludeTimeValue='FALSE'>" + SPUtility.CreateISO8601DateTimeFromSystemDateTime(dt) + "</Value>" +"</Leq>" +"</And> </Where>";
query.Query = "<Where><Geq>" + "<FieldRef Name='Created'/>" + "<Value Type='DateTime' IncludeTimeValue='FALSE'>" + SPUtility.CreateISO8601DateTimeFromSystemDateTime(sixmonthearlier) + "</Value>" + "</Geq></Where>";
Srccollitem = spWeb.Lists[Source].GetItems(query);
///Get the Destination List
destlib = spWeb.Lists[Destination];
///Start of For Loop to move items one by one from Source List to Destination List
for (int i = Srccollitem.Count - 1; i >= 0; i--)
{
item = Srccollitem[i];
SPQuery plquery = new SPQuery();
CurrentPostId = Convert.ToInt32(item["PostID"]);
plquery.Query = "<Where><Eq><FieldRef Name='PostID'/>" + "<Value Type='Integer' >" + CurrentPostId + "</Value>" + "</Eq></Where>";
// plquery.Query = "<Where><Eq><FieldRef Name='PostID'/>" + "<Value Type='Integer' >" + Convert.ToInt32(164) + "</Value>" + "</Eq></Where>";
SPWebApplication webApp = this.Parent as SPWebApplication;
SPSite site = webApp.Sites[0];
SPWeb web = site.OpenWeb();
siteUrl = web.Url;
///First add item to the destination list
targetItem = destlib.Items.Add();
///Start of ForEach Loop to add value of each Field from Source to Destionation
foreach (SPField f in item.Fields)
{
//Copy all except attachments.
if (!f.ReadOnlyField && f.InternalName != "Attachments"
&& null != item[f.InternalName])
{
targetItem[f.InternalName] = item[f.InternalName];
}
}
///End of ForEach Loop
///Update The Destination List
targetItem.Update();
MoveToListPLAwards(siteUrl, "pl_Awards", "pl_Awards_Archive", plquery);
// MoveToListPLAwards(siteUrl, "pl_Awards_Archive", "pl_Awards", plquery);
// item.Delete();
}
spWeb.AllowUnsafeUpdates = false;
///End of For Loop.
}
}
}
catch (Exception ex)
{
ex.Message.ToString();
}
}*/
public void MoveToListDbAwards(string siteUrl, string Source, string Destination)
{
// SPListItemCollection Srccollitem;
// SPList destlib;
// SPListItem item;
// SPListItem targetItem;
try
{
using (SPSite rootSite = new SPSite(siteUrl)) //siteUrl is the URL of the site
{
rootSite.AllowUnsafeUpdates = true;
using (SPWeb spWeb = rootSite.OpenWeb())
{
spWeb.AllowUnsafeUpdates = true;
SPWebApplication webApp = this.Parent as SPWebApplication;
SPSite site = webApp.Sites[0];
SPWeb web = site.OpenWeb();
siteUrl = web.Url;
//The below query gets all the items whose Expiry Date column contains Date Today’s Date or previous date
DateTime dt = DateTime.Today;
DateTime sixmonthearlier = dt.AddMonths(-6);
SPQuery query = new SPQuery();
query.Query = "<Where><Leq>" + "<FieldRef Name='CustomCreated'/>" + "<Value Type='DateTime' IncludeTimeValue='FALSE'>" + SPUtility.CreateISO8601DateTimeFromSystemDateTime(sixmonthearlier) + "</Value>" + "</Leq></Where>";
//Get the Source List Instance
SPList _srclst = spWeb.Lists[Source];
string rootfolder = _srclst.RootFolder.Name.ToString();
//Get SourceList ItemsCollection instance
SPListItemCollection srcitemcollection = _srclst.GetItems(query);
SPList _destlist = spWeb.Lists[Destination];
//Check whether the list is of Discussion Board template
if (_srclst.BaseTemplate == SPListTemplateType.DiscussionBoard)
{
for (int i = srcitemcollection.Count - 1; i >= 0; i--)
{
try
{
//Read single item from item collection
SPListItem srcitem;
srcitem = srcitemcollection[i];
SPQuery plquery = new SPQuery();
CurrentPostId = Convert.ToInt32(srcitem["PostID"]);
plquery.Query = "<Where><Eq><FieldRef Name='PostID'/>" + "<Value Type='Integer' >" + CurrentPostId + "</Value>" + "</Eq></Where>";
SPListItem targetitem;
targetitem = _destlist.Items.Add();
//Get the name of the item which is stored as Folder in Discussion Board
SPFolder folderdiscussion = srcitem.Folder;
//Get destination list item collection
SPListItemCollection destitemcollection = _destlist.Items;
//create a new discussion item in the destination list with the title from source list
SPListItem discussion = SPUtility.CreateNewDiscussion(destitemcollection, srcitem[SPBuiltInFieldId.Title].ToString());
discussion[SPBuiltInFieldId.Body] = srcitem[SPBuiltInFieldId.Body];
//Copy the Author from source to destination
discussion[SPBuiltInFieldId.Author] = srcitem[SPBuiltInFieldId.Author];
discussion[SPBuiltInFieldId.Editor] = srcitem[SPBuiltInFieldId.Editor];
//Copy the name of Created By from source to destination
discussion[SPBuiltInFieldId.Created] = srcitem[SPBuiltInFieldId.Created];
discussion["LikesCount"] = srcitem["LikesCount"];
discussion["LikedBy"] = srcitem["LikedBy"];
discussion["CustomCreated"] = srcitem["CustomCreated"];
discussion["HeadlineText"] = srcitem["HeadlineText"];
discussion["PostID"] = srcitem["PostID"];
discussion["PostedBy"] = srcitem["PostedBy"];
discussion.Update();
//Now we need to copy all the replies of the selected discussion item
SPQuery q = new SPQuery();
q.Query = "<OrderBy><FieldRef Name='Title'/></OrderBy>";
//fire query on the specific discussion item i.e on the specific folder
//The discussion item is stored under Folder Content Type
q.Folder = folderdiscussion;
//Get collection of replies for specific discussion
SPListItemCollection folderlistitemscoll = _srclst.GetItems(q);
//iterate through each reply to copy them in destination list for that particular discussion
foreach (SPListItem li in folderlistitemscoll)
{
//Each reply is read as a list item as it is stored in the Message Content Type
//Item content type is the parent of Message content type
SPListItem reply = SPUtility.CreateNewDiscussionReply(discussion);
//Copy the content from reply/body column from source list to destination
reply[SPBuiltInFieldId.Body] = li[SPBuiltInFieldId.Body];
//Copy the author from source list to destination
reply["ImgPostedBy"] = li[SPBuiltInFieldId.Author];
reply["PostedBy"] = li[SPBuiltInFieldId.Editor];
reply[SPBuiltInFieldId.Author] = li[SPBuiltInFieldId.Author];
//Copy the editor i.e. Modified By from source list to destination
reply[SPBuiltInFieldId.Editor] = li[SPBuiltInFieldId.Editor];
//Copy the name of Created By from source to destination
reply[SPBuiltInFieldId.Created] = li[SPBuiltInFieldId.Created];
//Update the reply in the discussion item
reply.Update();
}
discussion.Update();
//Call Function For Picture Library to move pictures on the basis of PostID of db_Awards
// MoveToListPLAwards(siteUrl, "pl_Awards", "pl_Awards_Archive", plquery);
MoveToListPLAwards(siteUrl, "pl_Awards", "pl_blogarea", plquery);
// srcitem.Delete();
}
catch (Exception ex)
{
SPList _sourcelistlogs = spWeb.Lists["lst_Logs"];
SPListItem item = _sourcelistlogs.Items.Add();
item["LogContent"] = ex.Message;
item["Title"] = "DataMove";
item.Update();
_sourcelistlogs.Update();
}
}
spWeb.AllowUnsafeUpdates = false;
}
}
}
}
catch (Exception ex)
{
}
}
public void MoveToListPLAwards(string siteUrl, string source, string destination, SPQuery query)
{
SPListItemCollection Srccollectionitem;
SPList destnationlib;
SPListItem srclistitem;
SPListItem desttargetItem;
try
{
using (SPSite rootSite = new SPSite(siteUrl)) //siteUrl is the URL of the site
{
// rootSite.AllowUnsafeUpdates = true;
using (SPWeb spWeb = rootSite.OpenWeb())
{
//spWeb.AllowUnsafeUpdates = true;
Srccollectionitem = spWeb.Lists[source].GetItems(query);
///Get the Destination List
destnationlib = spWeb.Lists[destination];
///Start of For Loop to move items one by one from Source List to Destination List
for (int i = Srccollectionitem.Count-1 ; i >= 0; i--)
{
srclistitem = Srccollectionitem[i];
//Added by Robin to create SPFile object to add source item images into Source Library
byte[] imageData = null;
///First add item to the destination list
///
desttargetItem = destnationlib.Items.Add();
desttargetItem = srclistitem;
SPFile srcfile = srclistitem.File;
imageData = srcfile.OpenBinary();
string destUrl = destnationlib.RootFolder.Url + "/" + srcfile.Name;
SPFile file = destnationlib.RootFolder.Files.Add(destUrl, imageData, true);
SPListItem item = file.Item;
item["PostID"] = CurrentPostId;
item["Description"] = srclistitem["Description"];
item["LikesCount"] = srclistitem["LikesCount"];
item["LikedBy"] = srclistitem["LikedBy"];
// item["Image_Size"] = srclistitem["Image_Size"];
// item["File Size"] = srclistitem["File Size"];
// item["PostID"] = srclistitem["PostID"];
file.Update();
item.Update();
//srclistitem.Delete();
}
}
}
}
catch (Exception ex)
{
ex.Message.ToString();
}
}
}
}
No comments:
Post a Comment