Introduction
In this article, you will learn how to retrieve or add users as site collection administrators from/to SharePoint online sites, using PnP Core CSOM library.
The main advantage of using PnP Core libraries is the reduced code to get the required information. The required object can be retrieved with a very small piece of code, once the client context is set.
Prerequisite
- PnP Core CSOM documentation can be found on the official site here.
- PnP Core CSOM library packages can be downloaded here.
The code, given below, is being tested, using Visual Studio console Application. Once the console application is created, the packages can be installed, using "Install-Package SharePointPnPCoreOnline" command on Package Manager console of Visual Studio. Once installed, the references and packages will be imported to the solution.
The references used in the sample are given below.
- Microsoft.SharePoint.Client
- OfficeDevPnP.Core
Connect to SharePoint Online site
The Authentication Manager is used to retrieve the client context of the site. To connect to SharePoint Online site, the token, given below is used.
- GetSharePointOnlineAuthenticatedContextToken
The parameters required are.
- SharePoint Online site URL
- Tenant UserId
- Tenant Password (or secured string)
Add Site Collection Admins
Users can be added as site collection admins to SharePoint online sites, using PnP Core component. The following steps explain the process in detail.
- Input the site detail, user details for authentication, and site collection admin information.
- Authenticate and get the client context of the site and then the necessary Web object.
- Using Web object, add the users using AddAdministrators method. The required parameters are given below.
- User entities
- Owner Permissions (Boolean)
- Display the results.
The code snippet, given below, shows the logic.
-
- string siteUrl = "https://nakkeerann.sharepoint.com/";
- string userName = "[email protected]";
- string password = "***";
-
-
- AuthenticationManager authManager = new AuthenticationManager();
- try
- {
-
-
- using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
- {
-
- List<UserEntity> admins = new List<UserEntity>();
- UserEntity admin = new UserEntity();
- admin.LoginName = "nirmal";
- admins.Add(admin);
-
-
-
- clientContext.Site.RootWeb.AddAdministrators(admins, true);
-
- Console.WriteLine("User added as Site Collection Admin");
- Console.ReadKey();
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Error Message: " + ex.Message);
- Console.ReadKey();
- }
Retrieve Site Collection Administrators
Site collection admin details can be accessed from SharePoint Online sites, using PnP Core component. The steps, given below, explain the process in detail.
- Input the site detail, user details for authentication.
- Authenticate and get the client context of the site and then the necessary Web object.
- Using Web object, retrieve the users using GetAdministrators method.
- Display the results.
The code snippet, given below, shows the logic.
-
- string siteUrl = "https://nakkeerann.sharepoint.com/";
- string userName = "[email protected]";
- string password = "***";
-
-
- AuthenticationManager authManager = new AuthenticationManager();
- try
- {
-
-
- using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
- {
-
- List<UserEntity> admins = clientContext.Site.RootWeb.GetAdministrators();
-
- foreach (UserEntity admin in admins)
- {
- Console.WriteLine("Admin Name : " + admin.Title);
- }
- Console.ReadKey();
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Error Message: " + ex.Message);
- Console.ReadKey();
- }
The below snapshot shows the administrators page.
Summary
Thus, you have learned retrieving and adding users from/to site collection administrators group, using PnP Core CSOM library components. The above operations are compatible with SharePoint Online sites.