Copy List trong Sharepoint sử dụng C# và ASP.NET

Ngày đưa:  07/08/2019 10:36:00 AM In bài
Trong Sharepoint có nhiều cách để copy dữ liệu giữa 2 List như phương pháp Save List As template, copy dòng dữ liệu trực tiếp giữa hai List, sử dụng code C# trong ASP.NET, copy trực tiếp trong SQLSERVER…Với các List có dữ liệu lớn, chúng ta sử dụng code C trong ASP.NET để copy cũng là một giải pháp hợp lý và tối ưu.

 Ta tạo 1 hàm trog ASP.NET với C# như sau, có thể hàm trả về hoặc không

public void CopyList(string siteUrl, string ListSource, string ListDestination{
 
using (SPSite site = new SPSite(siteUrl))
{using (SPWeb web = site.OpenWeb()){
web.AllowUnsafeUpdates = true;
SPList sourceList = web.Lists.TryGetList(ListSource);
SPQuery query = new SPQuery();
query.Query = “Điều kiện ở đây nếu có”;
foreach (SPListItem sourceItem in sourceList.GetItems(query))
{
SPList destinationList = web.Lists.TryGetList(ListDestination);
if (destinationList != null)
{
SPListItem destItem = destinationList.Items.Add();
foreach (SPField field in sourceItem.Fields)
{
if (!field.ReadOnlyField && !field.Hidden && field.InternalName != “Attachments”)
{
if (destItem.Fields.ContainsField(field.InternalName))
{
destItem[field.InternalName] = sourceItem[field.InternalName];
}
}
}
//copy file attachment
foreach (string fileName in sourceItem.Attachments)
{
SPFile file = sourceItem.ParentList.ParentWeb.GetFile(sourceItem.Attachments.UrlPrefix + fileName);
byte[] imageData = file.OpenBinary();
destItem.Attachments.Add(fileName, imageData);
}
destItem.Update();
}
}
web.AllowUnsafeUpdates = false;
}
}
}
Add Hàm trên vào 1 webpart nào đó để chạy. Với những List có dữ liệu lớn ta thêm điều kiện lọc vào chuỗi query.Query = “Điều kiện ở đây nếu có” để lọc dữ liệu theo ý muốn.

Bản quyền ©2011 Trung tâm CNTT và TT Quảng Ngãi