惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Troy Hunt's Blog
Scott Helme
Scott Helme
T
Threat Research - Cisco Blogs
T
Tenable Blog
L
LINUX DO - 热门话题
V
Visual Studio Blog
I
Intezer
Blog — PlanetScale
Blog — PlanetScale
Cisco Talos Blog
Cisco Talos Blog
A
Arctic Wolf
C
Cyber Attacks, Cyber Crime and Cyber Security
F
Fortinet All Blogs
aimingoo的专栏
aimingoo的专栏
Know Your Adversary
Know Your Adversary
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
N
Netflix TechBlog - Medium
SecWiki News
SecWiki News
I
InfoQ
Microsoft Security Blog
Microsoft Security Blog
Project Zero
Project Zero
W
WeLiveSecurity
Microsoft Azure Blog
Microsoft Azure Blog
A
About on SuperTechFans
Recorded Future
Recorded Future
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Vercel News
Vercel News
S
Securelist
Spread Privacy
Spread Privacy
L
LangChain Blog
云风的 BLOG
云风的 BLOG
G
Google Developers Blog
MongoDB | Blog
MongoDB | Blog
Google DeepMind News
Google DeepMind News
Recent Commits to openclaw:main
Recent Commits to openclaw:main
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
The Last Watchdog
The Last Watchdog
Attack and Defense Labs
Attack and Defense Labs
博客园 - 司徒正美
Help Net Security
Help Net Security
L
Lohrmann on Cybersecurity
人人都是产品经理
人人都是产品经理
Forbes - Security
Forbes - Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
PCI Perspectives
PCI Perspectives
博客园 - 【当耐特】
T
Tor Project blog

博客园 - Sunmoonfire

SharePoint 2013无代码实现列表视图的时间段动态筛选 SharePoint 2013:自定义ECB菜单项的添加 SharePoint Foundation 2013 with SP1 在SharePoint列表中使用自增栏 SharePoint 2010自定义母版页小技巧——JavaScript和CSS引用 修改SharePoint列表项显示“新”图标的天数 通过PowerShell实现SharePoint列表增删改 在SharePoint 2010页面中嵌入SWF文件 在LINQ to SharePoint中使用创建时间,创建者,修改时间,修改者 修改SharePoint页面上的控件数量的限制 SharePoint 2010:部署.resx(资源)文件到App_GlobalResources的简单方法 在SharePoint 2010中使用jQuery 在SharePoint 2010环境下区分w3wp进程 打SharePoint 2010 SP1后访问用户配置文件同步服务应用程序出错的解决办法 使用[本人]创建视图筛选时的一个问题和解答 "缺少服务器端相关性"的内容定位 SharePoint 2010开发工具图解系列:Visual Studio 2010创建事件接收器 SharePoint 2010开发工具图解系列:Visual Studio 2010创建列表 SharePoint 2010开发工具图解系列:Visual Studio 2010 SharePoint Tool入门
SharePoint 2010之LINQ与SPMetal
Sunmoonfire · 2012-08-07 · via 博客园 - Sunmoonfire

介绍

在本文中,我们将介绍以下高级开发领域中的内容:
•LINQ to SharePoint
•SPMetal
LINQ to SharePoint 是SharePoint 2010的一项新特性。 LINQ 本身是 Language Integrated Query 的意思,它是 .NET的一个组成部分。 LINQ的设计目标是使用相同的类型化查询语法来支持不同类型的数据源。到目前为止,它可以支持Objects, Datasets, SQL, Entities, XML等。

为什么我们需要 LINQ?

从前我们针对 List 的编程都是使用相应的栏名来访问。有了LINQ 之后,我们就使用类型化的方式访问列表了。换句话说,使用类似访问数据库的方式去访问列表项。
就像下面这样:

var result = from c in Citizen where c.Name == “John” select c;

什么是SPMetal?

