Introduction to PnP Core Component

Microsoft has introduced a new Client side APIs from SharePoint 2010 and it opens a much easier way to interact with SharePoint object than existing web services. The continuous improvement in client side development and the introduction of App model (now, App is renamed as Add-In) takes the SharePoint 2013 to another level. Then onwards Microsoft is also boosting the developers to move on to client side development.

In parallel, SharePoint online is also boosted due to the availability of new client side APIs and App / Add-In architecture. During that time, the need arises on transforming the SharePoint full trust code solutions to Add-In model for the office 365 environment. The Patterns and Practices team formed and they were involved in that transforming process by creating PnP component APIs and also with guidance.

The PnP components are developed as an open source extension as a part of Office 365 Developer Pattern and Practices deliverables. This component is built on top of SharePoint CSOM and Rest API managed codes. By having the references in our solution, that increases our efficiency in developing the client side add-in or applications.

The source code, sample solutions, guidance documentation, case studies are hosted in GitHub and it is open to all for any type of contributions. By the helpful community contributions, the components are gradually increased by merging more methods and operations to its API. This package is released every month as a part of PnP deliverables.

The assemblies from PnP Core Component will be updated periodically based on community contributions and new versions will be released as part of PnP releases.

The latest and previous versions of assemblies are available as a Nuget package to use in our Visual Studio solutions.

Key Features:

  • Simplify remote development for any managed code from any provider hosted add-in or windows code.

  • Console application or azure web jobs take advantage of CSOM by connecting SharePoint online and On-premise to modify the sites or provision the sites.

  • Relatively fast compared to normal way.

  • Reduced in number of code lines to achieve a particular task.
Key Capabilities

PnP core component provides reusable methods and guidance patterns that help us to integrate better solution to the SharePoint online / on-premise environment by having the below capabilities.

Extension Methods

Extension methods enable you to “add” methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type. – MSDN

PnP core component uses this feature and extends the numerous amount of methods to simplify and increase productivity by obstructing the complex coding blocks from native CSOM.

Extension methods from PnP core component are built on top of native CSOM and that help us to access the SharePoint object using one line of code by replacing large code blocks.

Comparison

 Native CSOM Code:
  1. //Code to add a new list to web using native CSOM  
  2. Web web = context.Web;  
  3. ListCreationInformation listInfo = new ListCreationInformation();  
  4. listInfo.Title = “WithoutPnP”;  
  5. listInfo.TemplateType = (int)ListTemplateType.GenericList;  
  6. List list1 = web.Lists.Add(listInfo);  
  7.    
  8. list1.Update();  
  9. context.ExecuteQuery();  
 PnP Extension Method: 
  1. //Code to add a new list to web using PnP extension method  
  2. var list2 = context.Web.CreateList(ListTemplateType.GenericList, "WithPnP"false);  
Reference: PnP Core Component – Basic Operations

Remote Timer Jobs

Timer jobs are synchronous jobs to run long operations and it should be deployed as Full trust code solutions in SharePoint and this supports only in on premise environment. There is no support for custom SharePoint timer jobs in SharePoint online or from client side environment.

To run the long operation job remotely and overcome the disadvantage of on premise timer job feature in online environment, PnP core components bring this Remote Timer Job capability to manipulate the SharePoint sites and its object remotely on SharePoint online and on-premise environment.

This framework also helps to authenticate the SharePoint sites remotely. The remote time jobs can run on azure environment or from windows services. No need of Farm solutions based timer jobs. No need of running the timer job on every site collection.

Comparison

Timer Jobs vs Remote Timer Jobs
                                          Figure 1: Timer Jobs vs Remote Timer Jobs
Reference: PnP Core Component – Remote Timer Job Framework

Authentication Manager

There area different number of versions in SharePoint and each of them may be configured to different authentication methods. To authenticate each environment from different application requires different type of codes with different reference to namespaces / classes.

The PnP Authentication manager from PnP core library bundles various authentication methods to use against different SharePoint environments (SharePoint Online, SharePoint 2013, SharePoint 2016). So we can use this single Authentication manager object to connect to different SharePoint versions from any add-in or application.

Comparison

