Add a Enterprise Resource to project with CSOM on PowerShell

Copper Contributor

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);

# Load Enterprise Resources -> User
Write-Host "Enterprise Resources -> User";
$ctx.EnterpriseResources | ForEach-Object { $ctx.Load($_.User); };

# 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)";
Write-Host "User found: $($userTo.User.LoginName)";

# Select a project
$Project = $ctx.Projects[6];
Write-Host "Project: $($Project.Name)";

# Load CheckedOutBy
If($Project.CheckedOutBy.LoginName -ne $Null) {
    Write-Host -ForegroundColor Yellow "Project Checked-Out";

# CheckOut project
Write-Host "Checking-Out";
$Draft = $Project.CheckOut();

# Add Resource
Write-Host "Add Enterprise Resource [$($userTo.GetType())]";

# Error line:
# # #

Write-Host "Add Enterprise Resource [`$ctx.ExecuteQuery()]";

# Update
Write-Host "Update";
$Update = $Draft.Update();
Do {
    $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 {
    $Result = $ctx.WaitForQueue($Publish, 1);
    Write-Host "* $Result";
} While ($Result -ne "Success");


2 Replies

@FabioBrandao Sometimes it's different way between Project Online and Project Server. I think your code is for Project Online and not for Project Server. Have you try by using this reference?

@Ade_Budiman_Lim no, it's on an On Premisse environment. And it is an unaccepted bug from Project Server CSON on PowerShell. I made a mistake talking about my goal to support of Microsoft, and after that they sad repeatedly "It will not work" referring to my goal, and not accepting the point I exposed on this post. I'm very frustrated every time I report things like that and they sidetracking me (this case took around 5 month to at the end I say "you will not focus on my problem and don't stop saying 'it will not work', close this case please.")

It is a bug, period. But they can't see it.
The solution was go to C#. There, exactly the same logic (creation of objects, callings and attributions) and it worked very well.

PS: Today I don't know if it was fixed on PowerShell.