開始使用 Project Server 2013 JavaScript 物件模型

在 Project Server 2013 中,JavaScript 物件模型可用於 Project Online、行動裝置及本地開發。 本主題簡要介紹 JavaScript 物件模型,並說明如何利用 JavaScript 物件模型建立一個應用程式頁面,以檢索並遍歷專案。

使用 Project Server JavaScript 物件模型

使用 JavaScript 物件模型是建立一個能執行客戶端 (應用程式的好方法,而非需要遠端) 的受管理客戶端程式碼。 應用程式可以使用 JavaScript 物件模型,透過向伺服器發送非同步呼叫來擷取或變更物件。 使用 JavaScript 物件模型的應用程式通常會部署為自訂的 SharePoint 外掛、應用程式頁面及網頁部分。 欲了解更多資訊,請參閱 SharePoint 2013 中的 Host webs、add-in webs 及 SharePoint 元件中的「SharePoint 應用程式中可存在的 SharePoint 元件類型」。

JavaScript 物件模型實作了 Project Server 2013 的主要功能,但 JavaScript 物件模型與伺服器物件模型並非一對一的奇偶校驗。 JavaScript 物件模型的入口是 ProjectContext 物件,代表 Project Server 2013 的用戶端上下文,並提供伺服器內容與功能的存取。 Project Server 2013 的 JavaScript 物件模型定義於 PS.js 檔案中,該檔案位於應用伺服器的預設路徑 %ProgramFiles%\Common Files\Microsoft Shared\Web Server Extensions\15\TEMPLATE\LAYOUTS 中。 Project Server 2013 也會在同一位置安裝 PS.Debug.js 檔案。 PS.Debug.js 是未壓縮版的 PS.js,提供 IntelliSense 資訊。

JavaScript 物件模型允許 Forms 認證,所有請求皆以目前使用者身份驗證。 欲了解更多關於安全及設計自訂應用程式與解決方案的其他考量,請參閱 《SharePoint 2013 中的認證、授權與安全性》、《 SharePoint 外掛架構與開發環境的重要面向》以及 SharePoint 外掛與 SharePoint 解決方案的比較。

注意事項

若要遠端存取 SharePoint 網站的資料,SharePoint 2013 提供跨域函式庫,讓你能進行用戶端跨域呼叫。 欲了解更多資訊,請參閱 使用跨域函式庫存取 SharePoint 2013 資料。

許多使用 Project Server 2013 JavaScript 物件模型的概念與流程與相關用戶端物件模型相似。 欲了解更多關於 Project Server 2013 受管理客戶端物件模型的資訊,請參閱 Microsoft.ProjectServer.Client。 欲了解更多關於 SharePoint 2013 JavaScript 物件模型及受管理用戶端物件模型的資訊,請參閱 SharePoint 2013 中的 JavaScript 函式庫程式碼完成基本操作 及 使用 SharePoint 2013 用戶端函式庫程式碼完成基本操作。

攻略:建立一個應用程式頁面,用來檢索並反覆執行專案

學習如何建立一個應用程式頁面,顯示每個已發佈專案的 ID、名稱及創建日期。

建立應用程式頁面以擷取並反覆執行專案的前提條件

要開發本主題中描述的應用程式頁面,您必須安裝並設定以下產品:

  • SharePoint Server 2013
  • Project Server 2013,至少有一個已發佈的專案
  • Visual Studio 2012
  • Visual Studio 2012 Office 開發人員工具

你也必須擁有將擴充功能部署到 SharePoint Server 2013 以及取得專案的權限。

注意事項

這些說明假設你是在執行 Project Server 2013 的電腦上開發。

在 Visual Studio 2012 中建立應用程式頁面

