示例:将文件作为 Web 资源导入

 

发布日期: 2017年1月

适用于: Dynamics 365 (online),Dynamics 365 (on-premises),Dynamics CRM 2016,Dynamics CRM Online

在开发大量要用作 Web 资源的文件时,可通过该应用程序节省手动添加这些资源的工作。 可以在 Microsoft Dynamics 365(在线或本地) 之外开发和测试许多 Web 资源,然后导入这些资源。

此示例提供该过程的简化形式。 有关提供可用于导入 Web 资源的 WPF 应用程序的更复杂示例,请参阅示例:Web 资源实用工具

此示例代码适用于 Microsoft Dynamics CRM 2015 和 Microsoft Dynamics CRM Online 2015 更新。下载 Microsoft Dynamics CRM SDK 包。 它可能位于下载包的以下位置:

SDK\SampleCode\CS\Client\WebResources\ImportWebResources\

要求

有关运行此 SDK 中提供的示例代码的要求的详细信息,请参阅使用示例和帮助程序代码

SDK 下载包中包含的示例代码包括此示例所需的以下文件:

  • ImportJob.xml
    该文件提供有关将创建的 Web 资源记录的数据。 对于每个文件,它都包含以下数据:

    • 路径: FilesToImport 文件夹中每个文件的路径。

    • displayName: Web 资源的显示名称。

    • 说明: 每个文件用途的说明。

    • 名称: 将用于 Web 资源的名称。

      备注

      • 其中每个名称都以下划线字符开头。 在创建 Web 资源时,解决方案发布商的自定义前缀将附加到该名称的前面。 此示例将检测组织中可能已存在的发布商记录的当前自定义前缀,而不是对特定自定义前缀进行硬编码。

      • 因为其中每个文件都是在 Microsoft Dynamics 365 之外开发的,且依赖于相对路径才能相互访问,所以名称中包括反斜线“/”字符来创建虚拟文件夹结构,以便相对链接在 Microsoft Dynamics 365 中将继续有效。

    • 类型: 指定要使用 web资源类型中的整数值创建的 Web 资源的类型。

  • FilesToImport/ShowData.htm
    此 HTML Web 资源需要其他每个文件都显示下表。

    Apurva

    Dalia

    Ofer

    Daliot

    Jim

    Daly

    Ryan

    Danner

    Mike

    Danseglio

    Alex

    Darrow

  • FilesToImport/CSS/Styles.css
    该文件提供 ShowData.htm 中使用的 CSS 样式。

  • FilesToImport/Data/Data.xml
    该文件包含显示在表中的名称列表。

  • FilesToImport/Script/Script.js
    该文件包含一个 JScript 库,其中包含有关 Data.xml 文件和 Transform.xslt 文件的相对位置的信息。 它还包含 showData 函数,用于转换数据并将其添加到 ShowData.htm 页中。

  • FilesToImport/XSL/Transform.xslt
    该文件包含有关如何将数据转换为 HTML 表的 XSL 定义。

演示

在解决方案上下文中创建 Web 资源

Web 资源是组织负责的记录,因此可使用 IOrganizationService.Create 方法或使用 CreateRequest 消息和 IOrganizationServiceExecute 方法结合使用。 此示例将显示如何使用 SolutionUniqueName 可选参数在创建 Web 资源时将其与特定解决方案关联。 这需要使用 CreateRequest 消息。

从磁盘上载文件

WebResource.Content 属性需要一个表示文件的二进制内容的 Base 64 字符串。 以下示例是用于将文件转换为所需类型的方法。


//Encodes the Web Resource File
static public string getEncodedFileContents(String pathToFile)
{
    FileStream fs = new FileStream(pathToFile, FileMode.Open, FileAccess.Read);
    byte[] binaryData = new byte[fs.Length];
    long bytesRead = fs.Read(binaryData, 0, (int)fs.Length);
    fs.Close();
    return System.Convert.ToBase64String(binaryData, 0, binaryData.Length);
}

将 Web 资源记录数据与文件数据结合使用

ImportJob.xml 文件演示如何结合使用有关所导入的文件的数据和有关要创建的 Web 资源的数据。 特别是,为使相关文件之间的相对链接仍然有效,所创建的 Web 资源的名称必须通过在文件名中使用模拟目录中保留有关磁盘上文件的相对位置的信息。 由于数据存储在 ImportJob.xml 文件中,因此将在公用虚拟文件夹下创建所有这些相关 Web 资源文件。

备注

在创建 Web 资源后,不必发布这些资源。 在更新 Web 资源后,则需要发布它们。

示例

ImportWebResources.cs 文件的以下部分需要下列变量:

  • _customizationPrefix:Microsoft Dynamics 365 SDK 示例 发布商的自定义前缀。 如果该发布商不存在,将使用自定义前缀“sample”创建它。

  • _ImportWebResourcesSolutionUniqueName:此示例中创建的导入 Web 资源示例解决方案的唯一名称。 值为 ImportWebResourcesSample。



//Read the descriptive data from the XML file
XDocument xmlDoc = XDocument.Load("../../ImportJob.xml");

//Create a collection of anonymous type references to each of the Web Resources
var webResources = from webResource in xmlDoc.Descendants("webResource")
                   select new
                   {
                       path = webResource.Element("path").Value,
                       displayName = webResource.Element("displayName").Value,
                       description = webResource.Element("description").Value,
                       name = webResource.Element("name").Value,
                       type = webResource.Element("type").Value
                   };

// Loop through the collection creating Web Resources
int counter = 0;
foreach (var webResource in webResources)
{
    //Set the Web Resource properties
    WebResource wr = new WebResource
    {
        Content = getEncodedFileContents(@"../../" + webResource.path),
        DisplayName = webResource.displayName,
        Description = webResource.description,
        Name = _customizationPrefix + webResource.name,
        LogicalName = WebResource.EntityLogicalName,
        WebResourceType = new OptionSetValue(Int32.Parse(webResource.type))
    };

    // Using CreateRequest because we want to add an optional parameter
    CreateRequest cr = new CreateRequest
    {
        Target = wr
    };
    //Set the SolutionUniqueName optional parameter so the Web Resources will be
    // created in the context of a specific solution.
    cr.Parameters.Add("SolutionUniqueName", _ImportWebResourcesSolutionUniqueName);

    CreateResponse cresp = (CreateResponse)_serviceProxy.Execute(cr);
    // Capture the id values for the Web Resources so the sample can delete them.
    _webResourceIds[counter] = cresp.id;
    counter++;
    Console.WriteLine("Created Web Resource: {0}", webResource.displayName);
}

在创建 Web 资源后,不必发布这些资源。 在更新 Web 资源后,则需要发布它们。

另请参阅

示例:Web 资源实用工具
WebResource 实体消息和方法
Microsoft Dynamics 365 的 Web 资源

Microsoft Dynamics 365

© 2017 Microsoft。 保留所有权利。 版权