本文中的情境展示了如何取得目前的 ProjectContext 實例;檢索並迭代伺服器上已發表專案的集合;透過使用 Project Server JavaScript 物件模型建立、檢索、結出及刪除專案;並改變專案的屬性。
注意事項
這些情境會在 SharePoint 應用程式頁面的標記中定義自訂程式碼,但不會使用 Visual Studio 2012 為該頁面建立的背後程式碼檔案。
在 JavaScript 物件模型中使用 Project Server 2013 專案的前提條件
要執行本文所述情境,您必須安裝並設定以下產品:
- SharePoint Server 2013
- Project Server 2013
- Visual Studio 2012
- Visual Studio 2012 Office 開發人員工具
你也必須擁有將擴充功能部署到 SharePoint Server 2013 以及貢獻專案的權限。
注意事項
這些說明假設你是在執行 Project Server 2013 的電腦上開發。
建立 Visual Studio 解決方案
以下步驟可建立包含 SharePoint 專案與應用程式頁面的 Visual Studio 2012 解決方案。 該頁面包含了與專案合作的邏輯。
在 Visual Studio 建立 SharePoint 專案
在執行 Project Server 2013 的電腦上,以管理員身份執行 Visual Studio 2012。
在選單列中,選擇 檔案、 新建、 專案。
在新專案對話框中,從對話框頂端的下拉選單選擇 .NET Framework 4.5。
在 Office/SharePoint 範本類別中,選擇 SharePoint 解決方案,然後選擇 SharePoint 2013 專案 範本。
將專案命名為 ProjectsJSOM,然後選擇 確定 按鈕。
在 SharePoint 自訂精靈 對話框中,選擇部署 為農場解決方案,然後選擇 完成 按鈕。
將 ProjectsJSOM 專案的 Site URL 屬性值與 Project Web App 實例的 URL 相符,例如 (
https://ServerName/PWA) 。
在 Visual Studio 建立應用程式頁面
在解決方案方案總管中,打開 ProjectsJSOM 專案的捷徑選單,然後新增一個 SharePoint 的「Layouts」映射資料夾。
在 Layouts 資料夾中,打開 ProjectsJSOM 資料夾的捷徑選單,然後新增一個名為 ProjectsList.aspx 的新 SharePoint 應用程式頁面。
打開 ProjectsList.aspx 頁面的捷徑選單,選擇 設定為啟動項目。
在 ProjectsList.aspx 頁的標記中,請在「Main」 asp:Content 標籤中定義使用者介面控制項,如下。
<table width="100%" id="tblProjects"> <tr id="headerRow"> <th width="25%" align="left">Name</th> <th width="25%" align="left">Description</th> <th width="25%" align="left">Start Date</th> <th width="25%" align="left">ID</th> </tr> </table> <textarea id="txtGuid" rows="1" cols="35">Paste the project GUID here.</textarea> <button id="btnSend" type="button"></button><br /> <span id="spanMessage" style="color: #FF0000;"></span>注意事項
這些控制措施並非在所有情況下都能使用。 例如,「建立專案」情境並未使用 文字區 和 按鈕 控制。
在結束 時段 標籤後,依序新增 SharePoint:ScriptLink 標籤、 SharePoint:FormDigest 標籤及 腳本 標籤,如下所示。
<SharePoint:ScriptLink id="ScriptLink" name="PS.js" runat="server" ondemand="false" localizable="false" loadafterui="true" /> <SharePoint:FormDigest id="FormDigest" runat="server" /> <script type="text/javascript"> // Replace this comment with the code for your scenario. </script>SharePoint:ScriptLink 標籤引用 PS.js 檔案,定義了 Project Server 2013 的 JavaScript 物件模型。 SharePoint:FormDigest 標籤會在更新伺服器內容的操作需要時產生訊息摘要,用於安全驗證。
將佔位符註解替換為以下程序中的程式碼:
要測試應用程式頁面,在選單列選擇 除錯, 開始除錯。 如果系統提示你修改 web.config 檔案,請選擇 確定。
使用 JavaScript 物件模型建立 Project Server 2013 專案
本節的程序是透過使用 JavaScript 物件模型建立專案。 此程序包含以下幾個高層次步驟:
取得目前的 ProjectContext 實例。
建立 一個 ProjectCreationInformation 物件來指定專案的初始屬性。 透過 ProjectCreationInformation.set_name 函式指定所需的名稱屬性。
使用 ProjectContext.get_projects 函式從伺服器檢索已發佈的專案。 get_projects 函式回傳一個 ProjectCollection 物件。
透過使用 ProjectCollection.add 函式並將 ProjectCreationInformation 物件傳遞,將新專案加入集合。
透過使用 ProjectCollection.update 函式和 ProjectContext.waitForQueueAsync 函式來更新集合。 update 函式會回傳一個 QueueJob 物件,你要把它傳給 waitForQueueAsync。 此徵求同時也公布了該專案。
將以下程式碼貼上你在 Visual Studio 中新增的腳本標籤間。
// Declare a global variable to store the project collection.
var projects;
// Ensure that the PS.js file is loaded before your custom code runs.
SP.SOD.executeOrDelayUntilScriptLoaded(CreateProject, "PS.js");
// Add a project the projects collection.
function CreateProject() {
// Initialize the current client context.
var projContext = PS.ProjectContext.get_current();
// Initialize a ProjectCreationInformation object and specify properties
// for the new project.
// The Name property is required and must be unique.
var creationInfo = new PS.ProjectCreationInformation();
creationInfo.set_name("Test Project 1");
// Specify optional properties for the new project.
// If not specified, the Start property uses the current date and the
// EnterpriseProjectTypeId property uses the default EPT.
creationInfo.set_description("Created through the JSOM.");
creationInfo.set_start("2013-10-01 09:00:00.000");
// Get the projects collection.
projects = projContext.get_projects();
// Add the new project to the projects collection.
projects.add(creationInfo);
// Add a second project to use in the deleting projects procedure.
creationInfo.set_name("Test Project 2");
projects.add(creationInfo);
// Submit the request to update the collection on the server
var updateJob = projects.update();
projContext.waitForQueueAsync(updateJob, 10, GetProjects);
}
// Get the projects collection.
function GetProjects(response) {
// This call demonstrates that you can get the context from the
// ProjectCollection object.
var projContext = projects.get_context();
// Register the request for information that you want to run on the server.
// This call includes an optional "Include" parameter to request only the Name, Description,
// StartDate, and Id properties of the projects in the collection.
projContext.load(projects, 'Include(Name, Description, StartDate, Id)');
// Run the request on the server.
projContext.executeQueryAsync(IterateThroughProjects, QueryFailed);
}
// Iterate through the projects collection.
function IterateThroughProjects(response) {
// Get the enumerator and iterate through the collection.
var enumerator = projects.getEnumerator();
while (enumerator.moveNext()) {
var project = enumerator.get_current();
// Create and populate a row with the values for the project's Name, Description,
// StartDate, and Id properties.
var row = tblProjects.insertRow();
row.insertCell().innerText = project.get_name();
row.insertCell().innerText = project.get_description();
row.insertCell().innerText = project.get_startDate();
row.insertCell().innerText = project.get_id();
}
// This scenario does not use the textarea or button controls.
$get("txtGuid").disabled = true;
$get("btnSend").disabled = true;
}
function QueryFailed(sender, args) {
$get("spanMessage").innerText = 'Request failed: ' + args.get_message();
}
透過使用 JavaScript 物件模型更新 Project Server 2013 專案
本節的程序會透過 JavaScript 物件模型更新專案的 startDate 屬性。 此程序包含以下幾個高層次步驟:
取得目前的 ProjectContext 實例。
使用 ProjectContext.get_projects 函式從伺服器檢索已發佈的專案。 get_projects 函式回傳一個 ProjectCollection 物件。
在伺服器端使用 ProjectContext.load 函式和 ProjectContext.executeQueryAsync 函式執行請求。
使用 ProjectContext.getById 函式來取得 PublishedProject 物件。
請使用 Project.checkOut 函式檢查目標專案。 checkOut 函式會回傳已發佈專案的草稿版本。
使用 DraftProject.set_startDate 功能更改專案開始日期。
透過 DraftProject.publish 函式和 ProjectContext.waitForQueueAsync 函式來發佈專案。 發佈函式會回傳一個 QueueJob 物件,你要把它傳給 waitForQueueAsync。
將以下程式碼貼上你在 Visual Studio 中新增的腳本標籤間。
// Declare global variables.
var projContext;
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.
projContext = PS.ProjectContext.get_current();
// Get the projects collection.
projects = projContext.get_projects();
// Register the request for information that you want to run on the server.
// This call includes an optional "Include" parameter to request only the Name, Description,
// StartDate, and Id properties of the projects in the collection.
projContext.load(projects, 'Include(Name, Description, StartDate, Id)');
// Run the request on the server.
projContext.executeQueryAsync(IterateThroughProjects, QueryFailed);
}
// Iterate through the projects collection.
function IterateThroughProjects(response) {
// Get the enumerator and iterate through the collection.
var enumerator = projects.getEnumerator();
while (enumerator.moveNext()) {
var project = enumerator.get_current();
// Create and populate a row with the values for the project's Name, Description,
// StartDate, and Id properties.
var row = tblProjects.insertRow();
row.insertCell().innerText = project.get_name();
row.insertCell().innerText = project.get_description();
row.insertCell().innerText = project.get_startDate();
row.insertCell().innerText = project.get_id();
}
// Initialize button properties.
$get("btnSend").onclick = function () { ChangeProject(); };
$get("btnSend").innerText = "Update";
}
// Change the project's start date.
function ChangeProject() {
// Get the identifier of the target project.
var targetGuid = $get("txtGuid").innerText;
// Get the target project and then check it out. The checkOut function
// returns the draft version of the project.
var project = projects.getById(targetGuid);
var draftProject = project.checkOut();
// Set the new property value and then publish the project.
// Specify "true" to also check the project in.
draftProject.set_startDate("2013-12-31 09:00:00.000");
var publishJob = draftProject.publish(true);
// Register the job that you want to run on the server and specify the
// timeout duration and callback function.
projContext.waitForQueueAsync(publishJob, 10, QueueJobSent);
}
// Print the JobState return code, which gives the status of the queue job.
function QueueJobSent(response) {
$get("spanMessage").innerText = 'JobState = ' + response + '. Wait a few seconds, then refresh the page to see your changes.';
}
function QueryFailed(sender, args) {
$get("spanMessage").innerText = 'Request failed: ' + args.get_message();
}
使用 JavaScript 物件模型刪除 Project Server 2013 專案
本節的程序是透過使用 JavaScript 物件模型刪除專案。 此程序包含以下幾個高層次步驟:
取得目前的 ProjectContext 實例。
使用 ProjectContext.get_projects 函式從伺服器檢索已發佈的專案。 get_projects 函式回傳一個 ProjectCollection 物件。
在伺服器端使用 ProjectContext.load 函式和 ProjectContext.executeQueryAsync 函式執行請求。
使用 ProjectCollection.getById 函式來取得 PublishedProject 物件。
透過將專案傳遞到 ProjectCollection.remove 函式來刪除。
透過使用 ProjectCollection.update 函式和 ProjectContext.waitForQueueAsync 函式來更新集合。 update 函式會回傳一個 QueueJob 物件,你要把它傳給 waitForQueueAsync。
將以下程式碼貼上你在 Visual Studio 中新增的腳本標籤間。
// Declare global variables.
var projContext;
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.
projContext = PS.ProjectContext.get_current();
// Get the projects collection.
projects = projContext.get_projects();
// Register the request for information that you want to run on the server.
// This call includes an optional "Include" parameter to request only the Name, Description,
// StartDate, and Id properties of the projects in the collection.
projContext.load(projects, 'Include(Name, Description, StartDate, Id)');
// Run the request on the server.
projContext.executeQueryAsync(IterateThroughProjects, QueryFailed);
}
// Iterate through the projects collection.
function IterateThroughProjects(response) {
// Get the enumerator and iterate through the collection.
var enumerator = projects.getEnumerator();
while (enumerator.moveNext()) {
var project = enumerator.get_current();
// Create and populate a row with the values for the project's Name, Description,
// StartDate, and Id properties.
var row = tblProjects.insertRow();
row.insertCell().innerText = project.get_name();
row.insertCell().innerText = project.get_description();
row.insertCell().innerText = project.get_startDate();
row.insertCell().innerText = project.get_id();
}
// Initialize button properties.
$get("btnSend").onclick = function () { DeleteProject(); };
$get("btnSend").innerText = "Delete";
}
// Delete the project.
function DeleteProject() {
// Get the identifier of the target project.
var targetGuid = $get("txtGuid").innerText;
// Get the target project and then remove it from the collection.
var project = projects.getById(targetGuid);
projects.remove(project);
var removeJob = projects.update();
// Register the job that you want to run on the server and specify the
// timeout duration and callback function.
projContext.waitForQueueAsync(removeJob, 10, QueueJobSent);
}
// Print the JobState return code, which gives the status of the queue job.
function QueueJobSent(response) {
$get("spanMessage").innerText = 'JobState = ' + response + '. Wait a few seconds, then refresh the page to see your changes.';
}
function QueryFailed(sender, args) {
$get("spanMessage").innerText = 'Request failed: ' + args.get_message();
}