您的位置:首页 > 其它

MOSS 2010 linq to SharePoint

2011-09-20 11:02 399 查看
这里主要是介绍一下如何在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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: