SharePoint 开发和设计工具及做法

本文提供了有关在 SharePoint 中可用的开发和设计选项的信息。 您还可以找到有关如何使用远程配置模式将品牌资产应用到 SharePoint 网站的信息。

注意

本文中的代码按原样提供,不提供任何明示或暗示的担保,包括对特定用途适用性、适销性或不侵权的默示担保。

条款和概念

表 1. 关键的 SharePoint 开发和设计术语和概念

术语或概念 定义 详细信息
设计管理器 在启用了发布功能的 SharePoint 发布网站或团队网站中激活的一个功能,可用于导入和管理网站品牌资产并将它们导出到一个设计包中。 使用设计管理器将在其他工具(如 Adobe PhotoShop 或 Adobe DreamWeaver)中创建的品牌资产导入到 SharePoint 中。

未启用发布时,SharePoint Designer 不可用于 OneDrive for Business 或 SharePoint 团队网站。
设计包 专为使用 SharePoint 发布网站而设计,包含存储在设计管理器中的品牌资产。 SharePoint 复合功能手册
远程预配 在提供程序托管的外接程序中,通过使用运行在 SharePoint 之外的模板和代码来预配网站的模型。 - SharePoint 2013 中的网站预配技术和远程预配
- 使用 SharePoint 2013 的相关应用预配自助服务网站
根网站 网站集内的第一个网站。 根网站有时也称为"Web 应用程序根"。
沙盒解决方案 包含程序集、其他非编译组件和 XML 清单文件的 .wsp 文件。 沙盒解决方案使用部分信任代码。 沙盒解决方案
SharePoint Designer 用于管理 SharePoint 中的品牌要素的 HTML 设计器和设计资产管理工具。 SharePoint Designer 主要支持自定义工作流。 - SharePoint 网站开发的新增内容
.wsp 文件 SharePoint 的解决方案文件。 .wsp 是一个可对网站资产进行分类并通过 manifest.xml 文件进行整理的 .cab 文件。 解决方案概述

开发选项

使用 SharePoint 作为开发平台时,你需要创建一个环境,以开发、测试、生成和部署你的内容。 有关开发选项的信息,请参阅 SharePoint 工作流对象模型应用程序生命周期管理 (ALM) API

表 2. SharePoint 开发、测试和验收相关选项

选项 注意事项
Team Foundation Server - 位于 Visual Studio Team Services 上以方便访问。
- 包括集中的源代码和生命周期管理系统。
云测试和验收环境 - 使用单独的租户进行验收测试。
- 用于本地测试的单独测试环境。
本地测试和验收环境 - 用于本地 SharePoint 部署。
- 由本地或 Microsoft Azure 中的客户托管。

在大多数情况下,虽然可以根据你的要求有所不同,但你至少需要以下租户:

  • 开发人员租户。 最佳做法是设置和使用自己的开发人员网站。 这样可以避免将数据和生产环境混淆。 若要注册和预配开发人员网站,请参阅在 Office 365 上设置 SharePoint 外接程序的开发环境

  • 集成/测试租户。 使用此网站可确保新的应用和功能跨多个网站集使用,并用于生产环境中的服务和数据。 将环境配置为包括预览版中的功能。 (为此,请在租户管理员控制台中,选择“服务设置”,然后在“更新”设置下选择“初版”。)你可以使用 Visual Studio Team Services 运行自动化测试和任何其他连续的集成测试。

  • 生产租户。 向此租户发布已测试、验收和批准的应用。 可在此租户上创建一个开发人员网站,以开发和测试影响范围较小或产生单独影响的应用。 一般情况下,请避免将开发环境和生产环境混淆。

设计工具

使用标准 Web 设计和开发工具(例如 HTML、图像、CSS 文件和 JavaScript 文件)创建 SharePoint 网站品牌资产。 例如,可以使用 Adobe DreamWeaver 和 Adobe PhotoShop 设计可以用于塑造 SharePoint 网站品牌的 HTML、CSS、JavaScript 和图像文件。 或者,也可以使用 SharePoint Designer 创建、管理和自定义品牌资产或在 Visual Studio 中创建自定义解决方案。

SharePoint 设计包和 .wsp 文件

设计包是由设计管理器创建的并遵循打包设计资产可预测规则的 .wsp 文件。 它们实质上是沙盒解决方案。 如果您使用另一种工具将品牌资产打包在 .wsp 文件中,您的品牌资产将处于不固定但可预测的状态。

该设计包中包括所有经过自定义的文件。 例如,如果您创建一个使用自定义内容类型的网页布局,设计包中包括页面布局、它所使用的自定义内容类型,以及所有自定义网站栏。 该设计包中还包括一些与已应用到您的 SharePoint 网站的各种组合外观相关的文件,包括上传到以下位置的文件:

  • 网站资产库
  • 样式库
  • 母版页样式库

如果您在应用自定义品牌之前,已对网站应用了组合外观,则设计包中将包括带有 .themedcss 和 .themedpng 文件扩展名的文件。 若要将设计包中的品牌资产应用到 SharePoint 网站,请导出该设计包,并使用远程配置模式应用设计包中的内容。

SharePoint 包含可与设计包一起使用的 API。 如果您在使用 SSOM、CSOM 或 JSOM,则您可以使用 DesignPackage 类或 DesignPackageInfo 类。

