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

    上一篇(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

  

  Customer List命名为ACustomer,并设置在Quich Launch栏上显示出来

  

   创建好ACustomer List后,在Quich Launch栏点击它,进入它的List Setting设置界面

 

  

   在List Setting设置界面上,点击Create Column链接,以创建一个新的Column

 

  

 

    在Create Column界面中 ,按下图设置相关属性。在此处我们设置此Column为Lookup类型,并让它从NWCustomer List中获取信息,

在ACustomer的此Column的默认View中,我们选择显示NWCustomer List的所有字段。

  

     设置完成并按Create按钮后,进入如下效果,我们可以看到一个没有任何Item的ACustomer List。也就是说,我们在上面的步骤中,搭好了一个ACustomer的框架,下面我们将使用代码来从NWCustomer List中批量复制Item信息到我们此处创建的ACustomer List中。

  

 

  2、使用代码快速复制NWCustomer List的内容给ACustomer List。

     打开VS2010,新建一个Empty Sharepoint Project,此处我们命名为CopyListContent,此部分,我们将使用此项目来完成对NWCustomer List内容的复制,在下一部分,我们还会基于此项目完成创建一个Visual Web Part来通过Linq to Sharepoint展示我们对ACustomer List的查询操作。在这一步,我们设置如下图:

 

  

    把项目关联到我们的测试网站

  

 

  创建好空项目后,在此项目下新建一个Visual Web Part 

  

 

  在此Visual WebPart上添加一个按钮,并定义此按钮的Click事件。

 

   WPCopyListContentUserControl.ascx控件的定义代码如下: 

<%@ Assembly Name= " $SharePoint.Project.AssemblyFullName$ " %>
<%@ Assembly Name= " Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c " %> 
<%@ Register Tagprefix= " SharePoint " Namespace= " Microsoft.SharePoint.WebControls " Assembly= " Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c " %> 
<%@ Register Tagprefix= " Utilities " Namespace= " Microsoft.SharePoint.Utilities " Assembly= " Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c " %>
<%@ Register Tagprefix= " asp " Namespace= " System.Web.UI " Assembly= " System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 " %>
<%@ Import Namespace= " Microsoft.SharePoint " %> 
<%@ Register Tagprefix= " WebPartPages " Namespace= " Microsoft.SharePoint.WebPartPages " Assembly= " Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c " %>
<%@ Control Language= " C# " AutoEventWireup= " true " CodeBehind= " WPCopyListContentUserControl.ascx.cs " Inherits= " CopyListContent.WPCopyListContent.WPCopyListContentUserControl " %>
<div>
 <asp:Button ID= " btnCopyList " runat= " server " 
        Text= " Copy List From External List To Customer List " Width= " 336px " 
        Height= " 34px " οnclick= " btnCopyList_Click " /> 
</div>

 用于复制List Item的代码如下:

  #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

 

WPCopyListContentUserControl.ascx.cs全部代码如下(包括上面的List Copy代码) 

ExpandedBlockStart.gif 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)用代码快速删除,当然,你也可以手动实现删除。

 

转载于:https://www.cnblogs.com/wsdj-ITtech/archive/2011/10/11/2207090.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值