Here's the code:
// Instantiate web instances
SPSite sourceSite = new SPSite(@"http://SharePointServer:31001");
SPWeb sourceWeb = sourceSite.RootWeb;
SPSite destSite = new SPSite(@"http://SharePointServer:31002");
SPWeb destWeb = destSite.RootWeb;
// Get a reference to the source list
SPList sourceList = sourceWeb.GetList("/Lists/Announcements");
// if the list exists on the destination site, delete it
try
{
SPList temp = destWeb.Lists[sourceList.Title];
destWeb.Lists.Delete(temp.ID);
}
catch { }
// create new list on the destination web with same properties
Guid newListID = destWeb.Lists.Add(sourceList.Title, sourceList.Description,
sourceList.BaseTemplate);
SPList destList = destWeb.Lists[newListID];
// copy items
foreach (SPListItem item in announcements.Items)
{
SPListItem newDestItem = destList.Items.Add();
foreach (SPField field in sourceList.Fields)
{
if (!field.ReadOnlyField)
newDestItem[field.Id] = item[field.Id];
}
newDestItem.Update();
}
// set quicklaunch settings
destList.OnQuickLaunch = sourceList.OnQuickLaunch;
destList.Update();
No comments:
Post a Comment