我们创建了一个包含自定义栏的自定义列表后,还需要创建一个实体模型(Entity Model)与之对应,这样才能用LINQ来访问它。 SPMetal.exe 就是一个帮助我们生成 Model 类的工具。尽管我们可以手工创建Model 类,但毕竟这属于枯燥单调的工作,并且很容易出错。 所以用SPMetal 才是生成model 类的王道。

活动

本文将包含以下活动:
1.    Manager 列表创建
2.    实体创建
3.    使用LINQ读取
4.    插入实体
5.    更新实体
6.    删除实体

开始 LINQ 和SPMetal 的体验之旅

作为实验对象,我们创建了一个自定义列表,名为Manager. 添加如下的自定义栏和数据:

生成实体模型

现在,我们就可以为以上列表生成实体模型。使用这个文件夹下的 SPMetal.exe来生成模型类:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN
打开命令行,进到该文件夹:

运行如下命令:

SPMetal.exe /web:http://你的站点 /code:SiteEntities.cs

 过一会儿我们就会生成出一个新文件。打开SiteEntities 文件,我们就可以看到其中包含的Manager 类。

创建应用程序

新建一个SharePoint > 2010 > 控制台应用程序 (目标框架 .NET 3.5 framework) 并将 SiteEntities.cs 文件添加到该项目。

添加如下程序集引用:

我们可以尝试以下操作: 通过LINQ to SharePoint读取,插入,更新,删除。

读取项

我们先试一下查询country 为USA 的managers :

using (SiteEntitiesDataContext ctx = new SiteEntitiesDataContext("http://moss.contoso.com/sites/Lab01"))
{
          var result = ctx.Manager.Where(m => m.国家 == 国家.USA);
           foreach (Manager项目 manager in result)
           {
                Console.WriteLine(manager.名称);
           }
}

注: 我们可以使用 LINQ 或 Lambda 表达式来进行查询。上面的例子中我们用的是Lambda。
运行该应用程序,我们可以看到如下的结果。

插入项

如果要在Manager 列表中插入数据,可以使用如下代码:

using (SiteEntitiesDataContext ctx = new SiteEntitiesDataContext("http://moss.contoso.com/sites/Lab01"))
{
         Manager项目 manager = new Manager项目();
         manager.名称 = "新的经理";
         manager.地址 = "新的地址";
         manager.国家 = 国家.China;
         ctx.Manager.InsertOnSubmit(manager);
         ctx.SubmitChanges();
}

 执行以上程序后,打开SharePoint 中的Manager 列表,可以看到如下结果:

更新项

如果要更新SharePoint 中的列表项,可以使用如下代码:

using (SiteEntitiesDataContext ctx = new SiteEntitiesDataContext("http://moss.contoso.com/sites/Lab01"))
 {
        Manager项目 manager = ctx.Manager.Where(m => string.IsNullOrEmpty(m.标题)).FirstOrDefault();
         if (manager != null)
         {
               manager.标题 = "新的标题";
         }
          ctx.SubmitChanges();
}

 我们可以在SharePoint 上看到更新后的实体:

删除项

如果要删除SharePoint 中的列表项,可以使用如下代码:

using (SiteEntitiesDataContext ctx = new SiteEntitiesDataContext("http://moss.contoso.com/sites/Lab01"))
{
        Manager项目 manager = ctx.Manager.Where(m => m.标题.Length>3).FirstOrDefault();
        if (manager != null)
        {
             ctx.Manager.DeleteOnSubmit(manager);
         }
         ctx.SubmitChanges();
}

 我们可以看到在SharePoint 中的该项已被删除:

以上就是如何通过LINQ to SharePoint 实现增删改查。希望大家都可以掌握。

总结

本文中我们使用了 LINQ 和SPMetal 工具。这些都是在实际的编程场景中经常使用的工具。

参考资料

http://msdn.microsoft.com/en-us/library/ee535491.aspx
http://msdn.microsoft.com/en-us/library/ee538255.aspx
SharePoint 2010 – LINQ and SPMetal