Exemplo n.º 1
0
        /// <summary>
        /// Create a new project with one local resource, one enterprise resource, one task and one assignment
        /// </summary>
        public static void CreateProjectWithTaskAndAssignment()
        {
            //
            // Load csom context
            context = GetContext(pwaInstanceUrl);

            //
            // Create a project
            csom.PublishedProject project = context.Projects.Add(new csom.ProjectCreationInformation()
            {
                Name        = projectName,
                Start       = DateTime.Today,
                Description = "Created project from C# library"
            });
            csom.JobState jobState = context.WaitForQueue(context.Projects.Update(), DEFAULTTIMEOUTSECONDS);
            JobStateLog(jobState, "Creating project");

            //
            // Create a task in project
            context.Load(project, p => p,
                         p => p.StartDate);             //load startdate of project
            context.ExecuteQuery();

            csom.DraftProject draft = project.CheckOut();
            Guid taskId             = Guid.NewGuid();

            csom.Task task = draft.Tasks.Add(new csom.TaskCreationInformation()
            {
                Id       = taskId,
                Name     = taskName,
                IsManual = false,
                Start    = project.StartDate.AddDays(1),
                Duration = "5d"
            });

            draft.Update();

            //
            // Create a local resource and assign the task to him
            Guid resourceId = Guid.NewGuid();

            csom.ProjectResource resource = draft.ProjectResources.Add(new csom.ProjectResourceCreationInformation()
            {
                Id   = resourceId,
                Name = localResourceName
            });

            draft.Update();

            csom.DraftAssignment assignment = draft.Assignments.Add(new csom.AssignmentCreationInformation()
            {
                ResourceId = resourceId,
                TaskId     = taskId
            });

            draft.Update();
            jobState = context.WaitForQueue(draft.Publish(true), DEFAULTTIMEOUTSECONDS);    // draft.Publish(true) means publish and check in
            JobStateLog(jobState, "Creating task and assgin to a local resource");
        }
Exemplo n.º 2
0
        /// <summary>
        /// Read and update the project,
        /// this method need a project named "New Project" with a task "New task" and assign to a local resource named "New local resource" already created.
        /// Basically please run CreateProjectWithTaskAndAssignment() before running this to avoid exceptions
        /// </summary>
        public static void ReadAndUpdateProject()
        {
            // Load csom context
            context = GetContext(pwaInstanceUrl);

            // Retrieve publish project named "New Project"
            // if you know the Guid of project, you can just call context.Projects.GetByGuid()
            csom.PublishedProject project = GetProjectByName(projectName, context);
            if (project == null)
            {
                Console.WriteLine("Failed to retrieve expected data, make sure you set up server data right. Press any key to continue....");
                return;
            }

            csom.DraftProject draft = project.CheckOut();

            // Retrieve project along with tasks & resources
            context.Load(draft, p => p.StartDate,
                         p => p.Description);
            context.Load(draft.Tasks, dt => dt.Where(t => t.Name == taskName));
            context.Load(draft.Assignments, da => da.Where(a => a.Task.Name == taskName &&
                                                           a.Resource.Name == localResourceName));
            context.Load(draft.ProjectResources, dp => dp.Where(r => r.Name == localResourceName));
            context.ExecuteQuery();

            // Make sure the data on server is right
            if (draft.Tasks.Count != 1 || draft.Assignments.Count != 1 || draft.ProjectResources.Count != 1)
            {
                Console.WriteLine("Failed to retrieve expected data, make sure you set up server data right. Press any key to continue....");
                Console.ReadLine();
                return;
            }

            // Since we already filetered and validated that the TaskCollection, ProjectResourceCollection and AssignmentCollection
            // contains just one filtered item each, we just get the first one.
            csom.DraftTask            task       = draft.Tasks.First();
            csom.DraftProjectResource resource   = draft.ProjectResources.First();
            csom.DraftAssignment      assignment = draft.Assignments.First();

            // Update the project description
            draft.Description += "(description updated)";

            // Update task duration and start date
            task.Duration = "10d";
            task.Start    = draft.StartDate.AddDays(3);
            draft.Update();         // Save updates so far to ensure the task changes are applied before the assignment changes

            // Update resource standard rate
            resource.StandardRate = 100.0d;
            draft.Update();

            // Update assignment work percent complete
            assignment.PercentWorkComplete = 50;

            // Publish and check in the project
            csom.JobState jobState = context.WaitForQueue(draft.Publish(true), DEFAULTTIMEOUTSECONDS);
            JobStateLog(jobState, "Updating project");
        }
        /// <summary>
        /// Read and Update Project/Task/Resource custom field values,
        /// this method need a project named "New Project" with a task "New task" and assign to a local resource named "New local resource" already created.
        /// Basically please run CreateProjectWithTaskAndAssignment() before running this to avoid exceptions
        /// </summary>
        public static void UpdateCustomFieldValues()
        {
            // Load csom context
            context = GetContext(pwaInstanceUrl);

            // Create custom field values before read it.
            // CreateCustomFields();

            // Retrive publish project named "New Project"
            // if you know the Guid of project, you can just call context.Projects.GetByGuid()
            csom.PublishedProject project = GetProjectByName(projectName, context);
            if (project == null)
            {
                Console.WriteLine("Failed to retrieve expected data, make sure you set up server data right. Press any key to continue....");
                return;
            }


            if (!project.IsCheckedOut)
            {
            }



            csom.DraftProject draft = project.CheckOut();

            // Retrieve project along with tasks & assignments & resources
            context.Load(draft.Tasks, dt => dt.Where(t => t.Name == taskName));
            context.Load(draft.Assignments, da => da.Where(a => a.Task.Name == taskName &&
                                                           a.Resource.Name == localResourceName));
            context.Load(draft.ProjectResources, dp => dp.Where(r => r.Name == localResourceName));
            context.ExecuteQuery();

            // Make sure the data on server is right
            if (draft.Tasks.Count != 1 || draft.Assignments.Count != 1 || draft.ProjectResources.Count != 1)
            {
                Console.WriteLine("Failed to retrieve expected data, make sure you set up server data right. Press any key to continue....");
                Console.ReadLine();
                return;
            }

            // Since we already filetered and validated that the TaskCollection, ProjectResourceCollection and AssignmentCollection
            // contains just one filtered item each, we just get the first one.
            csom.DraftTask            task       = draft.Tasks.First();
            csom.DraftProjectResource resource   = draft.ProjectResources.First();
            csom.DraftAssignment      assignment = draft.Assignments.First();

            // Retrieve custom field by name
            context.Load(context.CustomFields);
            context.ExecuteQuery();
            csom.CustomField projCF = context.CustomFields.FirstOrDefault(cf => cf.Name == projectCFName);
            csom.CustomField taskCF = context.CustomFields.FirstOrDefault(cf => cf.Name == taskCFName);
            csom.CustomField resCF  = context.CustomFields.FirstOrDefault(cf => cf.Name == resourceCFName);

            // Get random lookup table entry
            csom.LookupEntry taskLookUpEntry = GetRandomLookupEntries(taskCF);

            // Change project custom field value
            draft[projCF.InternalName] = "Moises was here";

            // Change task custom field value

            /*
             * --------------------------Important!---------------------------
             * if it is a lookup table customfield, need to be set as an array
             */
            task[taskCF.InternalName] = new[] { taskLookUpEntry.InternalName };

            // Change resource and assignment custom field value
            resource[resCF.InternalName]   = "Resource custom field value";
            assignment[resCF.InternalName] = "Assignment custom field value";

            // Update project and check in
            draft.Update();
            csom.JobState jobState = context.WaitForQueue(draft.Publish(true), DEFAULTTIMEOUTSECONDS);
            JobStateLog(jobState, "Updating project customfield values");
        }
