SharePoint Add-in Model (App Model) 介绍 – 概念、托管方式、开发语言

SharePoint Add-in Model 是自 2013 版本以来引入的新的扩展性开发模型, SharePoint 开发者可以利用这种新模型来实现往常利用场解决方案 (Farm Solution)或沙盒解决方案 (Sandbox Solution)进行的站点定制化 (Customizations)。提到站点定制化,常见的用户场景包括:

  • 实现一个自定义的Web 组件 (WebPart), 使得用户在编辑网站页面时可以添加该 Web 组件到页面上。
  • 实现一个事件接收器 (Event Receiver),使得文档或列表发生新增、删除、更新等操作时触发相应的逻辑。
  • 实现一个计时器任务 (TimerJob), 定期执行某项任务。
  • 通过程序化的方式定义一个列表,并对其进行增删改
  • 定义网站栏( Site Column)、内容类型(Content Types)
  • 程序化的方式创建网站集 (Site Collection)、网站 (Site),并创建新的文档库(Document Libarary),为网站设置主题(Theme),
  • 程序化的方式部署母版页(Master Page)、网页布局(Page Layouts)
  • 更多...

这些定制化在 Add-in Model 中绝大多数都是被支持的, 我们会在后续的文章中深入探讨。具体如何实现,可以参考 GitHub 上的开源代码实现: O365 Patterns and Practices (PnP) 项目

什么是 Add-in Model?

它是 SharePoint 提供的用来让开发者扩展自身功能的一种开发模型, 基于这种模型开发出的定制化解决方案, 通常称作 SharePoint Add-ins(我们暂且称它为定制化程序)。

Add-in 模型提供了一种机制, 让开发者像开发普通 Web 应用程序一样开发 SharePoint 定制化程序。这使得围绕 SharePoint 定制化的开发变得更加开放, 开发者可以使用更多的 Web 技术、框架快速构建出自己想要的解决方案。所以,简单说来: 基于 Add-in Model 的开发, 就是 Web 应用程序的开发。

简单说来, Add-ins 就是 Web 应用程序。

“如果你了解如何开发常见 Web 应用程序, 那么,你已经了解了如何基于 Add-in 模型去开发 SharePoint 定制化方案了…”

如何托管定制化方案(Add-ins)

既然基于 Add-in 模型的开发,更多的是 Web 应用程序的开发,那么开发出来的 Add-ins 如何部署, 托管在哪呢?

Add-in 模型提供了两种方式: SharePoint 自托管、Provider-Hosted 方式。

基于 SharePoint自托管( SharePoint-hosted) 方式实现的 Add-ins

特点描述
Add-ins 入口安装后,网站内容(Site Content)中包含入口瓷块(tile)
可以包含哪些 UI 组件 add-in 组件(parts) 和 自定义按钮(Custom Actions. that is, custom ribbon buttons or menu items)。 可以通过 XML 文件的方式在 Add-ins 中包含如下组件:
  • 自定义页面(Custom Page)
  • 工作流(Workflows)
  • Modules (sets of files)
  • 列表模板(List templates)
  • 列表和库的实例(List and library instances)
  • 自定义的列表表单和视图(Custom list forms and views)
  • 自定义内容类型(Custom content types)
  • 网站模板(Web templates)
  • 内置网站列(Built-in columns (not custom columns))
  • 内置 Web 组件(Built-in Web Parts (not custom Web Parts))
  • JavaScript 文件
  • Add-ins 网站内部自定义按钮和菜单项(Custom buttons and menu items)
  • Add-in 组件(add-in parts)
  • 自定义动作(custom actions),包括自定义的Ribbon按钮(custom ribbon buttons)或者 菜单项(menu items)
