It's not technically possible for one resource to have multiple assignments on a single task in Project.
The alternatives are:
- 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.
- Create one task per absence. Not ideal but this is probably the easiest option.
- 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