0

We're trying to calculate parameters in TFS server side plugin. I'm trying to restrict users from moving PBI into Committed state unless all of their child Tasks have value under the "remaining work" field.

This basic logic works, i can go through the child items and get all the values whether exists or not. What i'm failing to understand\Accomplish is how to prevent the Save action in case task's effort is null valued.

if (taskswithoutrw.Count() != 0)
{
    permitted = false;
    TeamFoundationApplicationCore.Log("changedpermitted to false", 0, EventLogEntryType.Information);
    throw new Exception("Some tasks does not contain \"Remaining Work\" value");
}
else
{
    var tasks = childs.Where(x => TFStringComparer.WorkItemTypeName.Equals(x.Type.Name, Task));
    double workSum = 0;

    foreach (var task in tasks)
    {
        workSum += Convert.ToDouble(task.Fields["Remaining Work"].Value) / 6;
        TeamFoundationApplicationCore.Log("Remaining Work sum: " + workSum.ToString(), 0, EventLogEntryType.Information);
    }
    wi.Fields["Story Points"].Value = workSum.ToString();
    wi.Save();
}

if exception is thrown im setting the EventNotificationStatus as ActionDenied

    catch (Exception ex)
    {
            // Log error (Should be logged to the windows event log by default)
            TeamFoundationApplicationCore.LogException(requestContext, "Exception occurd in ", ex);
            TeamFoundationApplicationCore.Log("outercatch", 0, EventLogEntryType.Information);
            statusCode = -1;
            return EventNotificationStatus.ActionDenied;
    }

 Method must return this value
 return EventNotificationStatus.ActionPermitted;

But that doesn't seems to do the trick, am i missing something?

Totem
  • 1,030
  • 7
  • 16

1 Answers1

0

You can simply add a REQUIRED rule in the work item field to avoid null value. With this rule, users cannot save a work item until they have assigned values to all required fields. For example:

<FieldDefinition name="Effort" refname="Microsoft.VSTS.Scheduling.Effort" type="Double" reportable="measure">
  <REQUIRED />
  <HELPTEXT>The estimated effort to implemented the backlog item</HELPTEXT>
</FieldDefinition>

Another way is, to prevent Work Item saving if field1's value is null, add another field field2 which would be populated when the field1's value is null. When filed1's value is null, filed2's value is invalid, which will prevent the work item to be saved.

To achieve the task that when the state of the PBI work item is changed to Committed, you need to find if the child Task work items have value under the "remaining work" field, and then disable the change of state. For this you need to capture the onChange event of the state dropdown:

1.add a method AddEventHandler() which adds an event to work item.

private void AddEventHandler()
        {
              workItem.FieldChanged += new WorkItemFieldChangeEventHandler (this.workItem_FieldChanged);
        }

2.Call this method in WorkItemDatasource property

object IWorkItemControl.WorkItemDatasource
        {
            get
            {
                return workItem;
            }
            set
            {
                workItem = (WorkItem)value;
                AddEventHandler();
            }
        }

3.Now perform the required functionality in workItem_FieldChanged method.

As per your requirement, you need to identify if the changed field is state, and if it is set to Committed. This can be achieved by checking the event args as below.

(e.Field.Name == "State" && e.Field.Value.ToString() == "Committed")

Once ensure that you are capturing the right event, now find if there are any children to that work item. Also check if the link is a child. Then check the value under the "remaining work" field.

You may check this blog for more details, which should help you: http://www.codeproject.com/Articles/730928/Close-a-Work-Item-only-if-Child-Work-items-are-c

Cece Dong - MSFT
  • 29,631
  • 1
  • 24
  • 39
  • if i'm not mistaken, by your explanation + the link, you are talking only on the inteface from inside the visual studio – Totem Jan 18 '16 at 09:58