Sharepoint copy list
Sharepoint 复制备份系列--使用编程方式复制列表(Copy a SharePoint List Programmatically)
在sharepoint的开发维护中,经常会用到备份的功能,有时会复制列表,网站,或者其他的对象,这里简单的介绍一下。
转自jianyi的博客
SPList EnsureArchiveList( SPList templateList)
{
SPWeb web = templateList.ParentWeb;
SPList list = templateList;
SPList archiveList = null;
string archiveListTitle = list.Title + "(Archive)";
try
{
archiveList = web.Lists[archiveListTitle];
}
catch { }
if (archiveList == null)
{
web.AllowUnsafeUpdates = true;
string url = "Archive/" + list.RootFolder.Url;
Guid listId = web.Lists.Add(list.Title + "(Archive)", "List for archive.", url , list.TemplateFeatureId.ToString(), (int)list.BaseTemplate, "");
archiveList = web.Lists[listId];
foreach (SPField f in list.Fields)
{
if (archiveList.Fields.ContainsField(f.InternalName) == false)
{
archiveList.Fields.Add(f);
}
}
}
return archiveList;
}
2.sharepoint 复制列表,但有些不足的地方,比如说:
列表A是源列表,列表B是目标列表,当修改A的只读field,B复制过去的时候没办法实现完全复制,这也许是我代码的不足。比如标题被改为项目名称,但复制过去的还是标题。
注意:复制过去之后,要记得更新视图
//更新视图
SPView oView = destList.Views["所有项目"];
SPViewFieldCollection collViewFields = oView.ViewFields;
collViewFields.Add(field.InternalName);
oView.Update();
以下为测试代码:不是很完整
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace SPCopyListProject
{
class Program
{
static void Main(string[] args)
{
// Instantiate web instances
SPSite sourceSite = new SPSite(@"http://mossdev:13579/itws");
// SPWeb sourceWeb = sourceSite.RootWeb;
SPWeb sourceWeb = sourceSite.OpenWeb();
SPSite destSite = new SPSite(@"http://mossdev:13579/");
// SPWeb destWeb = destSite.RootWeb;
SPWeb destWeb = destSite.OpenWeb();
// Get a reference to the source list
//SPListCollection collSiteLists = sourceWeb.Lists;
//foreach (SPList oList in collSiteLists)
//{
// Console.WriteLine(oList.ID.ToString());
// Console.WriteLine(oList.Title.ToString());
// Console.WriteLine(oList.ParentWebUrl);
//}
SPList sourceList = sourceWeb.GetList("/itws/Lists/Projects");
//Console.WriteLine(sourceList);
//Console.ReadLine();
//return;
// SPListCollection listsCollects = sourceWeb.Lists;
// SPList sourceList = listsCollects["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
//SPlistTemplateType must be customList
Guid newListID = destWeb.Lists.Add(sourceList.Title, sourceList.Description, SPListTemplateType.GenericList);
SPList destList = destWeb.Lists[newListID];
// copy items
foreach (SPListItem item in sourceList.Items)
{
SPListItem newDestItem = destList.Items.Add();
foreach (SPField field in sourceList.Fields)
{
//Console.WriteLine(field.InternalName);
//Console.WriteLine(field.Title);
if (field.InternalName == "Author")
{
newDestItem.Fields.CreateNewField(field.Type.ToString(),"OldAuthor");
newDestItem.Update();
}
//Console.WriteLine(field.InternalName);
//Console.WriteLine(field.Title);
//标题被改为项目名称
if (!field.ReadOnlyField && field.InternalName != "Attachments")
{
if (!newDestItem.Fields.ContainsField(field.InternalName))
{
newDestItem.Fields.Add(field);
//更新视图
SPView oView = destList.Views["所有项目"];
SPViewFieldCollection collViewFields = oView.ViewFields;
collViewFields.Add(field.InternalName);
oView.Update();
}
newDestItem[field.InternalName] = item[field.InternalName];
}
// 删除目标项上已有的附件
for (int i = newDestItem.Attachments.Count; i > 0; i--)
{
newDestItem.Attachments.Delete(newDestItem.Attachments[i - 1]);
}
// 复制所有的附件
foreach (string fileName in item.Attachments)
{
SPFile file = item.ParentList.ParentWeb.GetFile(item.Attachments.UrlPrefix + fileName);
byte[] imageData = file.OpenBinary();
newDestItem.Attachments.Add(fileName, imageData);
}
}
newDestItem.Update();
}
// set quicklaunch settings
destList.OnQuickLaunch = sourceList.OnQuickLaunch;
destList.Update();
//SPViewCollection spviewcet = sourceList.DefaultView;
//foreach (SPView mySpview in spviewcet)
//{
// Console.WriteLine(mySpview.Title);
//}
//Console.ReadLine();
}
}
}
参考:
http://geekswithblogs.net/AnneBougie/archive/2009/01/23/copy-a-sharepoint-list.aspx