SharePoint 2013 App Development读书笔记5

来源:互联网 发布:怎么加油最省钱 知乎 编辑:程序博客网 时间:2024/04/28 03:33

使用客户端对象模型(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 Foundationhttp://localhost/_api/searchEnterprise searchhttp://localhost/_api/sp.userProfiles.peopleManagerUser profiles

常用的对象及属性:

URI对象和属性http://localhost/_api/siteSite对象http://localhost/_api/webWeb对象http://localhost/_api/site/urlSite的url属性http://localhost/_api/web/listslist集合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')/itemsitem集合

使用$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的itemhttp://localhost/_api/web/lists/getbytitle('Contacts')/items?$filter=startwith(FirstName,'S')返回Contacts列表中,FirstName以S开头的itemhttp://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列表的前十条itemhttp://localhost/_api/web/lists/getbytitle('Contacts')/items?$top=10&$skip=10返回Contacts列表的第十一到第二十条itemhttp://localhost/_api/web/lists/getbytitle('Contacts')/items?$sort=Title&$top=10&$skip=10返回第十一到第二十条item,并按照Title排序

代码示例参见:git@code.csdn.net:shrenk/sharepoint-applab.git

0 0
原创粉丝点击