可用的编程语言
  • SharePoint 的客户端对象模型提供了 JavaScript 版本的库, 可以用来在 Add-ins 中增删改查(CRUD) SharePoint 网站上的数据。
  • 自定义的页面通常是一些 ASP.NET 页面(ASPX),他们可以引用 ASP.NET 和 SharePoint 内置的控件,但是不能使用 Code Behind。但是如果需要对 SharePoint 控件进行自定义,可以利用 Client-Side rendering 技巧
  • Add-ins 中的 JavaScript 可以访问该应用网站外的数据( 包括所其在 SharePoint 网站上的数据和资源, 也可以是互联网上的任何资源, 可以用如下两种方式: 1. 利用 JavaScript 跨域库 2. JavaScript WebProxy 类。
</td>

基于 Provider-Hosted 方式实现的 Add-ins

SharePoint 自托管方式支持的组件, 基于 provider-hosted 方式的 Add-ins 中也支持。

Provider-Hosted 方式, 是指将开发出来的 Add-ins 托管在 SharePoint farm 外部的任何服务器上, 这些服务器从硬件的角度上可以是开发者指定的一台物理机或者 Windows Azure 上的虚拟机。 而从软件的角度上, 它可以是由 Microsoft IIS 做支撑,也可有由开源的 Apache、Nginx、Tomcat等等。开发者可以利用这些 Web 服务器支持的开发语言如 C#、 JS、HTML、PHP、JAVA 等等来进行开发。

由于 Add-ins 托管在 Farm 外面, 所以如果实现和 SharePoint 网站风格一致,可以利用 SharePoint 提供的 Chrome 控件。代码如下:

$(document).ready(function () {
    //Get the URI decoded SharePoint site url from the SPHostUrl parameter.
    var spHostUrl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));
    //Get the URI decoded SharePoint app web url from the SPAppWebUrl parameter.
    var appWebUrl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl'));
    //Get the isDialog from url parameters
    var isDialog = decodeURIComponent(getQueryStringParameter('IsDlg'));

    //Build absolute path to the layouts root with the spHostUrl
    var layoutsRoot = spHostUrl + '/_layouts/15/';

    //load all appropriate scripts for the page to function
    $.getScript(layoutsRoot + 'SP.Runtime.js',
        function () {
            $.getScript(layoutsRoot + 'SP.js',
                function () {
                    //Create a Link element for the defaultcss.ashx 
					//resource
                    var linkElement = document.createElement('link');
                    linkElement.setAttribute('rel', 'stylesheet');
                    linkElement.setAttribute('href', layoutsRoot + 'defaultcss.ashx');

                    ////Add the linkElement as a child to the head 
					//section of the html
                    var headElement = document.getElementsByTagName('head');
                    headElement[0].appendChild(linkElement);


                    //Load the SP.UI.Controls.js file to render the App Chrome
                    $.getScript(layoutsRoot + 'SP.UI.Controls.js', renderSPChrome);
                });
    });

    function chromeLoaded() {
        $('body').show();
    }

    //function callback to render chrome after SP.UI.Controls.js loads
    function renderSPChrome() {
        //Set the chrome options for launching Help, Account, 
		// and Contact pages
        var options = {
            'appTitle': document.title,
            'onCssLoaded': 'chromeLoaded()'
        };

        //Load the Chrome Control in the divSPChrome element of the page
        var chromeNavigation = new SP.UI.Controls.Navigation('divSPChrome', options);
        chromeNavigation.setVisible(true);
    }

Remote data can be blobs, caches, message queues, content delivery networks (CDN), and databases, among others. And databases can be any type including relational and object-oriented. The remote data can be accessed in a variety of ways. For example, you can use Business Connectivity Services (BCS) to surface the data in a SharePoint list. Another option is to expose data in a grid on a page of a remote web application.

Add-ins 可以利用 APIs 来和 SharePoint 资源进行交互, 主要包括以下几种:

  • 利用 .NET 开发时, 可使用 SharePoint 客户端对象模型 (Client-Side Object Model 简称 CSOM)
  • REST/OData APIs

SharePoint Add-ins use SharePoint APIs to connect and integrate with SharePoint features—search, workflow, social networking, taxonomy, user profiles, BCS, and more. This lets them read documents, do searches, connect people, and perform CRUD operations.

内容同步发布在 simpeng.net 和 http://www.cnblogs.com/simpeng/p/4900591.html

posted @ 2015-10-22 13:09  SIMPENG  阅读(1849)  评论(0编辑  收藏  举报