SharePoint 2010中使用LINQ To SharePint
2011-12-14 10:46
218 查看
SharePoint 2010中使用LINQ To SharePint 是很方便的,让我们能够使用更加通用的LINQ来替代SharePoint 中传统的CAML查询.
1. 添加引用
添加对Microsoft.SharePoint.LINQ.dll的引用,它在路径Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\下。
2. 生成LINQ to SharePoint Proxy类,通过它来访问SharePoint站点中的List
使用spmetal.exe工具,它能根据SharePoint站点中的List定义生成对应C#或VB.Net类(默认C#),类似于使用WSDL.exe为Web Service生成Proxy类。它存放在路径Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\BIN下。关于它的详细介绍请参阅MSDN:http://msdn.microsoft.com/en-us/library/ee538255(office.14).aspx。
该命令的使用格式:spmetal /web:http://YourServerName /namespace:YourNameSpace /code:YourClassName
这是基本的参数,此外还有language、user、password等,不再一一介绍,请参阅MSDN。
此外,code参数是你对应的CS代码文件的完整路径,如果仅仅是.CS代码文件名,默认存放在Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\BIN下。
这样生成的类包含了对应站点下所有结构的定义,比如List,Image,Document,以及Item、Folder等等,建议大家仔细看一下。
生成之后,将该文件附加到我们的控制台应用程序项目中。
3. 使用LINQ to SharePoint
这样,我们就可以在应用程序中使用LINQ to SharePoint了,比如,我要查询站点中Contacts这个列表,条件是LastName为Bush,只取FirstName、LastName、Company和JobTitle四列。
DataContext dc = new DataContext("SharePoint站点路径");
EntityList<Contact> contacts = dc.GetList<Contact>("Contacts");
var empQuery = from emp in contacts
where emp.LastName == "Bush"
select new { emp.FirstName, emp.LastName, emp.Company, emp.JobTitle };
foreach (var u in conQuery)
Console.WriteLine(u);
再比如,我要向Contacts列表中插入一条记录:
DataContext data = new DataContext("SharePoint站点路径");
EntityList<Contact> contacts = data.GetList<Contact>("Contacts");
// Create the item to be added
ContactsContact newContact = new ContactsContact
{
LastName = "Obama",
FirstName = "Barack",
};
// Mark the item to be added on the next call of Submit
contacts.InsertOnSubmit(newContact);
// Submit all changes
data.SubmitChanges();
1. 添加引用
添加对Microsoft.SharePoint.LINQ.dll的引用,它在路径Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\下。
2. 生成LINQ to SharePoint Proxy类,通过它来访问SharePoint站点中的List
使用spmetal.exe工具,它能根据SharePoint站点中的List定义生成对应C#或VB.Net类(默认C#),类似于使用WSDL.exe为Web Service生成Proxy类。它存放在路径Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\BIN下。关于它的详细介绍请参阅MSDN:http://msdn.microsoft.com/en-us/library/ee538255(office.14).aspx。
该命令的使用格式:spmetal /web:http://YourServerName /namespace:YourNameSpace /code:YourClassName
这是基本的参数,此外还有language、user、password等,不再一一介绍,请参阅MSDN。
此外,code参数是你对应的CS代码文件的完整路径,如果仅仅是.CS代码文件名,默认存放在Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\BIN下。
这样生成的类包含了对应站点下所有结构的定义,比如List,Image,Document,以及Item、Folder等等,建议大家仔细看一下。
生成之后,将该文件附加到我们的控制台应用程序项目中。
3. 使用LINQ to SharePoint
这样,我们就可以在应用程序中使用LINQ to SharePoint了,比如,我要查询站点中Contacts这个列表,条件是LastName为Bush,只取FirstName、LastName、Company和JobTitle四列。
DataContext dc = new DataContext("SharePoint站点路径");
EntityList<Contact> contacts = dc.GetList<Contact>("Contacts");
var empQuery = from emp in contacts
where emp.LastName == "Bush"
select new { emp.FirstName, emp.LastName, emp.Company, emp.JobTitle };
foreach (var u in conQuery)
Console.WriteLine(u);
再比如,我要向Contacts列表中插入一条记录:
DataContext data = new DataContext("SharePoint站点路径");
EntityList<Contact> contacts = data.GetList<Contact>("Contacts");
// Create the item to be added
ContactsContact newContact = new ContactsContact
{
LastName = "Obama",
FirstName = "Barack",
};
// Mark the item to be added on the next call of Submit
contacts.InsertOnSubmit(newContact);
// Submit all changes
data.SubmitChanges();
相关文章推荐
- SharePoint 2010中的LINQ To SharePint
- Sharepoint学习笔记---SPList--使用Linq to Sharepoint间接查询External List(2.复制External List内容)
- Sharepoint学习笔记---SPList--使用Linq to Sharepoint间接查询External List(3.使用Linq to Sharepoint查询List内容)
- Sharepoint学习笔记---SPList--使用Linq to Sharepoint间接查询External List(1.通过BCS创建External List)
- Sharepoint学习笔记---SPList--使用Linq to Sharepoint间接查询External List(3.使用Linq to Sharepoint查询List内容)
- MOSS 2010:Visual Studio 2010开发体验(15)——LINQ to SharePoint
- Sharepoint学习笔记---Linq to Sharepoint--增,删,改操作
- SharePoint 2010 查询不以某个字符开头的数据[How to Create a SharePoint “Does Not Begin With” Filtered List View]---计算栏的妙用
- 浅谈使用Linq to sharepoint时可能遇到的2个问题
- SP 2010: Getting started with LINQ to SharePoint in SharePoint 2010
- 在SharePoint 2010中使用Linq时候,请注意特殊字符
- Sharepoint学习笔记---Linq to Sharepoint--如何获取Linq Query 生成的CALM
- How to Integrate SAP Business Data Into SharePoint 2010 Using Business Connectivity Services and LINQ to SAP
- Sharepoint学习笔记---Linq to Sharepoint--另一种生成Entity classes for sharepoint site的方法
- Sharepoint学习笔记---Linq to Sharepoint--如何获取Linq Query 生成的CALM
- MOSS 2010 linq to SharePoint
- LINQ To SharePint - 解决中文类名问题
- SPList--使用Linq to Sharepoint间接查询External List(3.使用Linq to Sharepoint查询List内容)
- LINQ To SharePint - 缺失Created By/Modified By 字段
- Sharepoint2010如何使用Linq to Sharepoint