使用设计包 CSOM 将设计软件包的内容应用到 SharePoint 网站

以下示例演示了如何在远程配置模式下使用设计包 API 将设计包中的内容应用到 SharePoint 网站。

此代码是专为用于发布网站而设计。 虽然可以使用设计包 API 将品牌塑造应用到已启用发布功能的团队网站,但是这可能会带来长期支持问题。

using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Client.Publishing;
namespace ProviderSharePointAppWeb
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_PreInit(object sender, EventArgs e)
        {
            Uri redirectUrl;
            switch (SharePointContextProvider.CheckRedirectionStatus(Context, out redirectUrl))
            {
                case RedirectionStatus.Ok:
                    return;
                case RedirectionStatus.ShouldRedirect:
                    Response.Redirect(redirectUrl.AbsoluteUri, endResponse: true);
                    break;
                case RedirectionStatus.CanNotRedirect:
                    Response.Write("An error occurred while processing your request.");
                    Response.End();
                    break;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            // Use TokenHelper to get the client context and Title property.
            // To access other properties, the add-in might need to request permissions
            // on the host web.
            var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);
            
            // Publishing feature GUID to use the infrastructure for publishing. 
            Guid PublishingFeature = Guid.Parse("f6924d36-2fa8-4f0b-b16d-06b7250180fa");

            // The site-relative URL of the design package to install.
            // This sandbox design package should be uploaded to a document library.
            // For practical purposes, this can be a configuration setting in web.config.
            string fileRelativePath = @"/sites/devsite/brand/Dev.wsp";

            //string fileUrl = @"https://SPXXXXX.com/sites/devsite/brand/Dev.wsp";
            
        
            using (var clientContext = spContext.CreateUserClientContextForSPHost())
            {
                // Load the site context explicitly or while installing the API, the path for
// the package will not be resolved.
                // If the package cannot be found, an exception is thrown. 
                var site = clientContext.Site;
                clientContext.Load(site);
                clientContext.ExecuteQuery();
              
                // Validate whether the Publishing feature is active. 
                if (IsSiteFeatureActivated(clientContext,PublishingFeature))
                {
                    DesignPackageInfo info = new DesignPackageInfo()
                    {
                        PackageGuid = Guid.Empty,
                        MajorVersion = 1,
                        MinorVersion = 1,
                        PackageName = "Dev"
                    };
                    Console.WriteLine("Installing design package ");
                    
                    DesignPackage.Install(clientContext, clientContext.Site, info, fileRelativePath);
                    clientContext.ExecuteQuery();

                    Console.WriteLine("Applying design package");
                    DesignPackage.Apply(clientContext, clientContext.Site, info);
                    clientContext.ExecuteQuery();
                }
            }
        }
        public  bool IsSiteFeatureActivated( ClientContext context, Guid guid)
        {
            var features = context.Site.Features;
            context.Load(features);
            context.ExecuteQuery();

            foreach (var f in features)
            {
                if (f.DefinitionId.Equals(guid))
                    return true;
            }
            return false;
        }
 
    }
}

使用 FileCreationInformation 将品牌资产和母版页上传到团队网站

你可以使用 SharePoint CSOM 功能来安装和卸载设计包,并将设计包导出到 SharePoint Online 网站。

例如,使用 SP.Publishing.DesignPackage.install 方法 (sp.publishing)DesignPackage.Install 方法在网站上安装设计包,如下例中所示。

public static void Install(
    	ClientRuntimeContext context,
	    Site site,
	    DesignPackageInfo info,
    	string path
)

DesignPackageInfo 类指定描述要安装的设计包内容的元数据。 使用 卸载方法卸载网站中的设计包,如以下示例所示。

public static void UnInstall(
	    ClientRuntimeContext context,
	    Site site,
    	DesignPackageInfo info
)

如果你需要为启用了发布功能的团队网站或者 SharePoint Online 上的发布网站进行品牌塑造,则可以使用 ExportEnterpriseExportSmallBusiness 方法将网站模板的设计包导出到解决方案库。 将 ExportSmallBusiness 方法用于小型企业网站模板,并将 ExportEnterprise 方法用于其他所有网站模板,如下例中所示。

public static ClientResult<DesignPackageInfo> ExportEnterprise(
	    ClientRuntimeContext context,
	    Site site,
	    bool includeSearchConfiguration
)

当你使用 ExportSmallBusiness 方法时,可在设计包中包括搜索配置,如下例中所示。 请注意,所有设计包方法在网站集级别上运行。 设计包的名称以字符串 packageName 表示。

public static ClientResult<DesignPackageInfo> ExportSmallBusiness(
	    ClientRuntimeContext context,
	    Site site,
	    string packageName,
	    bool includeSearchConfiguration
)

SharePoint Online 的设计工具选项

可用于品牌化 SharePoint Online 网站的工具取决于你的 SharePoint Online 版本和想要构建的网站类型。 例如,小型企业版包括一个团队网站和一个公共网站。 它不包括发布网站。 你可以在 SharePoint Online 中使用 Site Builder 外接程序自定义公共网站品牌。

企业版在根 Web 应用程序中包括一个团队网站集,适用于不包括发布的域。 它不包括创建公共网站。 在 SharePoint Online 企业版中的发布网站上,使用设计管理器来管理 SharePoint 网站品牌元素。

另请参阅