通过前面两步打下的基础 , 

     1、Sharepoint学习笔记---SPList--使用Linq to Sharepoint间接查询External List(1.通过BCS创建External List)

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

    这里我们将使用Linq to Sharepoint来查询我们前面创建的Customer List: ACustomer.因为此List的内容是从External List:NWCustomer复制过来的,所以通过以它为中介来间接实现对NWCustomer的间接查询效果。下面进入操作步骤:

    此处,我们要基于前面的Sharepoint 项目,新创建一个Visual Webpart (命名为WPShowCustomerInfo), 如下图:

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

  我们要在此Webpart上添加两个控件,一个是Dropdownlist控件,它的值绑定到ACustomer的Country值上,由用户选择某个Country值,然后基于此值对ACustomer的Item值进行过滤,把过滤结果显示到另一个控件SPGridview上。效果如下图

 

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

   在添加了前面的Visual Webpart后,我们先不管它的内容,我们先要在项目中引用我们的Linq to Sharepoint需要用到的DLL,它的位置在

 C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\ 目录下。

名字是: Microsoft.Sharepoint.Linq.dll。如下图所示:

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

    引用添加好之后 ,我们就需要使用SPMetal来创建我们测试网站的Entity Classes了,操作步骤如下:

    首先在我们的项目上点击右键,在弹出菜单中选择打开项目在目录(Open Folder in Windows Exploer)

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

   这样就打开了我们项目所在目录,我们需要按住Shift键不放,在Windows Exploer的任何空白位置点击鼠标右键,在弹出的菜单中选择Open Command Windows Here,从而直接进入Windows的命令行窗口。效果如下图:

 

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

 在Windows命令行窗口中输入SPMetal如下命令: 

spmetal.exe /web:http://sp1dev:1600  /namespace:CopyListContent.WPShowCustomerInfo  /code:SPLinq.cs

 SPmetal的使用请参见MSDN: SPMetal 

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

  命令执行成功后,就在我们的项目目录下新建了一个名为SPLinq.cs的类,如下图

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

 

接下来我们需要做的就是回到我们VS2010的项目中,在此项目中添加已存在的项目,即我们上面创建的SPLinq.cs类。如下图:

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

 然后回到我们在本文的最开始创建的Visual Web Part,做相应的修改,如下图:

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

 

WPShowCustomerInfoUserControl.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="WPShowCustomerInfoUserControl.ascx.cs"
    Inherits
="CopyListContent.WPShowCustomerInfo.WPShowCustomerInfoUserControl" %>
<div>
    <table>
        <tr>
            <td>
                Select Country Filter
            </td>
            <td>
                <asp:DropDownList ID="drplstCountry" runat="server" AutoPostBack="True" 
                    OnSelectedIndexChanged
="drplstCountry_SelectedIndexChanged" Height="20px" 
                    Width
="145px">
                </asp:DropDownList>
            </td>
        </tr>
    </table>
    <SharePoint:SPGridView ID="spGridView" runat="server" AutoGenerateColumns="false">
                <HeaderStyle HorizontalAlign="Left" ForeColor="Navy" Font-Bold="true" />
                <Columns>
                    <SharePoint:SPBoundField DataField="BCSFindCompanyName" 
                        HeaderText
="CompanyName">
                    </SharePoint:SPBoundField>
                    <SharePoint:SPBoundField DataField="BCSFindContactName" 
                        HeaderText
="ContactName">
                    </SharePoint:SPBoundField>
                    <SharePoint:SPBoundField DataField="BCSFindContactTitle" 
                        HeaderText
="ContactTitle">
                    </SharePoint:SPBoundField>
                    <SharePoint:SPBoundField DataField="BCSFindCity" HeaderText="City">
                    </SharePoint:SPBoundField>
                    <SharePoint:SPBoundField DataField="BCSFindAddress" HeaderText="Address">
                    </SharePoint:SPBoundField>
                    <SharePoint:SPBoundField DataField="BCSFindRegion" HeaderText="Region">
                    </SharePoint:SPBoundField>
                    <SharePoint:SPBoundField DataField="BCSFindCountry" HeaderText="Country">
                    </SharePoint:SPBoundField>
                    <SharePoint:SPBoundField DataField="BCSFindPhone" HeaderText="Phone">
                    </SharePoint:SPBoundField>
                    <SharePoint:SPBoundField DataField="BCSFindFax" HeaderText="Fax">
                    </SharePoint:SPBoundField>
                    <SharePoint:SPBoundField DataField="BCSFindPostalCode" HeaderText="PostalCode">
                    </SharePoint:SPBoundField>
                </Columns>
            </SharePoint:SPGridView>
        </td>
</div>

相关文章: