Compartir a través de


del método PublishedProject.DeleteObject

Elimina el objeto PublishedProject .

Espacio de nombres:  Microsoft.ProjectServer.Client
Ensamblado:  Microsoft.ProjectServer.Client (en Microsoft.ProjectServer.Client.dll)

Sintaxis

'Declaración
<RemoteAttribute> _
Public Function DeleteObject As QueueJob
'Uso
Dim instance As PublishedProject
Dim returnValue As QueueJob

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

Valor devuelto

Tipo: Microsoft.ProjectServer.Client.QueueJob
Un objeto QueueJob que contiene información sobre el trabajo en cola.

Comentarios

Si el trabajo en cola se realiza correctamente, el servicio de cola de Project Server elimina la versión publicada del proyecto. También se eliminará el borrador.

Ejemplos

En el siguiente ejemplo se elimina un proyecto especificado desde el almacén y el almacén de borradores. La descarga SDK de Project 2013 incluye la solución completa de Visual Studio 2012 en la carpeta ~\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);
        }
    }
}

Vea también

Referencia

clase PublishedProject

Miembros PublishedProject

Espacio de nombres Microsoft.ProjectServer.Client