您的位置:首页 > 其它

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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