Group Operations In SharePoint 2016 Using PnP Core CSOM Library

PnP stands for Practices and Patterns, which is a community driven open source project, where Microsoft and community members have created an implementation pattern for Office 365 and SharePoint On-premises. One of the branches of the PnP development is in PnP Core CSOM Library.

PnP Core library provides CSOM extension methods for SharePoint 2016 add-in model development. Official documentation can be accessed from here. PnP Core library increases the productivity of the developers by abstracting the complex operations. In this article, we will see, how to use PnP core library on a remote desktop and work with SharePoint 2016, using a console Application.

In order to work with PnP Core library, we first have to install the NuGet Package Manager, which is explained in this article,

Once the PnP Core Library is added, we can kick of the implementation, using a console Application.

Project structure

Create a console Application and add the references, given below:

  • Microsoft.SharePoint.Client;
  • OfficeDevPnP.Core;

Scope of the article will be to perform the below group operations, using PnP Core CSOM Library:

  • Creation of the group.
  • Check if the group exists.
  • Delete the group.

The code blocks for each operation will be given in the sub heading ‘Full Code’. It can be interchangeably placed within the Main function to test the working.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Microsoft.SharePoint.Client;  
  7. using OfficeDevPnP.Core;  
  8. namespace OfficeDevPnP  
  9. {  
  10.     class SP2016  
  11.     {  
  12.         static void Main(string[] args) {  
  13.             //Place Internal Implementation code here  
  14.         }  
  15.     }  
  16. }  
Internal Implementation

Let’s see how to work with PnP Core CSOM Library to perform the group operations in SharePoint 2016.

Create Group

Group creation can be implemented, using the PnP Core Extension method, as described below:  
  • Create an instance of the authentication manager, which will be used to create the client context.
    1. //Get instance of Authentication Manager  
    2. OfficeDevPnP.Core.AuthenticationManager authenticationManager = new OfficeDevPnP.Core.AuthenticationManager();  
    3. //Create authentication array for site url,User Name,Password and Domain  
    4. string[] authArray =   
    5.   {  
    6.     "http://sharepoint2016/sites/HOL",  
    7.     "Priyaranjan",  
    8.     "password-1",  
    9.     "SharePointHOL"  
    10. };  
  • Create Client Context by passing the authentication details to the authentication manager object.
    1. var clientContext = authenticationManager.GetNetworkCredentialAuthenticatedContext(authArray[0], authArray[1],authArray[2], authArray[3])  
  • Create the list, using the PnP extension method ‘AddGroup’.The parameters for the method are:

    • Group Name.
    • Group Description.
    • Group Is Owner (bool): Sets the created group as the owner of the security group.
    1. var groupDescription = "This group handles Blue Yonder Airlines payroll list permissions";  
    2. clientContext.Site.RootWeb.AddGroup("Payroll", groupDescription,true);  
    Output: After running the PnP CSOM script we get the success message in the console.

    Output

    We can check out the group in the People and Groups page.

    Output

    Full Code: The full code for creating the group, using PnP extension method is as follows:
    1. //Get instance of Authentication Manager  
    2. OfficeDevPnP.Core.AuthenticationManager authenticationManager = new OfficeDevPnP.Core.AuthenticationManager();  
    3. //Create authentication array for site url,User Name,Password and Domain  
    4. string[] authArray = {  
    5.     "http://sharepoint2016/sites/HOL",  
    6.     "Priyaranjan",  
    7.     "password-1",  
    8.     "SharePointHOL"  
    9. };  
    10. try {  
    11.     //Create the client context  
    12.     using(var clientContext = authenticationManager.GetNetworkCredentialAuthenticatedContext(authArray[0], authArray[1], authArray[2], authArray[3])) {  
    13.         //create the new group using group creation information  
    14.         var groupDescription = "This group handles Blue Yonder Airlines payroll list permissions";  
    15.         clientContext.Site.RootWeb.AddGroup("Payroll", groupDescription, true);  
    16.         Console.WriteLine("Payroll Group has been created successfully");  
    17.         Console.ReadLine();  
    18.     }  
    19. catch (Exception ex) {  
    20.     Console.WriteLine("Exception : " + ex.Message);  
    21. }  

Check if Group Exists

We can check if the group exists in the site, using PnP extension method ‘Group Exists’. The extension method accepts the group name as the parameter,

  1. var groupExists = clientContext.Site.RootWeb.GroupExists("Payroll") ;   
Output: Upon running the group check script, we get the Boolean response in the console as shown below:
Output

Full Code: The full code for checking the existence of a group is shown below:
  1. //Get instance of Authentication Manager  
  2. OfficeDevPnP.Core.AuthenticationManager authenticationManager = new OfficeDevPnP.Core.AuthenticationManager();  
  3. //Create authentication array for site url,User Name,Password and Domain  
  4. string[] authArray = {  
  5.     "http://sharepoint2016/sites/HOL",  
  6.     "Priyaranjan",  
  7.     "password-1",  
  8.     "SharePointHOL"  
  9. };  
  10. try {  
  11.     //Create the client context  
  12.     using(var clientContext = authenticationManager.GetNetworkCredentialAuthenticatedContext(authArray[0], authArray[1], authArray[2], authArray[3])) {  
  13.         //create the new group using group creation information  
  14.         var groupExists = clientContext.Site.RootWeb.GroupExists("Payroll");  
  15.         Console.WriteLine("Payroll Group Exists ? : " + groupExists);  
  16.         Console.ReadLine();  
  17.     }  
  18. catch (Exception ex) {  
  19.     Console.WriteLine("Exception : " + ex.Message);  
  20. }  
Delete existing group

PnP Core Component provides the extension method to delete the group, which can be invoked by calling the method ‘RemoveGroup’. It accepts the group name as the parameter.
  1. clientContext.Site.RootWeb.RemoveGroup("Payroll") ;   
Output: Upon running the script to delete the group, we get the successful group deletion message, as shown below:

Output

I had the group page opened in the Browser, prior to running the script. Upon refreshing the page post running the script, it throws ‘Group cannot be found’ error. This ensures the group has been deleted from the site groups.

Output
  1. //Get instance of Authentication Manager  
  2. OfficeDevPnP.Core.AuthenticationManager authenticationManager = new OfficeDevPnP.Core.AuthenticationManager();  
  3. //Create authentication array for site url,User Name,Password and Domain  
  4. string[] authArray = {  
  5.     "http://sharepoint2016/sites/HOL",  
  6.     "Priyaranjan",  
  7.     "password-1",  
  8.     "SharePointHOL"  
  9. };  
  10. try {  
  11.     //Create the client context  
  12.     using(var clientContext = authenticationManager.GetNetworkCredentialAuthenticatedContext(authArray[0], authArray[1], authArray[2], authArray[3])) {  
  13.         //delete the new group   
  14.         clientContext.Site.RootWeb.RemoveGroup("Payroll");  
  15.         Console.WriteLine("New group has been Deleted.");  
  16.         Console.ReadLine();  
  17.     }  
  18. catch (Exception ex) {  
  19.     Console.WriteLine("Exception : " + ex.Message);  
  20. }  
Summary

You can copy and paste the ‘Full Code’ fragment into the Main() of the console Application of the project structure, given at the starting of the article to test it at your end. Thus, we have seen, how to perform the SharePoint group operations on a remote desktop, using PnP Core library. In this method, you don’t have to get into the Server where SharePoint 2016 is installed and it enables us to work on a remote desktop, using the efficient and lightweight PnP Core Component Library.