SharePoint 加载项SharePoint Add-ins

SharePoint 加载项有下列两种基本类型:SharePoint 托管和提供商托管。There are two basic kinds of SharePoint Add-ins: SharePoint-hosted and provider-hosted. 为了能够做出最明智的决策,以选择最适合方案开发的加载项类型,请先了解这两种类型 SharePoint 加载项的共同点。To make the best decision about the right kind to develop for your scenario, start by learning what both types of SharePoint Add-ins have in common.

备注

设备应用(如移动应用)并不算是真正的“SharePoint 加载项”,即使应用访问 SharePoint,也不例外。A device app, such as mobile app, isn't really a "SharePoint Add-in" even when it accesses SharePoint. 对于从 SharePoint 外部启动的 Web 应用,也是如此。The same is true of a web application that is launched from outside of SharePoint. 若要开发此类应用,请参阅从移动和原生设备应用访问 SharePointIf you want to develop any of these kinds of apps, see Access SharePoint from mobile and native device apps.

  • SharePoint 加载项为独立式功能,可扩展 SharePoint 网站功能,从而解决明确定义的业务问题。A SharePoint Add-in is a self-contained piece of functionality that extends the capabilities of SharePoint websites to solve a well-defined business problem.

  • 加载项不包含在 SharePoint 服务器上运行的自定义代码Add-ins don't have custom code that runs on SharePoint servers. 相反,所有自定义逻辑都可以“上移”到云中,也可以“下移”到客户端计算机中,亦可以“迁移”到 SharePoint 场或 SharePoint Online 订阅外的本地服务器。Instead, all custom logic moves "up" to the cloud, or "down" to client computers, or "over" to an on-premises server that is outside the SharePoint farm or SharePoint Online subscription. 不在 SharePoint 服务器上运行自定义代码可以向 SharePoint 管理员保证,加载项既不会损坏服务器,也不会降低 SharePoint Online 网站的性能。Keeping custom code off SharePoint servers provides reassurance to SharePoint administrators that the add-in can't harm their servers or reduce the performance of their SharePoint Online websites.

  • SharePoint 外接程序中的业务逻辑可以通过 SharePoint 中包含的某些客户端 API 来访问 SharePoint 数据 。您的外接程序使用哪一个 API 取决于您做出的其他一些设计决策。Business logic in a SharePoint Add-in can access SharePoint data through one of the several client APIs included in SharePoint. Which API you use for your add-in depends on certain other design decisions you make.

  • 几乎所有主要类型的 SharePoint 组件都可作为 SharePoint 加载项的一部分,包括页面、列表、工作流、自定义内容类型、列表模板、Web 部件等。Almost all major types of SharePoint components can be part of a SharePoint Add-in, including pages, lists, workflows, custom content types, list templates, web parts, and more.

  • 其中安装了 SharePoint 加载项且用户可从中启动加载项的 SharePoint 网站称为“主机 Web”。The SharePoint websites where SharePoint Add-ins are installed, and from which users launch them, are called host webs. 不过,SharePoint 组件通常位于主机 Web 的特殊子 Web(称为“加载项 Web”)中。The SharePoint components, however, are generally in a special child web of the host web called the add-in web.

  • SharePoint 加载项可以通过下列多种方式安装到 SharePoint 网站SharePoint Add-ins can fit into a SharePoint website in several ways:

    沉浸式完整页面 SharePoint 应用程序体验 作为一种沉浸式完整页面体验 ,可以具有 SharePoint 页面的外观和感受。As an immersive full-page experience that can have the look and feel of a SharePoint page.
    SharePoint 应用的应用部件体验 作为网页的一部分,使用加载项部件这种特殊控件呈现As part of a webpage, using a special kind of control called an add-in part, to surface an
    包含加载项的 iframe 元素。iframe element that contains the add-in.
    SharePoint 应用的自定义操作体验 作为扩展列表、文档等的功能区和菜单的 UI 命令As UI commands that extend ribbons and menus for lists, documents, and more.
  • 用户安装的所有 SharePoint 加载项在 SharePoint 网站的“网站内容”页上都有对应的磁贴。All SharePoint Add-ins that users install get a tile on the Site Contents page of the SharePoint website. 单击磁贴即可运行加载项。Clicking the tile runs the add-in.

  • SharePoint 加载项是使用_加载项清单_进行配置。 加载项清单是一个 XML 文件,其中声明了加载项的基本属性、运行位置以及 SharePoint 应在加载项启动时执行的操作。A SharePoint Add-in is configured using an add-in manifest—an XML file that declares the add-in's basic properties, where it runs, and what SharePoint should do when the add-in starts. 此清单还可指定加载项支持的语言、加载项依赖的 SharePoint 服务和功能,以及加载项需要获取对主机 Web 的哪些权限等。Among other things, the manifest can specify what languages the add-in supports, what SharePoint services and functionality it depends on, and the permissions to the host web that the add-in needs. (SharePoint 加载项可完全控制自己的加载项 Web。)(SharePoint Add-ins have full control of their own add-in web.)

  • SharePoint 加载项是通过加载项包进行分发,其中始终至少包含加载项清单。You distribute SharePoint Add-ins in add-in packages that always include at least the add-in manifest. (如果没有任何 SharePoint 组件,加载项清单可能就是加载项包中的唯一内容。)如果加载项的加载项 Web 中有 SharePoint 组件,这些组件会以一组 XML 文件的形式包含在此包中。(If there are no SharePoint components, the add-in manifest may be the only thing in the add-in package.) If the add-in has SharePoint components in an add-in web, these are included in the package as a set of XML files. 此包中没有在 SharePoint 外部托管的远程组件(如远程 Web 应用或数据库),这些组件与加载项包分开部署。Remote components that are hosted outside of SharePoint, such as a remote web application or database, are not included in the package and are deployed separately from the add-in package. (不过,加载项清单确实会指定远程组件的 URL。)(However, the add-in manifest does specify the URLs of the remote components.)

  • 加载项包也可以包含 Office 加载项。 如果 SharePoint 加载项已安装,Office 加载项便会添加到 SharePoint 中的 Office 加载项目录。Add-in packages can also include Office Add-ins. When the SharePoint Add-in is installed, the Office Add-in is added to an Office Add-ins catalog in SharePoint. 用户可以通过目录将 Office 加载项安装到 Office 应用(如 Word 或 Excel)中。Users can install it from the catalog into Office applications such as Word or Excel.

提示

请查看 Office 应用商店中的一些 SharePoint 加载项,了解可以开发哪些内容。Take a look at a few SharePoint Add-ins in the Office Store to get a sense of what you can develop. 也可以打开 SharePoint 网站,并安装一些免费 SharePoint 加载项。只需依次转到“网站内容 | 添加加载项 | SharePoint 商店”**** 即可。Or open a SharePoint website and install some of the free SharePoint Add-ins. Just navigate to Site Contents | add an add-in | SharePoint Store.

对经验丰富的 SharePoint 开发人员的几点提示A few points for veteran SharePoint developers

我们已弃用包含自定义服务器端代码的沙盒解决方案,但仍支持“无代码”沙盒解决方案或仅包含 JavaScript 的沙盒解决方案。We've deprecated sandboxed solutions that contain custom server-side code. We still support "No code" sandboxed solutions and sandboxed solutions that contain only JavaScript.

SharePoint 加载项不使用服务器端 SharePoint 对象模型。SharePoint Add-ins don't use the server-side SharePoint object model. SharePoint 对客户端对象模型进行了大幅扩充。The client-side object models are greatly expanded in SharePoint. 虽然 SharePoint 服务器对象模型中的一些 API 不适用于客户端对象模型,但这些 API 几乎完全都是与安全相关的管理类。Although some APIs in the SharePoint server object model aren't available in the client object models, these are almost entirely administrative and security-related classes. 可应对这些情况的自定义 SharePoint 逻辑更适用于 Windows PowerShell 脚本或经典 SharePoint 场解决方案。Custom SharePoint logic that addresses these areas is more appropriate for a Windows PowerShell script or classic SharePoint farm solution. 若要了解如何选择 SharePoint 加载项、经典 SharePoint 场解决方案和沙盒解决方案,请参阅 SharePoint 加载项与 SharePoint 解决方案对比To learn how to choose among SharePoint Add-ins, classic SharePoint farm solutions, and sandboxed solutions, see SharePoint Add-ins compared with SharePoint solutions.

加载项包的两种分发或销售方法Two ways to distribute or sell an add-in package

加载项包可通过下列两种方式进行分发:You can distribute an add-in package in two ways:

  • 分发到组织加载项目录:这是 SharePoint Online 订阅或本地场中的专用 SharePoint 网站集。To an organization's add-in catalog, which is a dedicated SharePoint site collection in the SharePoint Online subscription or on-premises farm. 这种方法适用于专为特定组织定制的加载项。This method is used when the add-in is custom-made for a particular organization.

  • 分发到 Office 应用商店To the Office Store. 此应用商店负责市场营销环节(从发现到购买再到更新)。The store handles the marketing process for you, from discovery to purchase to updates. Microsoft 提供了“卖家面板”****,有助于通过 Office 应用商店销售加载项。Microsoft has a Seller Dashboard to help you sell add-ins through the Office Store.

在通过任一方法部署完外接程序后,就可在 SharePoint 网站的"添加外接程序"页面上提供以供安装。如果外接程序需要访问主机 Web 或其父订阅的权限,SharePoint 将提示要安装该外接程序的用户授予这些权限。After you deploy the add-in in either way, it's available for installation on the add an add-in page of SharePoint websites. If the add-in needs permissions to the host web or its parent subscription, SharePoint prompts the user who installs the add-in to grant those permissions.

当您需要更新加载项以修复缺陷或添加功能时,可在加载项清单中进行更改并提供加载项的版本号。然后将加载项包重新部署到商店或加载项目录中。用户会在 24 小时内在 SharePoint UI 中收到通知,提示有可用的更新。只要单击一次即可安装此更新。When you need to update an add-in to fix a bug or to add functionality, make the changes and raise the add-in's version number in the add-in manifest. Then redeploy the add-in package to the store or add-in catalog. Within 24 hours, users get a notice in the SharePoint UI that an update is available. They can install the update with a single click.

两种类型的 SharePoint 外接程序:SharePoint 托管和提供程序托管Two types of SharePoint Add-ins: SharePoint-hosted and provider-hosted

SharePoint 承载和提供程序承载的应用程序比较

SharePoint 托管的 SharePoint 外接程序SharePoint-hosted SharePoint Add-ins

SharePoint 托管加载项几乎完全由加载项 Web 中的 SharePoint 组件组成。这些组件有时被视为 SharePoint 的核心。SharePoint-hosted add-ins consist almost entirely of SharePoint components in an add-in web. They are sometimes said to have their center in SharePoint.

用户可以转到已安装加载项的 SharePoint 网站的“网站内容”**** 页,通过其上的磁贴来运行 SharePoint 托管加载项,这一点与所有 SharePoint 加载项一样。Like all SharePoint Add-ins, a user can run a SharePoint-hosted add-in from a tile on the Site Contents page of the SharePoint website to which it's installed. 它还可以根据需要在主机 Web 中添加另外两种 UI 组件:加载项部件和自定义操作(即自定义功能区按钮或菜单项)。Optionally, it can also have two other kinds of UI components in the host web: add-in parts and custom actions (that is, custom ribbon buttons or menu items). 除此之外,SharePoint 托管加载项中的其他所有内容都部署到加载项 Web。Everything else in a SharePoint-hosted add-in is deployed to the add-in web. 这些组件使用 XML 文件以声明方式进行定义,可以包含下列部分等:These components are defined declaratively using XML files, and they can include, among others:

自定义页面Custom pages 工作流Workflows 模块(文件集)Modules (sets of files)
列表模板List templates 列表和库实例List and library instances 自定义列表表单和视图Custom list forms and views
自定义内容类型Custom content types Web 模板Web templates 内置列(不是自定义列)Built-in columns (not custom columns)
内置 Web 部件(不是自定义 Web 部件)Built-in web parts (not custom web parts) JavaScript 文件JavaScript files 加载项 Web 的自定义按钮和菜单项Custom buttons and menu items for the add-in web

SharePoint 托管加载项中的所有业务逻辑都使用 JavaScript,无论是直接在自定义页面中,还是在通过自定义页面引用的 JavaScript 文件中。All business logic in a SharePoint-hosted add-in uses JavaScript, either directly on a custom page or in a JavaScript file that is referenced from a custom page. 使用提供的 JavaScript 版 SharePoint 对象模型 (JSOM),加载项可以对 SharePoint 数据轻松执行 CRUD(创建、读取、更新和删除)操作。A JavaScript version of the SharePoint object model (JSOM) is available to make it simple for the add-in to perform CRUD (create, read, update, and delete) operations on SharePoint data.

SharePoint 托管加载项中的自定义页面通常是 ASP.NET 页面 (ASPX),可以声明方式引用 ASP.NET 和现成 SharePoint 控件,而没有任何代码隐藏。Custom pages in a SharePoint-hosted add-in are generally ASP.NET pages (ASPX), and they can declaratively reference ASP.NET and in-the-box SharePoint controls, but there can be no code behind. 不过,可以使用客户端呈现选项和自定义 JavaScript 来自定义 SharePoint 控件。However, you can customize the SharePoint controls by using a client-side rendering option and custom JavaScript.

SharePoint 托管加载项中的 JavaScript 可以访问加载项 Web 外的数据和资源,具体是使用下列两种可安全处理浏览器的同源策略的技术之一:特殊 JavaScript 跨域库或特定 JavaScript WebProxy 类。The JavaScript in SharePoint-hosted add-ins can access data and resources that are outside of the add-in web by using either of two techniques for safely working around the browser's same origin policy: a special JavaScript cross-domain library or a specific JavaScript WebProxy class. 使用这些技术,SharePoint 托管加载项可以处理主机 Web 、父订阅或 Internet 上其他任何位置中的数据。Using these techniques, a SharePoint-hosted add-in can work with data on the host web, its parent subscription, or anywhere on the Internet.

提供商托管 SharePoint 加载项Provider-hosted SharePoint Add-ins

提供商托管加载项可以包含 SharePoint 托管加载项中的所有 SharePoint 组件。Any SharePoint component that can be in a SharePoint-hosted add-in can also be in a provider-hosted add-in. 不过,提供商托管加载项与 SharePoint 托管加载项有区别,因为前者至少包含一个在 SharePoint 场或 SharePoint Online 订阅外部托管的远程组件(如 Web 应用、服务或数据库)。But provider-hosted add-ins are distinguished from SharePoint-hosted add-ins because they include at least one remote component, such as a web application, service, or database, that is hosted externally from the SharePoint farm or SharePoint Online subscription. 这可以是与 SharePoint 场或云服务位于同一企业网络中的服务器。This could be a server in the same corporate network as a SharePoint farm or a cloud service. 外部组件可以托管在任何 Web 托管堆栈上,其中包括 Linux、Apache、MySQL 和 PHP (LAMP) 堆栈。The external components can be hosted on any web hosting stack, including the Linux, Apache, MySQL, and PHP (LAMP) stack.

备注

“提供商”是指拥有服务器或云帐户的任何人。The "provider" is whoever owns the server or cloud account. 它可以是拥有要安装加载项的 SharePoint 场或 SharePoint Online 租户的相同公司或组织。It can be the same company or organization that owns the SharePoint farm or SharePoint Online tenancy where the add-in is to be installed. 不过,加载项的开发人员也可以成为提供商。But the developer of the add-in can also be the provider. 通常,如果加载项是为一个组织而创建,此组织就负责托管。Typically, when an add-in is created for an organization, the organization provides the hosting. 不过,如果加载项是为多个组织而创建,更可能是由开发人员托管远程组件。However, when an add-in is created for multiple organizations, it's more likely that the developer hosts the remote components. 如果 SharePoint 加载项是通过 Office 应用商店进行市场营销,必须采用开发人员托管方式,因为开发人员没有加载项购买者的任何联系信息。Developer-hosting is mandatory if the SharePoint Add-in is being marketed through the Office Store, because the developer doesn't have any contact information for the add-in purchasers. 在这种情况下,加载项的各种实例都知道远程组件的 URI,因为加载项清单已对此进行指定。In this scenario, the various instances of the add-in know the URI of the remote component because it is specified in the add-in manifest.

可以完全灵活地对远程组件使用托管框架。You have complete flexibility for the hosting framework that you use for the remote components. 无需使用 Microsoft 堆栈。You don't have to use a Microsoft stack. 可以使用任何 Web 托管框架,包括 LAMP(Linux、Apache 服务器、MySQL、PHP)、MEAN(MongoDB、ExpressJS、AngularJS、Nodejs)、Java、Python 等。还可以使用非 Microsoft 开发工具。Any web hosting framework can be used, including LAMP (Linux, Apache server, MySQL, PHP), MEAN (MongoDB, ExpressJS, AngularJS, Nodejs), Java, Python, and others, and you are welcome to use non-Microsoft development tools. 此外,远程组件还可以托管在非 Microsoft 云服务中。In addition, the remote components can be hosted in non-Microsoft cloud services.

使用特殊的部件版式控制,可以为加载项中的远程页面指定 SharePoint 页面外观You can give remote pages in the add-in the look and feel of SharePoint pages by using a special chrome control.

远程数据可以是 Blob、缓存、消息队列、内容交付网络 (CDN) 和数据库 等。数据库可以是任何类型,包括面向关系和对象的类型。可通过多种方式访问远程数据。例如,您可以使用 Business Connectivity Services (BCS) 在 SharePoint 列表中呈现数据。另一种选择是,在远程 Web 应用程序的页面上的网格中公开数据。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.

SharePoint 外接程序 使用 SharePoint API 连接 SharePoint 功能(搜索、工作流、社交网络、分类、用户配置文件和 BCS 等)并与之进行集成。这样用户就可以阅读文档、进行搜索、与人员联系、执行 CRUD 操作等。提供多个 API 集: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. There is more than one set of the APIs:

  • 如果远程组件是使用 .NET 进行实现,可以使用托管代码 SharePoint 客户端对象模型 (CSOM) 库。When the remote components are implemented with .NET, the managed code SharePoint Client-Side Object Model (CSOM) library is available.

  • 对于不基于 .NET 的远程组件,可以使用一组 REST/OData API 访问 SharePoint 数据。For remote components that are not based on .NET, a set of REST/OData APIs can be used to access SharePoint data. 如果更希望使用 OData 接口,也可以通过 .NET 客户端使用它们。These can also be used from a .NET client if you prefer working with an OData interface.

  • 虽然前面提到 JSOM 库不能用于远程页面,但提供商托管加载项的加载项 Web 中可以包含自定义 SharePoint 页面,这些页面中的 JavaScript 能够使用 JSOM 库。The JSOM library, mentioned earlier, cannot be used on a remote page, but provider-hosted add-ins can have custom SharePoint pages in an add-in web, and JavaScript on these pages can use the JSOM library.

正如用户和组一样,能够访问 SharePoint 的提供程序托管的外接程序也是安全主体 。和用户一样,外接程序主体也必须经过身份验证和授权。外接程序需要具有对主机 Web 中的 SharePoint 数据执行操作的权限。在大多数方案中,用户通过 SharePoint 外接程序使用 SharePoint 的有效权限是用户权限与外接程序权限的交集,虽然在某些方案中用户可以使用某个外接程序执行原本并不具备执行权限的操作。Provider-hosted add-ins that access SharePoint are security principals just as users and groups are. The add-in principal has to be authenticated and authorized, just as the user does. The add-in needs permissions to perform operations on SharePoint data in the host web. In most scenarios, the effective permissions of a user working with SharePoint through a SharePoint Add-in are the intersection of the user's and the add-in's permissions, although there are some scenarios in which a user can do things with an add-in that she wouldn't otherwise have permission to do.

提供商托管加载项可以连接到任何内部或公共 Web 服务。与 SharePoint 托管加载项不同,提供商托管加载项可以处理 SharePoint 列表和列表项事件,如向文档库添加项。Provider-hosted add-ins can connect to any internal or public web service and, unlike SharePoint-hosted add-ins, they can handle SharePoint list and list item events, such as adding an item to a document library.

选择 SharePoint 开发路径Choose your SharePoint development path

准备好开始了吗?Ready to get started?

备注

如果是刚开始接触 SharePoint 和 Web 开发,请从 Microsoft Virtual Academy 提供的免费课程入手,或阅读关于 SharePoint 开发的书籍,这样可最大程度受益。If you're a beginner at both SharePoint and web development, you'd benefit most by starting with the free course at Microsoft's Virtual Academy, or working through a book about SharePoint development.

另请参阅See also