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
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.
// Input Parameters
string siteUrl = "https://nakkeerann.sharepoint.com/";
string userName = "nav@nakkeerann.onmicrosoft.com";
string password = "***";
// PnP component to set context
AuthenticationManager authManager = new AuthenticationManager();
try
{
// Get and set the client context
// Connects to SharePoint online site using inputs provided
using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
{
// Input Parameters
List<UserEntity> admins = new List<UserEntity>();
UserEntity admin = new UserEntity();
admin.LoginName = "nirmal";
admins.Add(admin);
// Multiple users can be added as admins with the help of above list
// Adds Site Collection Admins
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.
// Input Parameters
string siteUrl = "https://nakkeerann.sharepoint.com/";
string userName = "nav@nakkeerann.onmicrosoft.com";
string password = "***";
// PnP component to set context
AuthenticationManager authManager = new AuthenticationManager();
try
{
// Get and set the client context
// Connects to SharePoint online site using inputs provided
using (var clientContext = authManager.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
{
// Gets Site Collection Admins
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.
Comments