Introduction to PnP Core Component

This blog gives you the basics about PnP core component and its different key capabilities. And also explains how to add this component to your solution.

Microsoft introduces 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 also boosting the developers to move on to client side development.

In parallel, SharePoint online 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 involved in that transforming process by creating PnP component APIs and also with the 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 help community contributions, the components are gradually increases by merging more methods and operations to its API. This package is release by 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 releases 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 taking advantage of CSOM by connecting SharePoint online and On-premise to modifying the sites or provisioning 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:

[code lang=”csharp”]
//Code to add a new list to web using native CSOM
Web web = context.Web;
ListCreationInformation listInfo = new ListCreationInformation();
listInfo.Title = “WithoutPnP”;
listInfo.TemplateType = (int)ListTemplateType.GenericList;
List list1 = web.Lists.Add(listInfo);
 
list1.Update();
context.ExecuteQuery();
[/code]

PnP Extension Method:

[code lang=”csharp”]
//Code to add a new list to web using PnP extension method
var list2 = context.Web.CreateList(ListTemplateType.GenericList, "WithPnP", false);
[/code]

Useful 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
Useful Reference:

PnP Core Component – Remote Timer Job Framework

Authentication Manager:

There are 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:

[code lang=”csharp”]
//Native CSOM code to authenticate the SharePoint online site
Var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);
Using(var clientContext = spContext.CreateUserClientContextForSPHost())
{  //your code }
[/code]

PnP Authentication Manager Code:

[code lang=”csharp”]
//PnP Authentication Manage code to authenticate the SharePoint online site
ClientContext cc  = new AuthenticationManager().GetSharePointOnlineAuthenticatedContextTenant(siteUrl, username, password);
[/code]

Useful 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.

Useful 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

[code lang=”csharp”]
//To get the context
$url = "https://mycompany.sharepoint.com"
$creds = Get-Credential -Message "Enter Online Credential"
 
$O365Credential = New-Object Microsoft.SharePoint.Client.SharePointOnline.Credentials($creds.UserName, $creds.Password)
 
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url);
$ctx.Credentials = $O365Credential
[/code]

PnP PowerShell cmdlet

[code lang=”csharp”]
//To get the context
$url = "https://mycompany.sharepoint.com"
Connect-SPOnline -Url $url
$ctx = Get-SPOContext
[/code]

Useful Reference:

PnP Core Component – PnP PowerShell for scripting with Core component

SharePoint Version Support

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

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

  • Open Visual Studio
  • 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 Window
    Figure 3: Nuget – Solution Window
  • Click 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 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.

Shantha Kumar
Shantha Kumar
Articles: 278

24,849 Comments

  1. Do you have any sample code or reference step by step for Site Provisioning in SharePoint 2016 On Premises only.

  2. Is there any way that we can incorporate PnP package into an app and create Site collections ,apply branding for On premise development(Not Office 365)?

Comments are closed.