分享方式:


範例:匯入檔案為 Web 資源

 

發佈日期: 2017年1月

適用對象: Dynamics 365 (online)、Dynamics 365 (on-premises)、Dynamics CRM 2016、Dynamics CRM Online

當您開發了大量檔案用於 Web 資源,您可以透過此應用程式手動儲存這些成果。 許多 Web 資源可以在 Microsoft Dynamics 365 (線上和內部部署) 外部開發和測試,然後匯入。

此範例提供簡化版的程序範例。 如需提供 WPF 套用您可以使用匯入 Web 資源的複雜範例,請參閱 範例:Web 資源公用程式

這個範例程式碼適用於 Microsoft Dynamics CRM 2015 和 Microsoft Dynamics CRM Online 2015 更新。下載 Microsoft Dynamics CRM SDK 套件。 可以在下列位置的下載套件中找到:

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

需求

如需執行此 SDK 所提供範例程式碼的需求資訊,請參閱使用範例和 Helper 程式碼

在 SDK 下載套件包含範例程式碼,包含此範例需要下列檔案:

  • ImportJob.xml
    此檔案提供關於要建立的 Web 資源記錄的資料。 對於每個檔案,它包含下列資料:

    • path: 每個檔案的資料夾路徑為 FilesToImport。

    • displayName: 顯示 Web 資源的名稱。

    • descriptioin: 每個檔案的描述。

    • name: 為 Web 資源使用的名稱。

      備註

      • 這些名稱都是底線字元為首。 解決方案發行者自訂首碼會加到 web 資源名稱的前面。 而非將號碼特定自訂首碼,此範例會偵測已經存在於組織的發行者記錄的目前自訂首碼。

      • 因為這些檔案都已在 Microsoft Dynamics 365 外部開發,並使用相對路徑之間的名稱,包括連線斜「/」字元建立真正資料夾結構,讓相關的連結會繼續在 Microsoft Dynamics 365 運作。

    • type: 指定 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
    此檔案包含包含 Data.xml 檔案和 Transform.xslt 檔案的網站的相對資訊的 JScript 程式庫。 它也包含轉換資料以及新增至 ShowData.htm 頁面的 showData 函數。

  • FilesToImport/XSL/Transform.xslt
    此檔案包含如何轉換資料為 HTML 資料表的 XSL 定義。

示範

建立在解決方案中的 Web 資源

Web 資源是組織擁有的記錄,所以可以使用 IOrganizationService.Create 方法建立,或使用 CreateRequest 訊息和 IOrganizationService 建立。Execute 方法。 此範例顯示如何在建立此項目時,使用 SolutionUniqueName 選項參數,以特定的解決方案關聯 Web 資源特定解決方案。 必需使用 CreateRequest 訊息。

從磁碟上傳檔案

WebResource.Content 屬性需要代表檔案二進位內容的 Base64 字串。 下列範例是使用這個函數將該檔案轉換成必要類型。


//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 資源了。 當更新時,就需要發行。

範例

檔案 ImportWebResources.cs 中的下列區段需要下列變數:

  • _customizationPrefix:Microsoft Dynamics 365 SDK 範例發行者的自訂首碼。 如果此發行者不存在,會以「範例」為自訂首碼建立。

  • _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 資源公用程式
WebResource 實體訊息和方法
Microsoft Dynamics 365 的 Web 資源

Microsoft Dynamics 365

© 2017 Microsoft. 著作權所有,並保留一切權利。 著作權