using
System;
System.Collections.Generic;
System.Linq;
System.Text;
Microsoft.SharePoint;
namespace
CopyListInSameSiteCollection
{
class
Program
static
void
Main(
string
[] args)
Console.WriteLine(
"This tool copies a list within the same site collection"
);
"Please enter the URL of the Source Site"
String sourceSite = Console.ReadLine();
"Please enter the name of the source list"
String sourceListName = Console.ReadLine();
"Please enter the URL of the Destination Site"
String destinationSite = Console.ReadLine();
"Please enter the name of the Destination list"
String destinationListName = Console.ReadLine();
"Please enter the description of the Destination list"
String destinationListDescription = Console.ReadLine();
try
(SPSite site =
new
SPSite(sourceSite))
(SPWeb web = site.OpenWeb())
SPList sourceList = web.Lists[sourceListName];
String templateName = sourceList.Title;
String templateFileName = sourceList.Title;
sourceList.SaveAsTemplate(templateFileName, templateName, sourceList.Description,
true
SPListTemplate listTemplate = site.GetCustomListTemplates(web)[templateName];
String destinationWeb = destinationSite.Remove(0, site.Url.Length + 1);
(SPWeb destWeb = site.OpenWeb(destinationWeb))
destWeb.Lists.Add(destinationListName, destinationListDescription, listTemplate);
destWeb.Update();
}
SPList listTemplates = site.RootWeb.Lists[
"List Template Gallery"
];
SPListItemCollection listTemplateItems = listTemplates.Items;
foreach
(SPListItem listTemplateItem
in
listTemplateItems)
if
(listTemplate.Name.Equals(listTemplateItem[
"Title"
]))
listTemplateItem.Delete();
listTemplates.Update();
break
;
catch
(Exception e)
Console.WriteLine(e.Message);
"The List is copied"
Gokan Ozcifci edited Revision 9. Comment: changed title to 2010
Gokan Ozcifci edited Revision 8. Comment: content and title
Richard Mueller edited Revision 7. Comment: Removed (en-US) from title, added tag
Craig Lussier edited Revision 6. Comment: added en-US to tags and title
Nevin Janzen edited Revision 5. Comment: Tags Edit
Ed Price - MSFT edited Revision 4. Comment: Title casing and tags
Craig Lussier edited Revision 2. Comment: minor text edits
Craig Lussier edited Revision 1. Comment: added toc and post headings
Craig Lussier edited Original. Comment: modified title to indicate the usage of C#
great that you posted this in C# and PowerShell. Keep the posts coming!
Nice article
This code only copies the items in the list.
What about Version History?
What about metadata such as Modifier/Modified?
What about list attributes such as workflows and properties settings?
Any code for that?