0

I'm trying to automate a process which takes the absences of assigned project resources into account. We use a separate application for absence management of our personnel. What I'm trying to achieve is; query the absences (possibly multiple periods) and assign them to a corresponding task e.g.:

  • Holiday
  • Sickness
  • Training

I've been trying to figure out if it's possible to have multiple task assignments for the same resource, each covering one or more absence periods (start and finish) within the timeframe of a project.

Any ideas or clues on how I can solve this problem are welcome.

Thanks!

0

It's not technically possible for one resource to have multiple assignments on a single task in Project.

The alternatives are:

  1. Create one assignment and update the time-phased data, ie the assignment will have 8h per day for each day during those absences, for all other days 0h must be entered.
  2. Create one task per absence. Not ideal but this is probably the easiest option.
  3. Update the resource's calendar instead, create exceptions for the absence dates instead of assignments. Useful as it will mark those dates with 0 availability for those resources.

The code for each of these is quite different:

  • 1st option I believe a VBA macro or client side VSTO C# solution built on Project client is the only option.
  • No. 2 can be done via any API, client side or even via REST or PowerShell (I've done this before in PowerShell scripts - see example below)
  • AFAIK can be done via all API's.
function CreateAbsenceTask([string]$taskname, [datetime]$start, [datetime]$finish, [string]$resourceid)
{
    $startString = "$($start.ToString("yyyy-MM-dd"))T08:00:00"
    $finishString = "$($finish.ToString("yyyy-MM-dd"))T17:00:00"

    try {
        Write-Host "Creating task $taskname from $startString to $finishString" -ForegroundColor DarkGray -NoNewline

        # Task parameters as JSON payload
        $taskid = [Guid]::NewGuid()
        $body = "{
            'parameters': {
                'Id': '$taskid',
                'Name': '$taskname',        
                'Start': '$startString',
                'Finish': '$finishString',
                'IsManual': 'true'
            }
        }"

        # ReST request to create a task
        $response = Post-ReSTRequest $SiteUrl "ProjectServer/Projects('$ProjectId')/Draft/Tasks/Add" $body
        $json = ConvertFrom-Json $response
        $task = $json.d

        Write-Host ", created $($task.Id)" -ForegroundColor DarkGray

        AddTaskAssignment $taskid $resourceid

        Start-Sleep -Seconds 5
    }
    catch 
    {
        $ErrorMessage = $_.Exception.Message
        Write-Host ""
        Write-Warning $ErrorMessage
    }
}

function AddTaskAssignment([Guid]$taskId, [string]$resourceid)
{
    try {        
        Write-Host " - Adding task assignment for resource $resourceid"
        # Assignment parameters as JSON payload
        $body = "{
            'parameters': {
                'ResourceId': '$resourceid',
                'TaskId': '$taskid',
                'Notes': 'Created from PowerShell using REST API'
            }
        }"

        # ReST request to create an assignment for the local resource
        $response = Post-ReSTRequest $SiteUrl "ProjectServer/Projects('$ProjectId')/Draft/Assignments/Add" $body
    }
    catch
    {
        $ErrorMessage = $_.Exception.Message

        # Slow down and retry on errors!
        if ($ErrorMessage -like '*403*') {
            Start-Sleep -Seconds 10            
            AddTaskAssignment $taskid $resourceid
            Start-Sleep -Seconds 10
            return
        }

        Write-Error $ErrorMessage
        throw "Error creating assignment"
    }
}

Note that example is not complete and uses the helper ReST.ps1 methods from here: https://github.com/OfficeDev/Project-REST-Basic-Operations

  • I ended up creating a task per absence just like u said. Not ideal but it got the job done. Thank you for your clarification! – Kenny Aug 22 at 7:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.