none
CSOM: Update the IsLockedByManager task field RRS feed

  • Question

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

    Wednesday, December 17, 2014 5:09 AM

Answers

  • Hi,

    To give an update of this question, I managed this problem differently: the IsLockedByManager field has really no effect on the task. For DraftTask, this field is writtable, but the change is not taken into account, and not saved (bug or undiscovered function)...

    Instead of a batch, I created a macro in Project Pro: on the Save event, the macro tests the status of a custom field.

    Depending of this field, I update the Publish field of the task. The macro is integrated in the Entreprise Global, to make its deployment easy for all the users.

    This macro is based on a post at this adress: thanks to its author :)

    http://www.applepark.co.uk/automatically-setting-the-task-publish-field-to-no/

    Private Sub Project_BeforeSave(ByVal pj As Project)
        SetPublishField
    End Sub
    
    
    Sub SetPublishField()
    
        Dim Temp As Long
        Dim Tsk As Task
        Dim TaskField As Long
        Dim IsTaskToLock As String
        
        For Each Tsk In ActiveProject.Tasks
        ' check to see if a task exists
        
            If Not Tsk Is Nothing Then
        
        ' check to see if the task is external or a summary
                If Not (Tsk.ExternalTask Or Tsk.Summary) Then
                    TaskField = FieldNameToFieldConstant("MY LOCK FIELD", pjTask)
                    IsTaskToLock = Tsk.GetField(TaskField)
                    If IsTaskToLock = "Oui" Then
                        Tsk.IsPublished = False
                    Else
                        Tsk.IsPublished = True
                    End If
                End If
        
            End If
       
        Next Tsk
    
    End Sub

     

    Friday, January 9, 2015 10:29 AM