none
Add a Enterprise Resource to project with CSOM on PowerShell RRS feed

  • Question

  • I'm trying to add a new Resource to projects by PowerShell.

    What I wanna do is give a name of an AD user (that I know is not a Resource on the project) and add this as a new Resource.

    This is a simple script with basic commands where I can load some properties and list Enterprise Resources from a project.

    How can I add a new Resource?

    # Get context
    $ctx = New-Object Microsoft.ProjectServer.Client.ProjectContext($UrlPWA); $ctx.Load($ctx.Projects); $ctx.Load($ctx.EnterpriseResources); $ctx.ExecuteQuery(); $ctx.EnterpriseResources | ForEach-Object { $ctx.Load($_); } $ctx.ExecuteQuery(); $ctx.EnterpriseResources| ForEach-Object { $ctx.Load($_.User); } $ctx.ExecuteQuery();
    $Project = $ctx.Projects[6]; # Get a (specific) project
    $ctx.Load($Project.CheckedOutBy); $ctx.Load($Project.Draft.Assignments); $ctx.ExecuteQuery(); $ctx.Load($Project.Draft.ProjectResources); $ctx.ExecuteQuery(); $Project.Draft.ProjectResources | ForEach-Object { $ctx.Load($_.EnterpriseResource); } $ctx.ExecuteQuery(); $Resources = $Project.Draft.ProjectResources | Where-Object { $_.EnterpriseResource.ServerObjectIsNull -eq $false }; $Resources | ForEach-Object { $ctx.Load($_.EnterpriseResource.User); } $ctx.ExecuteQuery(); $Resources | ForEach-Object {
    Write-Host $_.EnterpriseResource.Email $_.EnterpriseResource.User.LoginName;
    }

    .
    Monday, April 29, 2019 7:23 PM

All replies

  • Paul,

    Thank you. This code clarify some points, but I'm still not capable to add a new resource to project.

    When I'm going to add with the method $Draft.ProjectResources.AddEnterpriseResource($userTo); this error stop the script:

    format-default : 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.
        + CategoryInfo          : NotSpecified: (:) [format-default], CollectionNotInitializedException
        + FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException,Microsoft.PowerShell.Commands.FormatDefaultCommand
    The code:

    ################################################ # User do add $To = "ValidUserName";
    $AD = "ActiveDirectory"; ################################################ ################################################ # Context / Project / EnterpriseResources Write-Host "Context / Project / EnterpriseResources"; $ctx = New-Object Microsoft.ProjectServer.Client.ProjectContext($env:PWA); $ctx.Load($ctx.Projects); $ctx.Load($ctx.EnterpriseResources); $ctx.ExecuteQuery(); ################################################ ################################################ # Load Enterprise Resources -> User Write-Host "Enterprise Resources -> User"; $ctx.EnterpriseResources | ForEach-Object { $ctx.Load($_.User); }; $ctx.ExecuteQuery(); ################################################ ################################################ # Lookup for the Enterprise User $userTo = $null; $ctx.EnterpriseResources | Where-Object { $_.User.LoginName -eq "i:0#.w|$($AD)\$($To)" } | ForEach-Object { $userTo = $_; }; If($userTo -eq $null) { Write-Host -ForegroundColor Yellow "User not found: i:0#.w|$($AD)\$($To)"; Return; } Write-Host "User found: $($userTo.User.LoginName)"; ################################################ ################################################ # Select a project $Project = $ctx.Projects[6]; Write-Host "Project: $($Project.Name)"; ################################################ ################################################ # Load CheckedOutBy $ctx.Load($Project.CheckedOutBy); $ctx.ExecuteQuery(); If($Project.CheckedOutBy.LoginName -ne $Null) { Write-Host -ForegroundColor Yellow "Project Checked-Out"; Return; } ################################################ ################################################ # CheckOut project Write-Host "Checking-Out"; $Draft = $Project.CheckOut(); $ctx.Load($Draft); $ctx.ExecuteQuery(); ################################################ ################################################ # With or without this load the same fail occurs #$ctx.Load($Draft.ProjectResources); #$ctx.ExecuteQuery(); ################################################ ################################################ # Add Resource Write-Host "Add Enterprise Resource [$($userTo.GetType())]"; $Draft.ProjectResources.AddEnterpriseResource($userTo); #format-default : 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. # + CategoryInfo : NotSpecified: (:) [format-default], CollectionNotInitializedException # + FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException,Microsoft.PowerShell.Commands.FormatDefaultCommand Write-Host "Add Enterprise Resource [`$ctx.ExecuteQuery()]"; $ctx.ExecuteQuery(); ################################################ ################################################ # Update Write-Host "Update"; $Update = $Draft.Update(); Do { $ctx.Load($Update); $ctx.ExecuteQuery(); $Result = $ctx.WaitForQueue($Update, 1); Write-Host "* $Result"; } While ($Result -ne "Success"); ################################################ ################################################ # Publish and CheckIn Write-Host "Publish and Check-In"; $Publish = $Draft.Publish($True); Do { $ctx.Load($Publish); $ctx.ExecuteQuery(); $Result = $ctx.WaitForQueue($Publish, 1); Write-Host "* $Result"; } While ($Result -ne "Success"); ################################################





    Thursday, May 2, 2019 10:35 PM