Assigning a resource to project team from PowerShell using CSOM RRS feed

  • Question

  • Hi

    I'm busy writing a script to assign resources to a task. I'm doing this throught PowerShell, and by referencing the CSOM dll and using those objects. One of the requirements to do that is to add the resource to the project team, but I am consistently getting "The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.", whenever I call AddEnterpiseResource or Add() on the DraftProject.ProjectResources collection

    I took the snippet from this thread and put it into a console app. That works 100%. Converting it to PowerShell keeps giving me the same issue. 

    Does anyone know what I need to do to add the resource to the collection? My script is below. It fails at the line in bold.

    Add-Type -Path 'C:\Scripts\CSOMAssignResources\Ref\Microsoft.ProjectServer.Client.dll'
    $global:pjContext = $null
    function Get-ProjectContext {
      if($global:pjContext -eq $null ) {
            $PWAInstanceURL = ""
            $PWAUserName = "" 
            $PWAUserPassword = ConvertTo-SecureString -string "" -AsPlainText -Force
            $PWAIntances++ | out-null
            $global:pjContext = New-Object Microsoft.ProjectServer.Client.ProjectContext($PWAInstanceURL)
            $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $PWAUserName, $PWAUserPassword
            $global:pjContext.Credentials = $cred 
        return $global:pjContext
    function assignToTeam($projectUID, $eResource)
        $project = $global:pjContext.Projects.GetByGuid($projectUID)
        if ($project -eq $null)
            Write-Host "Project $projectUID not found"
        $draftProj = $project.CheckOut()
        $numOfRes = $draftProj.ProjectResources.Count
        $resoDetails = New-Object Microsoft.ProjectServer.Client.ProjectResourceCreationInformation
        $resoDetails.Id = $eResource.Id
        $resoColl = $draftProj.ProjectResources
         $qJob = $global:pjContext.Projects.Update()
         $jobState = $global:pjContext.WaitForQueue($qJob, 30)
    $global:pjContext = Get-ProjectContext
    $entResUid = [Guid]"9B9867AB-1453-E611-80CB-00155D02BC0E"
    $projectGuid = [Guid]"C403D2DF-3C97-40F9-80B0-F969089698F9"
    $numResInCollection = $global:pjContext.EnterpriseResources.Count
    if ($numResInCollection -ge 0)
        $res = $global:pjContext.EnterpriseResources.GetByGuid($entResUid);
        assignToTeam -projectUID $projectGuid -eResource $res

    Thursday, February 9, 2017 1:00 PM

All replies