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及其相关代码后,整个项目如下图:
![](http://images.cnblogs.com/cnblogs_com/wsdj-ittech/2011Y/2011Y10M/BCS/2/c10.jpg)
把项目Build并部署到网站上。然后在此网站上创建一个WebPart页,在此页上引入我们上面创建的Visual WebPart控件,效果如下图:
![](http://images.cnblogs.com/cnblogs_com/wsdj-ittech/2011Y/2011Y10M/BCS/2/c11.jpg)
点击按钮,执行从NWCustomer List复制Items到ACustomer List的操作。完成后,进入网站点击ACustomer List,可以看到内容已经成功复制到此List下,效果如下图:
![](http://images.cnblogs.com/cnblogs_com/wsdj-ittech/2011Y/2011Y10M/BCS/2/c12.jpg)
如果你在复制Customer List的过程中有什么问题,想要清除Destination List(目标列表,此处就是ACustomer List)中的数据,则可以参照此文(Sharepoint学习笔记---SPList--清除List下的items与folders)用代码快速删除,当然,你也可以手动实现删除。
因此,本文主要分为两大部分。
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及其相关代码后,整个项目如下图:
![](http://images.cnblogs.com/cnblogs_com/wsdj-ittech/2011Y/2011Y10M/BCS/2/c10.jpg)
把项目Build并部署到网站上。然后在此网站上创建一个WebPart页,在此页上引入我们上面创建的Visual WebPart控件,效果如下图:
![](http://images.cnblogs.com/cnblogs_com/wsdj-ittech/2011Y/2011Y10M/BCS/2/c11.jpg)
点击按钮,执行从NWCustomer List复制Items到ACustomer List的操作。完成后,进入网站点击ACustomer List,可以看到内容已经成功复制到此List下,效果如下图:
![](http://images.cnblogs.com/cnblogs_com/wsdj-ittech/2011Y/2011Y10M/BCS/2/c12.jpg)
如果你在复制Customer List的过程中有什么问题,想要清除Destination List(目标列表,此处就是ACustomer List)中的数据,则可以参照此文(Sharepoint学习笔记---SPList--清除List下的items与folders)用代码快速删除,当然,你也可以手动实现删除。
相关文章推荐
- Sharepoint学习笔记---SPList--使用Linq to Sharepoint间接查询External List(3.使用Linq to Sharepoint查询List内容)
- Sharepoint学习笔记---SPList--使用Linq to Sharepoint间接查询External List(1.通过BCS创建External List)
- Sharepoint学习笔记---SPList--使用Linq to Sharepoint间接查询External List(3.使用Linq to Sharepoint查询List内容)
- SPList--使用Linq to Sharepoint间接查询External List(3.使用Linq to Sharepoint查询List内容)
- Sharepoint学习笔记---Linq to Sharepoint--如何获取Linq Query 生成的CALM
- Sharepoint学习笔记---Linq to Sharepoint--另一种生成Entity classes for sharepoint site的方法
- Sharepoint学习笔记---Linq to Sharepoint--如何获取Linq Query 生成的CALM
- Sharepoint学习笔记---Linq to Sharepoint--增,删,改操作
- Sharepoint学习笔记---SPList--External List的有关限制
- 使用 LINQ to SharePoint 写入内容数据库
- Sharepoint 复制备份系列--使用编程方式复制列表1(Copy a SharePoint List Programmatically)
- Sharepoint学习笔记---SPList--External List因BCS的Throttling limit 节流限制导致的错误
- SharePoint【学习笔记】-- 如何找到SharePoint List的Template ID
- 如何:使用 LINQ to SharePoint 进行查询
- Sharepoint学习笔记---如何找到SharePoint List的Template ID
- Sharepoint 复制备份系列--使用编程方式复制列表2(Copy a SharePoint List or site ,web Programmatically)
- 在LINQ to SharePoint中使用创建时间,创建者,修改时间,修改者
- C# 对sharepoint 列表的一些基本操作,包括添加/删除/查询/上传文件给sharepoint list添加数据
- C# 对sharepoint 列表的一些基本操作,包括添加/删除/查询/上传文件给sharepoint list添加数据
- MOSS Search学习记录(十):MOSS Visual How To使用SharePoint Server 2007搜索对象模型编程创建搜索查询