1
votes

I've been writing a script to connect to SharePoint online, create a document library and add some AD accounts to the permission. I've written all the code using snippets I have found through many searches but am having an issue with the permissions part.

I keep getting an error when adding the user and permission type to roledefinitionbinding. The error is:

Collection has not been initialized at line 0 char 0.

Here is my code

$collRdb = new-object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($ctx)
$collRdb.Add($role)
$collRoleAssign = $web.RoleAssignments;
$ctx.Load($principal)
$collRoleAssign.Add($principal, $collRdb)

$ctx.ExecuteQuery()

The issue is when it runs $collRoleAssign.Add($principal, $collRdb) part and stop with the error above.

I would really appreciate a hand with this before my PC get launched out of the window.

Thanks Mark

EDIT

All of the code is taken from this page:

http://jeffreypaarhuis.com/2012/06/07/scripting-sharepoint-online-with-powershell-using-client-object-model/

The only change is i'm using the get principal fun instead of the get group, but not sure if that's what has done it. I'm very new to powershell.

Thanks

1
You gave us a snippet of code, but it references variables that we have no idea what they are. What is $principal? What is $web? What is $role? What is $ctx? - TheMadTechnician

1 Answers

0
votes

I don't think you can add something into $collRoleAssign if it's not loaded before. You get an error because it has null value.

I would have wrote it like this:

$collRoleAssign = $web.RoleAssignments
$ctx.Load($collRoleAssign)

Comment: I suppose you already set $principal before

$ctx.Load($principal)

Comment: here I suppose $collRdb is set and loaded

$collRoleAssign.Add($principal, $collRdb)
$ctx.ExecuteQuery()

By the way there is a ";" in your code which should not be there

I didn't try it but that should help!

Sylvain