以下步驟會建立一個 SharePoint 專案及包含表格與標籤的應用程式頁面。 表格顯示專案資訊,標籤則顯示錯誤訊息。

  1. 在執行 Project Server 2013 的電腦上,以管理員身份執行 Visual Studio 2012。

  2. 建立一個空的 SharePoint 2013 專案,如下:

    1. 在新專案對話框中,從對話框頂端的下拉選單選擇 .NET Framework 4.5。

    2. 在範本分類清單中,選擇 Office SharePoint 類別,然後選擇 SharePoint 2013 專案 範本。

    3. 將專案命名為 GetProjectsJSOM,然後選擇 確定 按鈕。

    4. 在 SharePoint 自訂精靈 對話框中,選擇部署 為農場解決方案,然後選擇 確定 按鈕。

  3. 在 方案總管 中,編輯 ProjectsJSOM 專案的 Site URL 屬性值,使其與 Project Web App (https://ServerName/PWA 實例的 URL 相符,例如) 。

  4. 打開 GetProjectsJSOM 專案的捷徑選單,然後新增一個 SharePoint 的「Layouts」映射資料夾。

  5. 在 Layouts 資料夾中,打開 GetProjectsJSOM 資料夾的捷徑選單,然後新增一個名為 ProjectsList.aspx 的 SharePoint 應用程式頁面。

    注意事項

    此範例未使用 Visual Studio 2012 為應用程式頁面建立的程式碼後置檔案。

  6. 打開 ProjectsList.aspx 頁面的捷徑選單,選擇 設定為啟動項目。

  7. 在 ProjectsList.aspx 頁的標記中,請在「Main」 asp:Content 標籤內定義表格和訊息佔位符,如下。

    <table width="100%" id="tblProjects">
        <tr id="headerRow">
            <th width="25%" align="left">Project name</th>
            <th width="25%" align="left">Created date</th>
            <th width="50%" align="left">Project ID</th>
        </tr>
    </table>
    <div id="divMessage">
                <span id="spanMessage" style="color: #FF0000;"></span>
    </div>
    

取回專案收藏

以下步驟可取回並初始化專案集合。

  1. 關閉 div 標籤後,新增一個 SharePoint:ScriptLink 標籤,參考 PS.js 檔案,如下。

     <SharePoint:ScriptLink name="PS.js" runat="server" ondemand="false" localizable="false" loadafterui="true" />
    
  2. 在 SharePoint:ScriptLink 標籤下方,請新增 腳本 標籤,如下所示。

     <script type="text/javascript">
         // Paste the remaining code snippets here, in the order that they are presented.
     </script>
    
  3. 宣告一個全域變數來儲存專案集合,如下。

    var projects;
    
  4. 打電話給 警司。SOD.executeOrDelayUntilScriptLoaded 方法,確保 PS.js 檔案在自訂程式碼執行前已經載入。

    SP.SOD.executeOrDelayUntilScriptLoaded(GetProjects, "PS.js");
    
  5. 新增一個函式,連接當前上下文並檢索專案集合,如下所示。

     function GetProjects() {
         // Initialize the current client context.
         var projContext = PS.ProjectContext.get_current();
         // Get the projects collection.
         projects = projContext.get_projects();
         // Register the request that you want to run on the server.
         // This call includes an optional "Include" parameter to request only the
         // Name, CreatedDate, and Id properties of the projects in the collection.
         projContext.load(projects, 'Include(Name, CreatedDate, Id)');
         // Run the request on the server.
         projContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);
     }
    

    透過上下文擷取的某些客戶端物件在初始化前不包含資料;也就是說,在物件初始化之前,你無法存取物件的屬性值。 此外,對於 ValueObject 類型的屬性,您必須明確請求該屬性,才能存取其值。 (如果你在屬性尚未初始化前嘗試存取,你會收到 一個 PropertyOrFieldNotInitializedException 例外 )

    要初始化物件,你要呼叫 load method (或 loadQuery 方法) ,然後呼叫 executeQueryAsync 方法。

    • 呼叫 load 函式或 loadQuery 函式會註冊一個你想在伺服器上執行的請求。 你輸入一個參數,代表你想要取得的物件。 如果你使用 ValueObject 屬性,那麼你也會在參數中請求該屬性。

    • 呼叫 executeQueryAsync 函式會將查詢請求非同步傳送到伺服器。 你在收到伺服器回應時,會輸入成功回調函式和失敗回調函式。

    為了減少網路流量,呼叫 載入 函式時,請只請求你想處理的屬性。 此外,如果你處理多個物件,請在呼叫 executeQueryAsync 函式之前,先將多次呼叫 load 函式。

