다음을 통해 공유


PublishedProject.DeleteObject method

Deletes the PublishedProject object.

Namespace:  Microsoft.ProjectServer.Client
Assembly:  Microsoft.ProjectServer.Client (in Microsoft.ProjectServer.Client.dll)

Syntax

'Declaration
<RemoteAttribute> _
Public Function DeleteObject As QueueJob
'Usage
Dim instance As PublishedProject
Dim returnValue As QueueJob

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

Return value

Type: Microsoft.ProjectServer.Client.QueueJob
A QueueJob object that contains information about the queued job.

Remarks

If the queue job is successful, the Project Server Queuing Service deletes the published version of the project. The draft version is also deleted.

Examples

The following example deletes a specified project from both the published store and the draft store. The Project 2013 SDK download includes the complete Visual Studio 2012 solution in the ~\Samples\DeleteOneProjectCSOM folder.

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);
        }
    }
}

See also

Reference

PublishedProject class

PublishedProject members

Microsoft.ProjectServer.Client namespace