Check In/Check Out Documents 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 create 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 complex operations. In this article, we will see, how to set up PnP core library remotely 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 PnP Core Library is added, we can kick off 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 operations, given below, using PnP Core CSOM Library:

  • Check out the document
  • Check in the document

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 document Check-in and Check-out in SharePoint 2016.

Checkout the document

Document check out can be implemented, using 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 = { "http://sharepoint2016/sites/HOL""Priyaranjan","password-1","SharePointHOL" };  
  • 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])  
  • Check out the document, using the PnP extension method “CheckOutFile”.The method takes the site relative URL of the document as the parameter.
    1. clientContext.Web.CheckOutFile("/sites/HOL/Shared Documents/Add user to Group Using Nintex.docx")  

Output

Upon running the script, the specified file will be checked out and a success message comes in the console.

Output

Going to the library, we can see the checked out document.

Output

Full Code

The full code to check out the file, using PnP Extension method is shown below. Place the code within the main function of the console Application to get it running.

  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.         //Get the web object from client context and check out the file  
  14.         clientContext.Web.CheckOutFile("/sites/HOL/Shared Documents/Add user to Group Using Nintex.docx");  
  15.         Console.WriteLine("The File has been checked out.");  
  16.         Console.ReadLine();  
  17.     }  
  18. catch (Exception ex) {  
  19.     Console.WriteLine("Exception : " + ex.Message);  
  20. }  
Check In file

The checked-out file can be checked in, using the PnP extension method ‘CheckInFile’. This method takes in the parameters.  
  • Checked out file Server relative URL.
  • Check in Type (MinorCheckIn, MajorCheckIn, OverwriteCheckIn).
  • Check in comment.
    1. clientContext.Web.CheckInFile("/sites/HOL/Shared Documents/Add user to Group Using Nintex.docx", CheckinType.MajorCheckIn,"Checked in") ; 

Output

Upon running the script, the checked out file will be checked in and a success message comes in the console.

Output

Going back to the list, we can verify that the document has been checked in.

Output

Full Code

The full code to check in a checked out document is shown below. Place the code within the main function of the console Application to get it running.

  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. };  
  11. Try {  
  12.     //Create the client context  
  13.     using(var clientContext = authenticationManager.GetNetworkCredentialAuthenticatedContext(authArray[0], authArray[1], authArray[2], authArray[3])) {  
  14.         //Get the web object from client context and check out the file  
  15.         clientContext.Web.CheckInFile("/sites/HOL/Shared Documents/Add user to Group Using Nintex.docx", CheckinType.MajorCheckIn, "Checked in");  
  16.         Console.WriteLine("The File has been checked in.");  
  17.         Console.ReadLine();  
  18.     }  
  19. catch (Exception ex)  
  20. {  
  21.     Console.WriteLine("Exception : " + ex.Message);  
  22. }  
Summary

You can copy 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 out from your end. Thus, we have seen, how to perform the SharePoint Check-Out and Check-In 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 enables us to work on a remote desktop, using the efficient and lightweight PnP Core Component Library.

Up Next
    Ebook Download
    View all
    Learn
    View all