反覆檢視專案收藏

以下步驟會遍歷專案集合 (伺服器呼叫是否成功) 或若呼叫失敗 () 則顯示錯誤訊息。

  1. 新增一個成功回調函式,會遍歷專案集合,具體如下。

     function onQuerySucceeded(sender, args) {
         // Get the enumerator and iterate through the collection.
         var projectEnumerator = projects.getEnumerator();
         while (projectEnumerator.moveNext()) {
             var project = projectEnumerator.get_current();
             // Create the row for the project's information.
             var row = tblProjects.insertRow();
             // Insert cells for the Id, Name, and CreatedDate properties.
             row.insertCell().innerText = project.get_name();
             row.insertCell().innerText = project.get_createdDate();
             row.insertCell().innerText = project.get_id();
         }
     }
    
  2. 新增一個失敗回調函式,會呈現錯誤訊息,如下所示。

    function onQueryFailed(sender, args) {
        $get("spanMessage").innerText = 'Request failed: ' + args.get_message();
    }
    
  3. 要測試應用程式頁面,在選單列選擇 除錯, 開始除錯。 如果系統提示你修改 web.config 檔案,請選擇 確定。

完整程式碼範例

以下是ProjectsList.aspx檔案的完整程式碼。

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ProjectsList.aspx.cs" Inherits="GetProjectsJSOM.Layouts.GetProjectsJSOM.ProjectsList" DynamicMasterPageFile="~masterurl/default.master" %>
<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
</asp:Content>
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
    <table width="100%" id="tblProjects">
    <tr id="headerRow">
        <th width="25%" align="left">Project name</th>
        <th width="25%" align="left">Created date</th>
        <th width="50%" align="left">Project ID</th>
    </tr>
</table>
<div id="divMessage">
        <span id="spanMessage" style="color: #FF0000;"></span>
</div>
<SharePoint:ScriptLink name="PS.js" runat="server" ondemand="false" localizable="false" loadafterui="true" />
<script type="text/javascript">
    // Declare a variable to store the published projects that exist
    // in the current context.
    var projects;
    // Ensure that the PS.js file is loaded before your custom code runs.
    SP.SOD.executeOrDelayUntilScriptLoaded(GetProjects, "PS.js");
    // Get the projects collection.
    function GetProjects() {
        // Initialize the current client context.
        var projContext = PS.ProjectContext.get_current();
        // Get the projects collection.
        projects = projContext.get_projects();
        // Register the request that you want to run on the server.
        // This call includes an optional "Include" parameter to request only the
        // Name, CreatedDate, and Id properties of the projects in the collection.
        projContext.load(projects, 'Include(Name, CreatedDate, Id)');
        // Run the request on the server.
        projContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);
    }
    function onQuerySucceeded(sender, args) {
        // Get the enumerator and iterate through the collection.
        var projectEnumerator = projects.getEnumerator();
        while (projectEnumerator.moveNext()) {
            var project = projectEnumerator.get_current();
            // Create the row for the project's information.
            var row = tblProjects.insertRow();
            // Insert cells for the Id, Name, and CreatedDate properties.
            row.insertCell().innerText = project.get_name();
            row.insertCell().innerText = project.get_createdDate();
            row.insertCell().innerText = project.get_id();
        }
    }
    function onQueryFailed(sender, args) {
        $get("spanMessage").innerText = 'Request failed: ' + args.get_message();
    }
</script>
</asp:Content>
<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
Application Page
</asp:Content>
<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >
My Application Page
</asp:Content>

另請參閱