MOSS 2010 linq to SharePoint

 

这里主要是介绍一下如何在sharepoint 2010 中使用linq to sharepoint

首先我们新建一个bat执行文件: linq to sharepoint.bat 代码如下:

"%systemdrive%/Program Files/Common Files/Microsoft Shared/web server extensions/14/BIN/spmetal.exe"  /web:http://sharepoint02:9003 /code:Entities.cs /language:csharp

pause

 

这里我解释一下 :

/web 指的是要生成linq的站点,最终生成的代码是这个站点下的所有列表实体类和操作类。

/code 指的是生成的文件名 。

/language 指的是要生成的语言。例如 csharp就代表的是asp.net  的c# 。

关于spmetal.exe的详细用法,请参考 MSDN   http://msdn.microsoft.com/en-us/library/ee538255.aspx

OK,我们执行下这个linq to shartepoint.bat文件,如下图所示:

 

我们执行这个linq to shartepoint.bat 文件之后,生成的的Entities.cs 是存放在哪呢,

它存放在C:/Windows/System32/Entities.cs

我们把这个文件添加到sharepoint的项目进来,我们打开来看看它是什么样的,如下图所示:

 

我们发现这里有很多报错的痕迹,主要是因为我们没有引用Microsoft.Sharepoint.Linq.dll导致的。

它的路径地址是:

C:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/14/ISAPI/Microsoft.SharePoint.Linq.dll

我们在项目中创建一个可视化web部件 LinqToSharepointTest

LinqToSharepointTest.ascx 代码如下:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

    EnableModelValidation="True">

    <Columns>

        <asp:BoundField DataField="ID" HeaderText="ID" />

        <asp:BoundField DataField="标题" HeaderText="标题" />

        <asp:BoundField DataField="Url地址" HeaderText="Url地址" />

    <Columns>

<asp:GridView>

 

LinqToSharepointTest.cs代码如下:

 

using System;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Linq;

using Microsoft.SharePoint.Linq;

using Microsoft.SharePoint.WebControls;

namespace SharePointTest.LinqToSharepointTest

{

    public partial class LinqToSharepointTestUserControl : UserControl

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                BindData();

            }

        }

        private void BindData()

        {

            string SiteUrl = "http://sharepoint02:9003";

            EntitiesDataContext edc = new EntitiesDataContext(SiteUrl);

            var query = from sxt in edc.关于深信通

                        select new

                        {

                            sxt.Id,

                                sxt.标题,

                                Url地址=SiteUrl+"/Lists/List1/DispForm.aspx?ID="+sxt.Id

                        };

            GridView1.DataSource = query;

            GridView1.DataBind();

        }

    }

}

 

 

 

最后我们把这个webpart添加到该站点进来,效果如下 :

 

ID标题Url地址
2公司简介http://sharepoint02:9003/Lists/List1/DispForm.aspx?ID=2
3公司荣誉http://sharepoint02:9003/Lists/List1/DispForm.aspx?ID=3
4公司文化http://sharepoint02:9003/Lists/List1/DispForm.aspx?ID=4
6核心理念http://sharepoint02:9003/Lists/List1/DispForm.aspx?ID=6
7招贤纳士http://sharepoint02:9003/Lists/List1/DispForm.aspx?ID=7
8联系我们http://sharepoint02:9003/Lists/List1/DispForm.aspx?ID=8
9公司大事记http://sharepoint02:9003/Lists/List1/DispForm.aspx?ID=9
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值