共用方式為


PublishedProject.DeleteObject 方法

會刪除PublishedProject物件。

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

語法

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

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

傳回值

類型:Microsoft.ProjectServer.Client.QueueJob
QueueJob物件,其中包含已排入佇列工作的相關資訊。

備註

佇列工作成功時,Project Server 佇列服務會刪除已發佈的專案版本。也會刪除草稿版本。

範例

下列範例會從已發佈的存放區與草稿儲存區刪除指定的專案。Project 2013 SDK 下載~\Samples\DeleteOneProjectCSOM資料夾中包含完整Visual Studio 2012解決方案。

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

namespace DeleteOneProject
{
    class Program
    {
        // Change the path for your Project Web App instance.
        private const string pwaPath = "https://servername/pwa/";
        private static string projName = string.Empty;
        private static int timeoutSeconds = 10;  // The default queue job wait time queue job, in seconds.

        private static ProjectContext projContext;

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

            projContext = new ProjectContext(pwaPath);

            bool projDeleted = DeleteTheProject(projName);

            ExitApp(projDeleted);
        }

        // Delete the specified project from the collection of projects.
        private static bool DeleteTheProject(string projectName)
        {
            bool isProjectDeleted = false;

            Console.Write("Deleting the project: '{0}'", projName);

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

            projContext.ExecuteQuery();

            int numProjectsInCollection = projCollection.Count();

            if (numProjectsInCollection > 0)
            {
                projCollection.First().DeleteObject();

                // Update the collection of published projects.
                QueueJob qJob = projContext.Projects.Update();
                isProjectDeleted = SubmitQueueJob(qJob);
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("\nNo project named '{0}' exists.", projName);
            }

            if (isProjectDeleted)
            {
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine("\nThe '{0}' project was deleted.", projName);
                Console.ResetColor();
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("\nThere was a problem deleting the project: '{0}'.", projName);
                Console.ResetColor();
            }
            return isProjectDeleted;
        }

        // Call WaitForQueue, for the specified maximum time. If the job state is not successful,
        // write an error message.
        private static bool SubmitQueueJob(QueueJob qJob)
        {
            bool jobSuccess = false;
            // Calling Load and ExecuteQuery for the queue job is optional. If qJob is 
            // not initialized when you call WaitForQueue, Project Server initializes it.
            //projContext.Load(qJob);
            //projContext.ExecuteQuery();

            JobState jobState = projContext.WaitForQueue(qJob, timeoutSeconds);          

            try
            {
                if (jobState == JobState.Success)
                {
                    jobSuccess = true;
                }
                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();
                }              
            }

            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("\nError: {0}", ex.Message);
                Console.ResetColor();
            }
                return jobSuccess;
        }

        // 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 "*timeout":
                        case "*t":
                            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: -projName | -n \"Project name\" [-timeout | -t sec]";
            example += "\nExample: -n \"My New Project\"";
            Console.WriteLine(example);
        }

        private static void ExitApp(bool projDeleted)
        {
            Console.Write("\n{0}Press any key to exit... ", projDeleted ? "Success! " : "");
            Console.ReadKey(true);
            Environment.Exit(0);
        }
    }
}

請參閱

參照

PublishedProject 類別

PublishedProject 成員

Microsoft.ProjectServer.Client 命名空間