标签:style blog http color io os ar 使用 for
本节学习目标:用Linq读取Sharepoint List数据,并通过SPGridView展示数据
通过命令行定位到“c:\program files\common files\microsoft shared\web server extensions\14\bin”
spmetal.exe /web:http://intranet.contoso.com /namespace:Jonny.VisualWebPart1 /code:SPLinq.cs
备注:/web为你的站点,/namespace生成类的命名空间,最好和你要引用的项目命名空间一致,/code生成的类名。默认生成的类与spmetal.exe同一个目录
通过右键项目-->新增-->现有项-->选择我们刚刚生成的代理类
通过右键项目-->添加引用-->浏览(C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI)
在上一节创建的空web part中,添加SPGridView
<SharePoint:SPGridView id="spGridView" runat="server" AutoGenerateColumns="false"> <HeaderStyle HorizontalAlign="Left" ForeColor="Navy" Font-Bold="true" /> <Columns> <SharePoint:SPBoundField DataField="Title" HeaderText="Title"></SharePoint:SPBoundField> <SharePoint:SPBoundField DataField="JobTitle" HeaderText="JobTitle"></SharePoint:SPBoundField> <SharePoint:SPBoundField DataField="ProjectTitle" HeaderText="ProjectTitle"></SharePoint:SPBoundField> <SharePoint:SPBoundField DataField="DueDate" HeaderText="DueDate"></SharePoint:SPBoundField> </Columns> </SharePoint:SPGridView>
添加代码后的效果图
var dc = new SPLinqDataContext(SPContext.Current.Web.Url); var Employees = dc.GetList<EmployeesItem>("Employees"); var empQuery = from emp in Employees where emp.Project.DueDate < DateTime.Now.AddMonths(6) select new { emp.Title, emp.JobTitle, ProjectTitle = emp.Project.Title, DueDate = emp.Project.DueDate.Value.ToShortDateString() }; spGridView.DataSource = empQuery; spGridView.DataBind();
备注:代码运行前需在站点中存在"Employees"和"Project"的List, 并且‘Employees‘中有2个LookUp类型的字段是来自于‘Project‘(Title,DueDate)
使用Linq to sharepoint步骤
Linq to sharepoint优劣势
标签:style blog http color io os ar 使用 for
原文地址:http://www.cnblogs.com/splearning/p/4032505.html