您的位置:首页 > 其它

Sharepoint学习笔记---SPList--使用Linq to Sharepoint间接查询External List(2.复制External List内容)

2011-10-11 18:43 597 查看
上一篇(Sharepoint学习笔记---SPList--使用Linq to Sharepoint间接查询External List(1.通过BCS创建External List))我们创建了一个名为NWCustomer的External List,它的内容来自于SQL SERVER数据库的Northwind的Customer表。此处我们将讲述如何创建一个Customer List来快速引用External List并通过代码复制NWCustomer的Items。在此基础上,我们将在下一篇讲述如何使用Linq来查询相关信息(arepoint学习笔记---SPList--使用Linq to Sharepoint间接查询External List(3.使用Linq to Sharepoint查询List内容))

因此,本文主要分为两大部分。

1、创建Customer List: ACustomer,并基于此List,创建Lookup Column来引用 NWCustomer List的字段

打开的你网站,点击Action下的More Options,进入下面的界面,点击Customer List

View Code using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;

namespace CopyListContent.WPCopyListContent
{
public partial class WPCopyListContentUserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}

protected void btnCopyList_Click(object sender, EventArgs e)
{
string sourceListName = "NWCustomer";
string destiListName = "ACustomer";
SpCopyToLookUpList(sourceListName, destiListName);
}

#region Copy List From Exteranl List To Customer List
protected void SpCopyToLookUpList(string sourceListName, string desListName)
{

SPWeb currentWeb = SPContext.Current.Web;

// Get a reference to the source list and dest List
SPList sourceList = currentWeb.Lists[sourceListName];
SPList destList = currentWeb.Lists[desListName];

// copy items
foreach (SPListItem item in sourceList.Items)
{
CopyToLookUpItem(item, destList);
}

destList.Update();

}
#endregion

#region Copy List Items
private SPListItem CopyToLookUpItem(SPListItem sourceItem, SPList destinationList)
{
//Copy sourceItem to destinationList
SPListItem targetItem = destinationList.Items.Add();

foreach (SPField f in sourceItem.Fields)
{
//Copy all except attachments.
if (!f.ReadOnlyField && f.InternalName != "Attachments"
&& null != sourceItem[f.InternalName])
{
targetItem["BCSFind: " + f.InternalName] = sourceItem[f.InternalName];
}

if (f.Title == "CustomerID")
{
targetItem["BCSFind: " + f.Title] = sourceItem[f.Title];
targetItem["Title"] = sourceItem[f.Title];
}

}

targetItem.Update();

//Copy attachments
foreach (string fileName in sourceItem.Attachments)
{
SPFile file = sourceItem.ParentList.ParentWeb.GetFile(sourceItem.Attachments.UrlPrefix + fileName);
byte[] imageData = file.OpenBinary();
targetItem.Attachments.Add(fileName, imageData);
}

return targetItem;
}
#endregion
}
}

创建好Visual WebPart及其相关代码后,整个项目如下图:



 

 把项目Build并部署到网站上。然后在此网站上创建一个WebPart页,在此页上引入我们上面创建的Visual WebPart控件,效果如下图:



 点击按钮,执行从NWCustomer List复制Items到ACustomer List的操作。完成后,进入网站点击ACustomer List,可以看到内容已经成功复制到此List下,效果如下图:



如果你在复制Customer List的过程中有什么问题,想要清除Destination List(目标列表,此处就是ACustomer List)中的数据,则可以参照此文(Sharepoint学习笔记---SPList--清除List下的items与folders)用代码快速删除,当然,你也可以手动实现删除。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