Home

Add a Enterprise Resource to project with CSOM on PowerShell

%3CLINGO-SUB%20id%3D%22lingo-sub-583244%22%20slang%3D%22en-US%22%3EAdd%20a%20Enterprise%20Resource%20to%20project%20with%20CSOM%20on%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-583244%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EI'm%20using%20PowerShell%20with%20CSOM%20to%20add%20an%20enterprise%20resource%20to%20a%20project%20on%20Project%20Server%202016%20(on%20premisse).%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EI%20found%20some%20examples%20with%20C%23%20that%20guide%20me%2C%20but%20it's%20doesn't%20looks%20like%20a%20common%20thing%20on%20internet.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt%20is%20so%20frustrating%20don't%20get%20more%20clue%20to%20the%20error%2Fproblem.%20Some%20times%20I%20think%20that%20is%20a%20kind%20of%20bug%20on%20this%20library%20to%20PowerShell.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECould%20any%20one%20help%20me%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20message%20erro%3A%3C%2FP%3E%3CPRE%3Eformat-default%20%3A%20The%20collection%20has%20not%20been%20initialized.%20It%20has%20not%20been%20requested%20or%20the%20request%20has%20not%20been%20executed.%20It%20may%20need%20to%20be%20explicitly%20requested.%3CBR%20%2F%3E%20%20%20%20%2B%20CategoryInfo%20%3A%20NotSpecified%3A%20(%3A)%20%5Bformat-default%5D%2C%20CollectionNotInitializedException%3CBR%20%2F%3E%20%20%20%20%2B%20FullyQualifiedErrorId%20%3A%20Microsoft.SharePoint.Client.CollectionNotInitializedException%2CMicrosoft.PowerShell.Commands.FormatDefaultCommand%3C%2FPRE%3E%3CP%3EThe%20code%20I%20wrote%3A%3C%2FP%3E%3CPRE%3E%23%20User%20do%20add%3CBR%20%2F%3E%24To%20%3D%20%22ValidUserName%22%3B%3CBR%20%2F%3E%24AD%20%3D%20%22ActiveDirectory%22%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%23%20Context%20%2F%20Project%20%2F%20EnterpriseResources%3CBR%20%2F%3EWrite-Host%20%22Context%20%2F%20Project%20%2F%20EnterpriseResources%22%3B%3CBR%20%2F%3E%24ctx%20%3D%20New-Object%20Microsoft.ProjectServer.Client.ProjectContext(%24env%3APWA)%3B%3CBR%20%2F%3E%24ctx.Load(%24ctx.Projects)%3B%3CBR%20%2F%3E%24ctx.Load(%24ctx.EnterpriseResources)%3B%3CBR%20%2F%3E%24ctx.ExecuteQuery()%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%23%20Load%20Enterprise%20Resources%20-%26gt%3B%20User%3CBR%20%2F%3EWrite-Host%20%22Enterprise%20Resources%20-%26gt%3B%20User%22%3B%3CBR%20%2F%3E%24ctx.EnterpriseResources%20%7C%20ForEach-Object%20%7B%20%24ctx.Load(%24_.User)%3B%20%7D%3B%3CBR%20%2F%3E%24ctx.ExecuteQuery()%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%23%20Lookup%20for%20the%20Enterprise%20User%3CBR%20%2F%3E%24userTo%20%3D%20%24null%3B%3CBR%20%2F%3E%24ctx.EnterpriseResources%20%7C%20Where-Object%20%7B%20%24_.User.LoginName%20-eq%20%22i%3A0%23.w%7C%24(%24AD)%5C%24(%24To)%22%20%7D%20%7C%20ForEach-Object%20%7B%20%24userTo%20%3D%20%24_%3B%20%7D%3B%3CBR%20%2F%3EIf(%24userTo%20-eq%20%24null)%20%7B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20Write-Host%20-ForegroundColor%20Yellow%20%22User%20not%20found%3A%20i%3A0%23.w%7C%24(%24AD)%5C%24(%24To)%22%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20Return%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3EWrite-Host%20%22User%20found%3A%20%24(%24userTo.User.LoginName)%22%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%23%20Select%20a%20project%3CBR%20%2F%3E%24Project%20%3D%20%24ctx.Projects%5B6%5D%3B%3CBR%20%2F%3EWrite-Host%20%22Project%3A%20%24(%24Project.Name)%22%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%23%20Load%20CheckedOutBy%3CBR%20%2F%3E%24ctx.Load(%24Project.CheckedOutBy)%3B%3CBR%20%2F%3E%24ctx.ExecuteQuery()%3B%3CBR%20%2F%3EIf(%24Project.CheckedOutBy.LoginName%20-ne%20%24Null)%20%7B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20Write-Host%20-ForegroundColor%20Yellow%20%22Project%20Checked-Out%22%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20Return%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%3CBR%20%2F%3E%23%20CheckOut%20project%3CBR%20%2F%3EWrite-Host%20%22Checking-Out%22%3B%3CBR%20%2F%3E%24Draft%20%3D%20%24Project.CheckOut()%3B%3CBR%20%2F%3E%24ctx.Load(%24Draft)%3B%3CBR%20%2F%3E%24ctx.ExecuteQuery()%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%23%20Add%20Resource%3CBR%20%2F%3EWrite-Host%20%22Add%20Enterprise%20Resource%20%5B%24(%24userTo.GetType())%5D%22%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSTRONG%3E%23%20Error%20line%3A%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%24Draft.ProjectResources.AddEnterpriseResource(%24userTo)%3B%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3E%23%20%23%20%23%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CBR%20%2F%3EWrite-Host%20%22Add%20Enterprise%20Resource%20%5B%60%24ctx.ExecuteQuery()%5D%22%3B%3CBR%20%2F%3E%24ctx.ExecuteQuery()%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%23%20Update%3CBR%20%2F%3EWrite-Host%20%22Update%22%3B%3CBR%20%2F%3E%24Update%20%3D%20%24Draft.Update()%3B%3CBR%20%2F%3EDo%20%7B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%24ctx.Load(%24Update)%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%24ctx.ExecuteQuery()%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%24Result%20%3D%20%24ctx.WaitForQueue(%24Update%2C%201)%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20Write-Host%20%22*%20%24Result%22%3B%3CBR%20%2F%3E%7D%20While%20(%24Result%20-ne%20%22Success%22)%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%23%20Publish%20and%20CheckIn%3CBR%20%2F%3EWrite-Host%20%22Publish%20and%20Check-In%22%3B%3CBR%20%2F%3E%24Publish%20%3D%20%24Draft.Publish(%24True)%3B%3CBR%20%2F%3EDo%20%7B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%24ctx.Load(%24Publish)%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%24ctx.ExecuteQuery()%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%24Result%20%3D%20%24ctx.WaitForQueue(%24Publish%2C%201)%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20Write-Host%20%22*%20%24Result%22%3B%3CBR%20%2F%3E%7D%20While%20(%24Result%20-ne%20%22Success%22)%3B%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
FabioBrandao
Visitor

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

 

Related Conversations
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
Tabs and Dark Mode
cjc2112 in Discussions on
30 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
Security Community Webinars
Valon_Kolica in Security, Privacy & Compliance on
7 Replies