Imagine a situation where you want to add users to the associated SharePoint groups of a site and you do not know the Group Id / or name of the SharePoint groups associated for each permission level (Owners, Members or Visitors).
This will be very much needed when you want to setup permission of a SharePoint site when you have list of users whom they are supposed to be part.
Use Case
Here is the typical use case,
- You have executed migration of content from another system to SharePoint.
- Let us say, permission is not migrated and all you have is the list of users and its permission level of the source system and you may have that in a format something like below,
Sample Permission Data
-
Now in the above case, you know that you need to add the users who has Read permission needs to be added to Visitors Group of SharePoint site.
-
If you have this action to be done for one site, it will not be a big issue. But consider a scenario where you have large number of sites and it will be nearly impossible to know the associated groups of all the sites
Solution
I have built a simple script which reads information from this CSV file and add the users to the respective SharePoint Groups.
Script
Following is the script whose explanation is given here,
Once the script is executed, you will have the result something like below,
Explanation
Script uses CLI for Microsoft 365 for achieving the result.
Line # 12 in the code snippet uses command m365 spo web get which gets the information about the site. This also supports an option called --withGroups
when passed, you will get the associated groups (Owners, Members, Visitors) of the Site.
Then the users are added to the respective group id using the command m365 spo group user add.
The best part of the above command is that, it supports multiple usernames to be included in the options so that you donât need recursive loop to add the users one by one. This will be super handy when you have large number of users to be added to the groups and all you need is a single command which will do it for you. How cool is that!!
If you still havenât used CLI for Microsoft 365, you can always get started from here.
If you see any improvement areas, please feel free to initiate an issue in our GitHub repo. We will be more than happy to take that into account.
Photo by Erol Ahmed on Unsplash