Add a Enterprise Resource to project with CSOM on PowerShell

New 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);
$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");

 

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?
https://vamsi-sharepointhandbook.blogspot.com/2016/06/project-server-2013-build-project-team.html

@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.