none
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)
    {
        $global:pjContext.Load($global:pjContext.Projects.GetByGuid($projectUID))
        $global:pjContext.ExecuteQuery()
    
        $project = $global:pjContext.Projects.GetByGuid($projectUID)
    
        if ($project -eq $null)
        {
            Write-Host "Project $projectUID not found"
            return
        }
    
        $draftProj = $project.CheckOut()
        $global:pjContext.Load($draftProj)
        $global:pjContext.ExecuteQuery()
    
        $global:pjContext.Load($draftProj.ProjectResources)
        $global:pjContext.ExecuteQuery()
    
        $numOfRes = $draftProj.ProjectResources.Count
    
        $resoDetails = New-Object Microsoft.ProjectServer.Client.ProjectResourceCreationInformation
        $resoDetails.Id = $eResource.Id
    
        $resoColl = $draftProj.ProjectResources
        $global:pjContext.Load($resoColl)
        $global:pjContext.ExecuteQuery()
        $resoColl.Add($resoDetails)
        #$resoColl.AddEnterpriseResource($eResource)
    
        $global:pjContext.EnterpriseResources.Update()
    
    
         $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"
    
    $global:pjContext.Load($global:pjContext.EnterpriseResources)
    $global:pjContext.ExecuteQuery()
    
    $numResInCollection = $global:pjContext.EnterpriseResources.Count
    
    if ($numResInCollection -ge 0)
    {
        $res = $global:pjContext.EnterpriseResources.GetByGuid($entResUid);
        $global:pjContext.Load($res);
        $global:pjContext.ExecuteQuery();
    
        assignToTeam -projectUID $projectGuid -eResource $res
    }
    
    
    

    Thursday, February 9, 2017 1:00 PM

All replies