Quantcast
Channel: Project Customization and Programming forum
Viewing all articles
Browse latest Browse all 5347

CSOM: Update the IsLockedByManager task field

$
0
0

Hi,

I have a request from a Customer: he need to automatically Lock Tasks for Updates, depending on some conditions (mainly based on the "age" of te task. The Customer is hosted in Project Online.

My idea was to create a task boolean field, with a formula:

  • true if the task has to be locked
  • false otherwize

A daily batch written as a C# application will run everynight, and set the IsLockByManager field to true, for the selected task.

I've written a first proto, and it does not work: it seems that this field cannot updated, even if it is writtable. I tried to update some other tasks field (like IsManual, or IsActive), and for those fields, it works.

Is this behaviour normal ? Does anybody there already experienced this problem ? I enclose below a part of my code. Thank you for your help.

Sylvain

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.ProjectServer.Client;
using Microsoft.SharePoint.Client;
//using NeosSDI.ProjectOnline.Business;

namespace NeosSDI.ProjectOnline.CSOM
{
    public class LockTasks
    {
        static void Main(string[] args)
        {
            ConsoleColor initialColor = Console.ForegroundColor;
            try
            {
                // First, very simple: list Projects, and their tasks
                var projectContext = ProjectCSOMManager.ReadProjects();
                ProjectCSOMManager.LockNonActiveTasks(projectContext.Projects);
            }
            catch (Exception ex)
            {
                //...
            }
            Console.WriteLine("Press a key...");
            Console.ReadLine();
        }
        public static ProjectContext ReadProjects()
        {
            try
            {
                projContext = new ProjectContext(PwaPath);
                    projContext.ExecutingWebRequest += ClaimsHelper.clientContext_ExecutingWebRequest;

                // Use IncludeWithDefaultProperties to force CSOM to load the Tasks collection, otherwize we have a (very) lazy loading
                // Careful: the Load method does not perform the Load ! It prepare the context before the ExecuteQuery is run.
                projContext.Load(projContext.Projects,
                    c => c.Where(p => p.Name == "Project 1").IncludeWithDefaultProperties(pr => pr.StartDate, pr => pr.FinishDate, pr => pr.Tasks));//.Where(t=>t.Name=="Project 2 - T1")));

                // Actual execution of the Load - AFter this method, the Projects collection contains data, and the properties which are specified below.
                projContext.ExecuteQuery();

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return projContext;
        }
        public static void LockNonActiveTasks(ProjectCollection projects)
        {
            foreach (var p in projects)
            {
                if (!p.IsCheckedOut)
                {
                    DraftProject dp = p.CheckOut();
                    projContext.Load(dp.Tasks, dt => dt.IncludeWithDefaultProperties(lt => lt.Name));
                    projContext.ExecuteQuery();

                    foreach (var t in dp.Tasks)
                    {
                        // With IsActive, it works
                        // t.IsActive = false;

                        // With IsLockedByManager, it doesn't work
                        t.IsLockedByManager = true;
                    }
                    // Récup le job qui sort d'Update
                    var job = dp.Update();
                    var js = projContext.WaitForQueue(job, 10);

                    job = dp.Publish(true);
                    js = projContext.WaitForQueue(job, 10);
                    projContext.ExecuteQuery();
                }
            }
        }
    }
}



Viewing all articles
Browse latest Browse all 5347

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>