Office 365 With PNP PowerShell 2013: Base Cmdlets

This is my third blog post on the topic PnP-PowerShell 2013. In this post I am going to give clear understanding, how to make the connection and close the connection using PnP Base Cmdlets section. 


In the background the “Microsoft.SharePoint.Client.dll” is responsible for get the all results from the SharePoint. 

The value also increase of learning if you already have experience on client object Model.Basic tip from my side please read some information about the client side object model.  

As you may be know the client site object model you can’t get all the data from the server in single request. You need to identify which property you need and just use the load method to load that type of property also you can use the Get-PnpProperties Cmdlet. 

Implementation
The command that I am going to cover as mentioned below. 

These are the most common rest of others not much important in terms of PNP 2013 on premise. The numbers of command are 6.

Sr. No
Command Name
Description as PNP team
1.
Connects to a SharePoint site and creates a context that is required for the other PnP Cmdlets
2.
Returns a Client Side Object Model context
4.
Sets the Client Context to use by the cmdlets
3.
Will populate properties of an object and optionally, if needed, load the value from the server. If one property is specified its value will be returned to the output.
5.
Executes any queued actions / changes on the SharePoint Client Side Object Model Context
6.
Disconnects the context

Connect-PnPOnline: To connect with the SharePoint site there are many methods are available. I recommended, please do check the documentation first.  Depending upon your environment use that type of authentication parameters it could be window, form authentication, O365, app authentication and Azure authentication.'



In On Premise you can use the following two methods mostly.
Case 1 it will show the User name and password prompt. And case 2  the logged machine access to the site then you can use this method with CurrentCredentials switched.

Case 1: Connect-PnPOnline -Url https://contoso.sharepoint.com -Credentials (Get-Credential)
Case 2: Connect-PnPOnline -Url http://yourlocalserver CurrentCredentials
Get-PnPContext :  If you want to save the current context. This command is very handy and it returns the current context. Why you need context it might be the case you need to save the current context. By switch between two sites you need this.

$ctx = Get-PnPContext

Set-PnPContext: It is used to set the connection of the existing saved connection via Get-PnPContext command.
Set-PnPContext -Context <ClientContext>

Get‑PnPProperty:  Load the other properties of the object. By defaults these properties not included in the SharePoint Clinet Site object mode. We need to load the explicitly. Example as mentioned below.
PS:> $web = Get-PnPWeb
PS:> Get-PnPProperty -ClientObject $web -Property Id, Lists
PS:> $web.Lists

Execute-PnPQuery: This will execute the any queued actions/changes on the SharePoint Clint Object model. Also you can retry and wait parameter pass as an optional.
PS:> Execute-PnPQuery -RetryCount 5
PS:> Execute-PnPQuery -RetryWait 10

Disconnect-PnPOnline: Disconnects the context
Disconnect-PnPOnline

I am using all the above methods as we discussed/Covered.

Example Plan: I give you example to connect the site using “Connect‑PnPOnline” and save the context to using “Get‑PnPContext” command. Then use the existing context with the help of “Set‑PnPContext” command and read other properties. And use the  using this command “Execute‑PnPQuery” method to get the some of the important properties. At the end I am close the connect using “Disconnect‑PnPOnline” command. 


Here is the example 1
#Please make sure you have installed all the powershell package.
Import-Module 'C:\Users\basantp\Documents\PNP Blog Section\Code\SharePointPnPPowerShell2013\SharePointPnP.PowerShell.2013.Commands.dll';
try {
Set-PnPTraceLog -On -LogFile 'C:\Users\basantp\Documents\PNP Blog Section\Code\traceoutput.txt' -Level Error -Delimiter ","
$Url1 = "https://sharepoint-xxx.com/xx/xxx"
Connect-PnPOnline -Url $Url1 -CurrentCredentials -ErrorAction Stop
Write-host "Site connected".
$Context= Get-PnPContext # Connect and store the context
$Web = $Context.Site.RootWeb
$Context.Load($Web)
$Web.Title #You will not find any output in your console.
Execute-PnPQuery #Once you use Execute-PnpQuery you will find the properties.
$Web.Title #Title Will be Print
}
catch {
Write-Host "Site not found"
}
view raw BaseCmdlets.ps1 hosted with ❤ by GitHub



Here is the example 2

Import-Module 'C:\Users\basantp\Documents\PNP Blog Section\Code\SharePointPnPPowerShell2013\SharePointPnP.PowerShell.2013.Commands.dll';
try {
Set-PnPTraceLog -On -LogFile 'C:\Users\basantp\Documents\PNP Blog Section\Code\traceoutput.txt' -Level Error -Delimiter ","
$Url1 = "<SiteURL>"
$Url2 = "<SiteURL>"
Connect-PnPOnline -Url $Url1 -CurrentCredentials -ErrorAction Stop
Write-host "Site $Url1 connected".
$Context1= Get-PnPContext
#Get the list of $url1
# if you https://github.com/SharePoint/PnP-PowerShell/blob/master/Documentation/SetPnPContext.md
# if you simply write Get-PnPList, you will get the error as mentioned below
# The collection has not been initialized. It has not been requested or the request has not been executed.
# It may need to be explicitly requested.
$list =Get-PnPList
write-host "Total number of list exist in this site $($list.Length)"
Connect-PnPOnline -Url $Url2 -CurrentCredentials -ErrorAction Stop
Write-host "Site $Url2 connected".
#Get the list of $url2
$Context2= Get-PnPContext
$list2 =Get-PnPList
write-host "Total number of list exist in this site $($list2.Length)"
Set-PnPContext -Context $Context1 # switch back to context1
$items = Get-PnPListItem -List "User Information List"
write-host "Total number of Item exist in the user information list $($items.Length)"
Set-PnPContext -Context $Context2 # switch back to context2
$items2 = Get-PnPListItem -List "User Information List"
write-host "Total number of Item exist in the user information list $($items2.Length)"
}
catch {
Write-Host "Site not found"
}

Here is the Example 3

Import-Module 'C:\Users\basantp\Documents\PNP Blog Section\Code\SharePointPnPPowerShell2013\SharePointPnP.PowerShell.2013.Commands.dll';
try {
Set-PnPTraceLog -On -LogFile 'C:\Users\basantp\Documents\PNP Blog Section\Code\traceoutput.txt' -Level Error -Delimiter ","
$Url1 = "<URL>"
Connect-PnPOnline -Url $Url1 -CurrentCredentials -ErrorAction Stop
Write-host "Site $Url1 connected".
$web =Get-PnPWeb
Get-PnPProperty -ClientObject $web -Property Id
"Only print the ID : $($web.Id)"
"IsMultilingual value is : $($web.IsMultilingual)"
"List Count: $($web.Lists.Count)"
#This value not going to be print becase this value not loaded by the context using Get-PnPProperty method
Get-PnPProperty -ClientObject $web -Property Lists , IsMultilingual
"Only print the title : $($web.Title)" #Title is the default property
"IsMultilingual value is : $($web.IsMultilingual)"
"List Count: $($web.Lists.Count)"
catch {
Write-Host "Site not found"
}
finally{
Disconnect-PnPOnline
}



Comments

Popular posts from this blog

SharePoint RPC Protocols Examples Using OWSSVR.DLL

Query suggestions in SP 2013 Using Rest API (/_api/search/suggest)

STS CryptographicException Error : Key set does not exist