User Profile
FabioBrandao
Copper Contributor
Joined 6 years ago
User Widgets
Recent Discussions
Add a Enterprise Resource to project with CSOM on PowerShell
I'm using PowerShell with CSOM to add an enterprise resource to a project on Project Server 2016 (on premisse). I found some examples with C# that guide me, but it's doesn't looks like a common thing on internet. It is so frustrating don't get more clue to the error/problem. Some times I think that is a kind of bug on this library to PowerShell. Could any one help me? The message erro: 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 I wrote: # 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(); # Add Resource Write-Host "Add Enterprise Resource [$($userTo.GetType())]"; # Error line: $Draft.ProjectResources.AddEnterpriseResource($userTo); # # # 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");3.8KViews0likes2Comments
Groups
Recent Blog Articles
No content to show