I am new to SharePoint and CSOM, but I am comfortable in PowerShell and .NET. I'm trying to do the following tasks in SharePoint Online:
- Get the list of users in a particular named group
- For each user, create a folder
- For each folder created, break inheritance with the parent folder and assign exclusive access to its respective user
For the first task, I've tried the following code:
# Get a specific group by name
$allGroups = $web.SiteGroups
$ctx.Load($allGroups)
$ctx.ExecuteQuery()
$group = $allGroups.GetByName('My Group')
# Get all users in the group 'My Group'
$users = $group.Users
$ctx.Load($users)
$ctx.ExecuteQuery()
The GroupCollection.GetByName
method always gives me a "collection has not been initialized" error. I know that $allGroups
is initialized because I am able to query other properties such as $allGroups.Count
. What am I doing wrong? Is there another way to retrieve a particular group by name?
The second task should be straightforward. I already have successfully created new folders, so I just need to use a foreach loop to accomplish this step.
I haven't been able to test the third task yet, but I have seen plenty of code samples online for breaking inheritance, and I expect that adding a permission for a user won't be too difficult. I'll edit this question if I run into problems, but in the meantime, some sample code to get me started would be very appreciated.