moss 2010 linq to sharepoint

来源:互联网 发布:戴尔游匣开机优化 编辑:程序博客网 时间:2024/04/29 20:05

这里主要是介绍一下如何在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=23公司荣誉http://sharepoint02:9003/Lists/List1/DispForm.aspx?ID=34公司文化http://sharepoint02:9003/Lists/List1/DispForm.aspx?ID=46核心理念http://sharepoint02:9003/Lists/List1/DispForm.aspx?ID=67招贤纳士http://sharepoint02:9003/Lists/List1/DispForm.aspx?ID=78联系我们http://sharepoint02:9003/Lists/List1/DispForm.aspx?ID=89公司大事记http://sharepoint02:9003/Lists/List1/DispForm.aspx?ID=9

 

 

学习文章地址:

http://www.cnblogs.com/chenxizhang/archive/2010/04/26/1721573.html

http://msdn.microsoft.com/zh-cn/library/ee538250.aspx

 

原创粉丝点击