次の方法で共有


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 名前空間