Exemplo n.º 4
0
        public void ReadAndCreateProject(string projectName)
        {
            // Load csom context
            Jobs jobs = new Jobs();

            context = jobs.GetContext(pwaInstanceUrl);

            // Retrieve publish project named "New Project"
            // if you know the Guid of project, you can just call context.Projects.GetByGuid()
            csom.PublishedProject project = jobs.GetProjectByName(projectName, context);
            if (project == null)
            {
                Console.WriteLine("Failed to retrieve expected data, make sure you set up server data right. Press any key to continue....");
                return;
            }

            csom.DraftProject draft = project.CheckOut();

            // Retrieve project along with tasks & resources
            context.Load(draft, p => p.StartDate,
                         p => p.Description);
            context.Load(draft.Tasks, dt => dt.Where(t => t.Name == taskName));
            context.Load(draft.Assignments, da => da.Where(a => a.Task.Name == taskName &&
                                                           a.Resource.Name == localResourceName));
            context.Load(draft.ProjectResources, dp => dp.Where(r => r.Name == localResourceName));
            context.ExecuteQuery();

            // Make sure the data on server is right
            if (draft.Tasks.Count != 1 || draft.Assignments.Count != 1 || draft.ProjectResources.Count != 1)
            {
                Console.WriteLine("Failed to retrieve expected data, make sure you set up server data right. Press any key to continue....");
                Console.ReadLine();
                return;
            }

            // Since we already filetered and validated that the TaskCollection, ProjectResourceCollection and AssignmentCollection
            // contains just one filtered item each, we just get the first one.
            csom.DraftTask            task       = draft.Tasks.First();
            csom.DraftProjectResource resource   = draft.ProjectResources.First();
            csom.DraftAssignment      assignment = draft.Assignments.First();


            //
            // Create a project
            csom.PublishedProject copyProject = context.Projects.Add(new csom.ProjectCreationInformation()
            {
                Name        = project.Name + "(copy)",
                Start       = project.StartDate,
                Description = "Copy project from a Published one",
            });
            csom.JobState jobState = context.WaitForQueue(context.Projects.Update(), DEFAULTTIMEOUTSECONDS);
            jobs.JobStateLog(jobState, "Creating project");



            //
            // Copy and Create a task in project

            csom.DraftProject copyDraft = copyProject.CheckOut();
            Guid taskId = Guid.NewGuid();

            csom.Task copyTask = copyDraft.Tasks.Add(new csom.TaskCreationInformation()
            {
                Id       = taskId,
                Name     = task.Name,
                IsManual = task.IsManual,
                Start    = task.Start,
                Duration = task.Duration
            });

            copyDraft.Update();


            //
            // Create a local resource and assign the task to him
            Guid resourceId = Guid.NewGuid();

            csom.ProjectResource copyResource = copyDraft.ProjectResources.Add(new csom.ProjectResourceCreationInformation()
            {
                Id   = resourceId,
                Name = resource.Name
            });

            copyDraft.Update();

            csom.DraftAssignment copyAssignment = copyDraft.Assignments.Add(new csom.AssignmentCreationInformation()
            {
                ResourceId = resourceId,
                TaskId     = taskId
            });

            copyDraft.Update();


            jobState = context.WaitForQueue(copyDraft.Publish(true), DEFAULTTIMEOUTSECONDS);
            jobs.JobStateLog(jobState, "Creating task and assgin to a local resource");
        }