使用客户端对象模型(Client-Side Object Model,即CSOM)和REST API来开发app,针对不同的host环境,有不同的限制,可以参考下图:
这张图告诉我们,如果开发SharePoint-hosted的app,只能使用javascript调用CSOM或者REST API,
如果开发Provider或者auto-hosted的app,可以使用C#来调用CSOM或者REST(当然这些C#代码是运行在remote web中的),也可以使用js来调用REST,但是不能使用JS调用CSOM。
使用客户端对象模型访问SharePoint
这张架构图的左边部分是使用客户端对象模型访问SharePoint的App,右边是SharePoint服务端,可以看到,在客户端有两种方式,一种是供JS调用的CSOM,一种是托管的C#代码调用的CSOM,它们通过web service向SharePoint发送请求(XML格式),接收SharePoint发回的JSON。在服务端处理请求的是Client.svc,它针对客户端的请求,调用服务端代码,返回结果。
使用REST API访问SharePoint
SharePoint 2010的时候,可以通过listdata.svc来访问SharePoint,在SharePoint 2013中,listdata.svc仍旧可以使用,但是推荐使用client.svc。我们可以通过_api来访问client.svc,例如:http://localhost/_api/site将返回站点集的属性(注意如果使用IE浏览器的时候,需要关闭feed view):
<?xml version="1.0" encoding="utf-8" ?>
<entry xml:base="http://sp2013test/_api/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml">
<id>http://sp2013test/_api/site</id>
<category term="SP.Site" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<link rel="edit" href="site" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/EventReceivers" type="application/atom+xml;type=feed" title="EventReceivers" href="site/EventReceivers" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Features" type="application/atom+xml;type=feed" title="Features" href="site/Features" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Owner" type="application/atom+xml;type=entry" title="Owner" href="site/Owner" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/RecycleBin" type="application/atom+xml;type=feed" title="RecycleBin" href="site/RecycleBin" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/RootWeb" type="application/atom+xml;type=entry" title="RootWeb" href="site/RootWeb" />
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/UserCustomActions" type="application/atom+xml;type=feed" title="UserCustomActions" href="site/UserCustomActions" />
<title />
<updated>2014-04-09T01:19:48Z</updated>
<author>
<name />
</author>
<content type="application/xml">
<m:properties>
<d:AllowDesigner m:type="Edm.Boolean">true</d:AllowDesigner>
<d:AllowMasterPageEditing m:type="Edm.Boolean">false</d:AllowMasterPageEditing>
<d:AllowRevertFromTemplate m:type="Edm.Boolean">false</d:AllowRevertFromTemplate>
<d:AllowSelfServiceUpgrade m:type="Edm.Boolean">true</d:AllowSelfServiceUpgrade>
<d:AllowSelfServiceUpgradeEvaluation m:type="Edm.Boolean">true</d:AllowSelfServiceUpgradeEvaluation>
<d:CompatibilityLevel m:type="Edm.Int32">15</d:CompatibilityLevel>
<d:Id m:type="Edm.Guid">8e5e5d0c-f242-49e8-bf17-055ec80cf3d0</d:Id>
<d:LockIssue m:null="true" />
<d:MaxItemsPerThrottledOperation m:type="Edm.Int32">5000</d:MaxItemsPerThrottledOperation>
<d:PrimaryUri>http://sp2013test/</d:PrimaryUri>
<d:ReadOnly m:type="Edm.Boolean">false</d:ReadOnly>
<d:ServerRelativeUrl>/</d:ServerRelativeUrl>
<d:ShareByLinkEnabled m:type="Edm.Boolean">false</d:ShareByLinkEnabled>
<d:ShowUrlStructure m:type="Edm.Boolean">false</d:ShowUrlStructure>
<d:UIVersionConfigurationEnabled m:type="Edm.Boolean">false</d:UIVersionConfigurationEnabled>
<d:UpgradeReminderDate m:type="Edm.DateTime">1899-12-30T00:00:00</d:UpgradeReminderDate>
<d:Upgrading m:type="Edm.Boolean">false</d:Upgrading>
<d:Url>http://sp2013test</d:Url>
</m:properties>
</content>
</entry>
一些常用的URI:
命名空间:
URI | 命名空间 |
---|---|
http://localhost/_api/ | SharePoint Foundation |
http://localhost/_api/search | Enterprise search |
http://localhost/_api/sp.userProfiles.peopleManager | User profiles |
常用的对象及属性:
URI | 对象和属性 |
---|---|
http://localhost/_api/site | Site对象 |
http://localhost/_api/web | Web对象 |
http://localhost/_api/site/url | Site的url属性 |
http://localhost/_api/web/lists | list集合 |
http://localhost/_api/web/lists('543456r5-r45w-4rds-34dd-d3f44563429v') | list集合索引其 |
http://localhost/_api/web/lists/getbytitle('Contacts') | list集合方法 |
http://localhost/_api/web/lists/getbytitle('Contacts')/items | item集合 |
使用$select和$order来选择特定的对象和排序
URI | 功能 |
---|---|
http://localhost/_api/web/lists/getbytitle('Modules')/items | 选择Modules列表中的所有字段 |
http://localhost/_api/web/lists/getbytitle('Modules')/items?$select=Title | 选择Modules列表中的Title字段 |
http://localhost/_api/web/lists/getbytitle('Modules')/items?$select=Title,Instructor/FullName&$expand=Instructor/FullName | 选择Title和Instructor这俩个字段,其中Instructor字段是一个lookup字段,使用$expand包含Instructor所lookup的字段“FullName” |
http://localhost/_api/web/lists/getbytitle('Modules')/items?$select=Title&$order=Modified | 选择Title字段并按照modified排序 |
使用$filter来过滤:
URI | 功能 |
---|---|
http://localhost/_api/web/lists/getbytitle('Contacts')/items?$filter=FirstName eq 'Sam' | 返回Contacts列表中,FirstName是Sam的item |
http://localhost/_api/web/lists/getbytitle('Contacts')/items?$filter=startwith(FirstName,'S') | 返回Contacts列表中,FirstName以S开头的item |
http://localhost/_api/web/lists/getbytitle('Contacts')/items?$filter=month(Modified) eq 8 | 返回Contacts列表中,在8月份有修改的item |
使用$top和$skip实现分页获取数据:
URI | 功能 |
---|---|
http://localhost/_api/web/lists/getbytitle('Contacts')/items?$top=10 | 返回Contacts列表的前十条item |
http://localhost/_api/web/lists/getbytitle('Contacts')/items?$top=10&$skip=10 | 返回Contacts列表的第十一到第二十条item |
http://localhost/_api/web/lists/getbytitle('Contacts')/items?$sort=Title&$top=10&$skip=10 | 返回第十一到第二十条item,并按照Title排序 |
代码示例参见:git@code.csdn.net:shrenk/sharepoint-applab.git