ProjectCollection.Update 方法

命名空間:  Microsoft.ProjectServer.Client
組件:  Microsoft.ProjectServer.Client (在 Microsoft.ProjectServer.Client.dll 中)

語法

'宣告
<RemoteAttribute> _
Public Function Update As QueueJob
'用途
Dim instance As ProjectCollection
Dim returnValue As QueueJob

returnValue = instance.Update()
[RemoteAttribute]
public QueueJob Update()

傳回值

類型:Microsoft.ProjectServer.Client.QueueJob
已排入佇列的工作。

範例

下列範例會取得ProjectCollection其中物件的專案名稱會指定、 取出專案、 新增的任務,然後發佈並存回專案。專案名稱是唯一的因為ProjectCollection物件可以包含一個專案。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.ProjectServer.Client;

namespace AddTaskToProject
{
    class Program
    {
        private const string pwaPath = "https://ServerName/pwa/"; // Change the path to Project Web App.
        private static string projName = string.Empty;
        private static string taskName = string.Empty;
        private static int timeoutSeconds = 10;  // The maximum wait time for a queue job, in seconds.

        private static ProjectContext projContext;

        static void Main(string[] args)
        {
            if (!ParseCommandLine(args))
            {
                Usage();
                ExitApp();
            }

            projContext = new ProjectContext(pwaPath);

            Console.Write("Editing project: '{0}'; adding task: '{1}' . . .", projName, taskName);

            var projCollection = projContext.LoadQuery(
                projContext.Projects
                    .Where(p => p.Name == projName));

            projContext.ExecuteQuery();

            if (projCollection.Count() > 0)
            {
                PublishedProject proj2Edit = projCollection.First();

                DraftProject projCheckedOut = proj2Edit.CheckOut();

                // Create a task.
                TaskCreationInformation newTask = new TaskCreationInformation();
                newTask.Name = taskName;
                newTask.IsManual = false;
                newTask.Duration = "3d";
                newTask.Start = DateTime.Today;

                // Add the task to the checked out project.
                projCheckedOut.Tasks.Add(newTask);
                projCheckedOut.Publish(true);

                // Update the published projects collection.
                QueueJob qJob = projContext.Projects.Update();
                JobState jobState = projContext.WaitForQueue(qJob, timeoutSeconds);

                if (jobState == JobState.Success)
                {
                    Console.WriteLine("\nSuccess!");
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("\nThere is a problem in the queue. Timeout is {0} seconds.",
                        timeoutSeconds);
                    Console.WriteLine("\tQueue JobState: {0}", jobState.ToString());
                    Console.ResetColor();
                }
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("\nNo project with the name '{0}'", projName);
                Console.ResetColor();
            }

            ExitApp();
        }

        // Parse the command line. Return true if there are no errors.
        private static bool ParseCommandLine(string[] args)
        {
            bool error = false;
            int argsLen = args.Length;

            try
            {
                for (int i = 0; i < argsLen; i++)
                {
                    if (error) break;
                    if (args[i].StartsWith("-") || args[i].StartsWith("/"))
                        args[i] = "*" + args[i].Substring(1).ToLower();

                    switch (args[i])
                    {
                        case "*projname":
                        case "*p":
                            if (++i >= argsLen) return false;
                            projName = args[i];
                            break;
                        case "*taskname":
                        case "*t":
                            if (++i >= argsLen) return false;
                            taskName = args[i];
                            break;
                        case "*queuetime":
                        case "*q":
                            if (++i >= argsLen) return false;
                            timeoutSeconds = Convert.ToInt32(args[i]);
                            break;
                        case "*?":
                        default:
                            error = true;
                            break;
                    }
                }
            }
            catch (FormatException)
            {
                error = true;
            }

            if (string.IsNullOrEmpty(projName)) error = true;
            return !error;
        }

        private static void Usage()
        {
            string example = "Usage: AddTaskToProject -projName | -p \"Project name\"\n\t\t\t-taskName | -t \"Task name\"\n\t\t\t[-queueTime | -q sec]";
            example += "\n\nExample: AddTaskToProject -p \"TestProj1\" -t \"Task name\"";
            Console.WriteLine(example);
        }

        private static void ExitApp()
        {
            Console.Write("\nPress any key to exit... ");
            Console.ReadKey(true);
            Environment.Exit(0);
        }
    }
}

請參閱

參照

ProjectCollection 類別

ProjectCollection 成員

Microsoft.ProjectServer.Client 命名空間