设为首页 收藏本站
查看: 531|回复: 0

[经验分享] Sharepoint学习笔记---SPList--使用Linq to Sharepoint间接查询External List(3.使用Linq to Share

[复制链接]

尚未签到

发表于 2015-9-25 08:45:32 | 显示全部楼层 |阅读模式
  通过前面两步打下的基础 ,
  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), 如下图:
DSC0000.jpg
  我们要在此Webpart上添加两个控件,一个是Dropdownlist控件,它的值绑定到ACustomer的Country值上,由用户选择某个Country值,然后基于此值对ACustomer的Item值进行过滤,把过滤结果显示到另一个控件SPGridview上。效果如下图
  
DSC0001.jpg
  在添加了前面的Visual Webpart后,我们先不管它的内容,我们先要在项目中引用我们的Linq to Sharepoint需要用到的DLL,它的位置在
  C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\ 目录下。
  名字是: Microsoft.Sharepoint.Linq.dll。如下图所示:
DSC0002.jpg
  引用添加好之后 ,我们就需要使用SPMetal来创建我们测试网站的Entity Classes了,操作步骤如下:
  首先在我们的项目上点击右键,在弹出菜单中选择打开项目在目录(Open Folder in Windows Exploer)
DSC0003.jpg
  这样就打开了我们项目所在目录,我们需要按住Shift键不放,在Windows Exploer的任何空白位置点击鼠标右键,在弹出的菜单中选择Open Command Windows Here,从而直接进入Windows的命令行窗口。效果如下图:
  
DSC0004.jpg
  在Windows命令行窗口中输入SPMetal如下命令:


spmetal.exe /web:http://sp1dev:1600  /namespace:CopyListContent.WPShowCustomerInfo  /code:SPLinq.cs  SPmetal的使用请参见MSDN: SPMetal
DSC0005.jpg
  命令执行成功后,就在我们的项目目录下新建了一个名为SPLinq.cs的类,如下图
DSC0006.jpg
  
  接下来我们需要做的就是回到我们VS2010的项目中,在此项目中添加已存在的项目,即我们上面创建的SPLinq.cs类。如下图:
DSC0007.png
  然后回到我们在本文的最开始创建的Visual Web Part,做相应的修改,如下图:
DSC0008.png
  
  WPShowCustomerInfoUserControl.ascx代码如下 :

DSC0009.gif DSC00010.gif View Code

<%@ 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>  
  WPShowCustomerInfoUserControl.ascx.cs代码如下 :


using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.Linq;
using Microsoft.SharePoint;
using System.Linq;
namespace CopyListContent.WPShowCustomerInfo
{
    public partial class WPShowCustomerInfoUserControl : UserControl
    {
        EntityList<ACustomerItem> MyCustomers;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetDistinctCustomerCountries();
            }
        }
        protected void GetDistinctCustomerCountries()
        {
            var dc = new SPLinqDataContext(SPContext.Current.Web.Url);
            MyCustomers = dc.GetList<ACustomerItem>("ACustomer");
            var q = (from c in MyCustomers select c.BCSFindCountry).Distinct();
            this.drplstCountry.DataSource = q;
            this.drplstCountry.DataBind();
        }
        protected void drplstCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            string CountryStr = this.drplstCountry.SelectedValue.ToString().Trim();
            GetLookUpDataByCountry(CountryStr);
        }
        protected void GetLookUpDataByCountry(string CountryStr)
        {
            var dc = new SPLinqDataContext(SPContext.Current.Web.Url);
            var Customers = dc.GetList<ACustomerItem>("ACustomer");
            var customerQuery = from customer in Customers
                                where customer.BCSFindCountry == CountryStr
                                select new
                                {
                                    customer.BCSFindCompanyName,
                                    customer.BCSFindContactName,
                                    customer.BCSFindContactTitle,
                                    customer.BCSFindCity,
                                    customer.BCSFindAddress,
                                    customer.BCSFindRegion,
                                    customer.BCSFindCountry,
                                    customer.BCSFindPhone,
                                    customer.BCSFindFax,
                                    customer.BCSFindPostalCode
                                };
            spGridView.DataSource = customerQuery;
            spGridView.DataBind();
        }
    }
}  从上面代码中我们可以看到,我们在两个地方用到了Linq to Sharepoint,一处是创建Dropdownlist的数据源时,使用Linq来从ACustomer中获取Country的Distinct数据值列,然后和Dropdownlist进行绑定,用于向用户提供可供过滤的条件值。一处是在用户获取到Country的过滤条件后,使用此条件来过滤ACostomer List的Items,并把过滤结果在SPGridview中显示出来,在SPGridview控件中,我们设置了针对ACustomer的绑定列。
  
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-118419-1-1.html 上篇帖子: SharePoint 2013 App 开发—SharePoint Hosted方式, 下篇帖子: 有关SharePoint Client Object应用的笔记
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表