The Discussion Board Template contains two content types attached with it viz. 1) “Discussion” Content Type whose parent Content Type is “Folder” & 2) “Message” Content Type whose parent Content Type is “Item”. The “Discussion” Content Type works as a folder & the “Message” content Type works as the items in the Folder. Hence moving items from Discussion Board same as Other Lists will copy only the folder & not the replies within the folder. Hence to copy the items from one Discussion Board to other with all its corresponding replies, we have to first copy the items from the Discussion Content Type & after that we have to read all the replies of that item & copy each reply in the Destination list one by one. Here during copying we have to copy the items from the default fields as it is as we should know the name who had replied to the discussion & also the name of the creator of the Discussion.
Below is the code for the same.
public void MoveToList(string siteUrl, string Sourcelist, string Destinationlist)
{
try
{
using (SPSite rootSite = new SPSite(siteUrl))
{
using (SPWeb spWeb = rootSite.OpenWeb())
{
string camlquery = string.Empty;
camlquery = "<Where>"
+ "<Lt>"
+ "<FieldRef Name='Expiry_x0020_Date' />"
+ "<Value IncludeTimeValue='FALSE' Type='DateTime' ><Today /></Value>"
+ "</Lt>"
+ "</Where>";
SPQuery qry = new SPQuery();
qry.Query = camlquery;
///Get the Collection of SourceListItem by Caml Query
SPList _sourcelist = spWeb.Lists[Sourcelist];
string rootfolder = _sourcelist.RootFolder.Name.ToString();
SPListItemCollection Srccollitem;
Srccollitem = _sourcelist.GetItems(qry);
SPList destlib;
destlib = spWeb.Lists[Destinationlist];
//Check whether the list is of Discussion Board template
if (_sourcelist.BaseTemplate == SPListTemplateType.DiscussionBoard)
{
///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--)
{
//Read single item from item collection
SPListItem item;
item = Srccollitem[i];
SPListItem targetItem;
targetItem = destlib.Items.Add();
//Get the name of the item which is stored as Folder in Discussion Board
SPFolder fldrDiscussion = item.Folder;
//get destination list items
SPListItemCollection listCol = destlib.Items;
//create a new discussion item in the destination list with the title from source list
SPListItem discussion = SPUtility.CreateNewDiscussion(listCol, item[SPBuiltInFieldId.Title].ToString());
//Copy the content from body column from source list to destination
discussion[SPBuiltInFieldId.Body] = item[SPBuiltInFieldId.Body];
//Copy the Author from source to destination
discussion[SPBuiltInFieldId.Author] = item[SPBuiltInFieldId.Author];
//Copy the Editor i.e. "Modified By" from source to destination
discussion[SPBuiltInFieldId.Editor] = item[SPBuiltInFieldId.Editor];
//Copy the name of Created By from source to destination
discussion[SPBuiltInFieldId.Created] = item[SPBuiltInFieldId.Created];
//Copy the content from custom columns created by the user
discussion["Initiated By"] = item["Initiated By"];
discussion["Shared Members"] = item["Shared Members"];
discussion["Expiry Date"] = item["Expiry Date"];
//Update the discussion item
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 = fldrDiscussion;
//Get collection of replies for specific discussion
SPListItemCollection folderlistcoll = _sourcelist.GetItems(q);
//iterate through each reply to copy them in destination list for that particular discussion
foreach (SPListItem li in folderlistcoll)
{
//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[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();
}
//After updating every reply now update the whole discussion item
discussion.Update();
Console.WriteLine("MoveToList() : ItemName=" + item.Name + ", SiteUrl=" + siteUrl + ", Source=" + Source list+ ", Destination=" + Destinationlist);
}//Now read next discussion item from source list to move it to destination list
}///End of For Loop.
}
}
}
catch (Exception ex)
{
}
}
Below is the code for the same.
public void MoveToList(string siteUrl, string Sourcelist, string Destinationlist)
{
try
{
using (SPSite rootSite = new SPSite(siteUrl))
{
using (SPWeb spWeb = rootSite.OpenWeb())
{
string camlquery = string.Empty;
camlquery = "<Where>"
+ "<Lt>"
+ "<FieldRef Name='Expiry_x0020_Date' />"
+ "<Value IncludeTimeValue='FALSE' Type='DateTime' ><Today /></Value>"
+ "</Lt>"
+ "</Where>";
SPQuery qry = new SPQuery();
qry.Query = camlquery;
///Get the Collection of SourceListItem by Caml Query
SPList _sourcelist = spWeb.Lists[Sourcelist];
string rootfolder = _sourcelist.RootFolder.Name.ToString();
SPListItemCollection Srccollitem;
Srccollitem = _sourcelist.GetItems(qry);
SPList destlib;
destlib = spWeb.Lists[Destinationlist];
//Check whether the list is of Discussion Board template
if (_sourcelist.BaseTemplate == SPListTemplateType.DiscussionBoard)
{
///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--)
{
//Read single item from item collection
SPListItem item;
item = Srccollitem[i];
SPListItem targetItem;
targetItem = destlib.Items.Add();
//Get the name of the item which is stored as Folder in Discussion Board
SPFolder fldrDiscussion = item.Folder;
//get destination list items
SPListItemCollection listCol = destlib.Items;
//create a new discussion item in the destination list with the title from source list
SPListItem discussion = SPUtility.CreateNewDiscussion(listCol, item[SPBuiltInFieldId.Title].ToString());
//Copy the content from body column from source list to destination
discussion[SPBuiltInFieldId.Body] = item[SPBuiltInFieldId.Body];
//Copy the Author from source to destination
discussion[SPBuiltInFieldId.Author] = item[SPBuiltInFieldId.Author];
//Copy the Editor i.e. "Modified By" from source to destination
discussion[SPBuiltInFieldId.Editor] = item[SPBuiltInFieldId.Editor];
//Copy the name of Created By from source to destination
discussion[SPBuiltInFieldId.Created] = item[SPBuiltInFieldId.Created];
//Copy the content from custom columns created by the user
discussion["Initiated By"] = item["Initiated By"];
discussion["Shared Members"] = item["Shared Members"];
discussion["Expiry Date"] = item["Expiry Date"];
//Update the discussion item
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 = fldrDiscussion;
//Get collection of replies for specific discussion
SPListItemCollection folderlistcoll = _sourcelist.GetItems(q);
//iterate through each reply to copy them in destination list for that particular discussion
foreach (SPListItem li in folderlistcoll)
{
//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[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();
}
//After updating every reply now update the whole discussion item
discussion.Update();
Console.WriteLine("MoveToList() : ItemName=" + item.Name + ", SiteUrl=" + siteUrl + ", Source=" + Source list+ ", Destination=" + Destinationlist);
}//Now read next discussion item from source list to move it to destination list
}///End of For Loop.
}
}
}
catch (Exception ex)
{
}
}
Great dude! Got a similar requirement. You saved my time.
ReplyDeleteAwesome Post yaar....
ReplyDeleteThis is exactly a problem I have at the moment, just one question, how can i activate this code upon clicking a button after the desired item is selected to move it to the other list? Thanks.
ReplyDeleteyes you can
Delete