Native CSOM Code:
  1. //Native CSOM code to authenticate the SharePoint online site  
  2. Var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);  
  3. Using(var clientContext = spContext.CreateUserClientContextForSPHost())  
  4. //your code }  
PnP Authentication Manager Code:
  1. //PnP Authentication Manage code to authenticate the SharePoint online site  
  2. ClientContext cc = new AuthenticationManager().GetSharePointOnlineAuthenticatedContextTenant(siteUrl, username, password);  
Reference: PnP Core Component – Authentication Manager

PnP provisioning Engine

Before the introduction of PnP provision engine, we might have used feature framework, web templates, site templates and site definition to provision sites and site collections. To do this we have to use full trust code solution or sandboxed solution to add the assets required for branding by the help of feature framework.

PnP core components have the capability to create custom apps for SharePoint that can provision site branding and perform other site provisioning tasks by the help of remote provisioning pattern. Which helps to apply the branding on any sites in SharePoint and it doesn’t require to use the server based features or solutions to deploy the assets for branding.

PnP site provisioning engine remotely extracts the configurations from SharePoint site (site columns, lists, settings, Languages, regions and additional settings etc..) and generates the template in xml format. Imports the xml to any site to implement the same configurations and it helps to really implement more dynamic assets and configurations to the SharePoint sites.

It doesn’t concentrate on content within a site. This template is reusable and can deploy to any number of sites. Updates can be done without any down time to the sites.
 
Reference: PnP Core Component – Site Provisioning Framework

PnP PowerShell cmdlets

If you are more on the PowerShell and you want to modify the SharePoint, you can take the PowerShell cmdlets from PnP core component. It extends the PowerShell cmdlet, which are already provided in native PowerShell commands

This is used to manage the artifacts in sites (Lists, Views, Fields, etc..), but SharePoint online management PowerShell cmdlets do the administrative tasks like creating sites, removing sites, adding users, etc.

Comparison

Native CSOM PowerShell cmdlet 
  1. //To get the context  
  2. $url = "https://mycompany.sharepoint.com"  
  3. $creds = Get-Credential -Message "Enter Online Credential"  
  4.    
  5. $O365Credential = New-Object Microsoft.SharePoint.Client.SharePointOnline.Credentials($creds.UserName, $creds.Password)  
  6.    
  7. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url);  
  8. $ctx.Credentials = $O365Credential  
PnP PowerShell cmdlet
  1. //To get the context  
  2. $url = "https://mycompany.sharepoint.com"  
  3. Connect-SPOnline -Url $url  
  4. $ctx = Get-SPOContext  

PnP core components support three version of SharePoint and all version of assemblies available in Nuget Package. The below table shows the information about each version,

SharePoint Edition Version Nuget Package Name
SharePoint 2013 15 SharePointPnPCore2013
SharePoint 2016 16 SharePointPnPCoreOnline
SharePoint Online 16.1 SharePointPnPCore2016

Open Visual StudioFollow the below steps to add any of the CSOM extension to the Visual Studio solutions,

  • Click File > New > Project.

  • Select any VS template and click OK to create a new visual studio Project.

  • Select Tools from menu.

  • Select Nuget Package Manager > Manage Nuget Packages for Solution.
Nuget Package Manager
                           Figure 2: Nuget Package Manager
  • Select Browse tab from Nuget – Solution window.

  • In the search box, type SharePointPnP. That will search the PnP core components from Nuget packages and pull down the result to you.

  • From the three extension options, click one of the CSOM extension for which environment, your application targeting. The select populates the opened VS solution on right side.

  • Select the Solution from right side and click “Install” button to add the package to the Visual Studio solution.
  • Nuget - Solution WindowFigure 3: Nuget – Solution WindowClick OK to add dependency references in pop window.
  • Click I Accept to accept the dependency licenses to add those references to the Solution.

  • Core assembly and its dependent assembles are added to the VS solution.
Conclusion

It is open source extensions built on top of native SharePoint CSOM and REST API for managed code. It extends the  typical set of APIs which are available within the client side object model and within REST. That helps to develop easy solutions on building in our own solutions on top of SharePoint online or in on premises. This PnP core component also provides the extensions for Office 365, Microsoft Graph, etc.