DraftProject.Tasks property
Gets the collection of task objects for the project.
Namespace: Microsoft.ProjectServer.Client
Assembly: Microsoft.ProjectServer.Client (in Microsoft.ProjectServer.Client.dll)
Syntax
'Declaration
<RemoteAttribute> _
Public ReadOnly Property Tasks As DraftTaskCollection
Get
'Usage
Dim instance As DraftProject
Dim value As DraftTaskCollection
value = instance.Tasks
[RemoteAttribute]
public DraftTaskCollection Tasks { get; }
Property value
Type: Microsoft.ProjectServer.Client.DraftTaskCollection
A collection of DraftTask objects.
Examples
The following example gets a ProjectCollection object where a project name is specified, checks out the project, adds a task to the Tasks collection, and then publishes and checks the project back in. Because project names are unique, the ProjectCollection object can contain only one project.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.ProjectServer.Client;
namespace AddTaskToProject
{
class Program
{
private const string pwaPath = "https://ServerName/pwa/"; // Change the path to Project Web App.
private static string projName = string.Empty;
private static string taskName = string.Empty;
private static int timeoutSeconds = 10; // The maximum wait time for a queue job, in seconds.
private static ProjectContext projContext;
static void Main(string[] args)
{
if (!ParseCommandLine(args))
{
Usage();
ExitApp();
}
projContext = new ProjectContext(pwaPath);
Console.Write("Editing project: '{0}'; adding task: '{1}' . . .", projName, taskName);
var projCollection = projContext.LoadQuery(
projContext.Projects
.Where(p => p.Name == projName));
projContext.ExecuteQuery();
if (projCollection.Count() > 0)
{
PublishedProject proj2Edit = projCollection.First();
DraftProject projCheckedOut = proj2Edit.CheckOut();
// Create a task.
TaskCreationInformation newTask = new TaskCreationInformation();
newTask.Name = taskName;
newTask.IsManual = false;
newTask.Duration = "3d";
newTask.Start = DateTime.Today;
// Add the task to the checked out project.
projCheckedOut.Tasks.Add(newTask);
projCheckedOut.Publish(true);
// Update the published projects collection.
QueueJob qJob = projContext.Projects.Update();
JobState jobState = projContext.WaitForQueue(qJob, timeoutSeconds);
if (jobState == JobState.Success)
{
Console.WriteLine("\nSuccess!");
}
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();
}
}
else
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("\nNo project with the name '{0}'", projName);
Console.ResetColor();
}
ExitApp();
}
// 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 "*taskname":
case "*t":
if (++i >= argsLen) return false;
taskName = args[i];
break;
case "*queuetime":
case "*q":
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: AddTaskToProject -projName | -p \"Project name\"\n\t\t\t-taskName | -t \"Task name\"\n\t\t\t[-queueTime | -q sec]";
example += "\n\nExample: AddTaskToProject -p \"TestProj1\" -t \"Task name\"";
Console.WriteLine(example);
}
private static void ExitApp()
{
Console.Write("\nPress any key to exit... ");
Console.ReadKey(true);
Environment.Exit(0);
}
}
}