Freigeben über


PublishedProject.DeleteObject-Methode

Löscht das Objekt PublishedProject .

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()

Rückgabewert

Typ: Microsoft.ProjectServer.Client.QueueJob
Ein QueueJob -Objekt, das Informationen über den Auftrag in der Warteschlange enthält.

Hinweise

Wenn der Warteschlangenauftrag erfolgreich ist, löscht der Project Server-Warteschlangendienst die veröffentlichte Version des Projekts. Die Entwurfsversion wird ebenfalls gelöscht.

Beispiele

Das folgende Beispiel löscht ein angegebenes Projekt aus der veröffentlichten Store und Entwurfsspeicher. Der Project 2013 SDK-Download enthält die vollständige Visual Studio 2012 -Lösung in den Ordner ~\Samples\DeleteOneProjectCSOM .

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

Siehe auch

Referenz

PublishedProject Klasse

PublishedProject-Member

Microsoft.ProjectServer.Client-Namespace