Skip to content

[PnP PowerShell] Set a PnPContext to SharePoint On-Premises

PnP Powershell is really useful for scripting stuff, we know that. It simplifies SharePoint objects manipulations, whether it's about Web, List, User,... In a way that we prefer using commands that those objects.

But what about working with On-Premises environment? I don't know for you, but I got some trouble when I wanted to authenticate to a 2013 environment as I was site collection administrator. Probably because I was facing some proxy / federated authentication in my context.

Below some errors met:

PnP PowerShell -CurrentCredential error

"Object reference not set to an instance of an object"

PnP PowerShell -CurrentCredential GetCredential error

"An error occurred while sending the request"

Then I remembered that it was possible to authenticate explicitly to On-Premises environment with the -TransformationOnPrem and -CurrentCredential arguments available in the Connect-PnPOnline command. As stated, the first argument is dedicated to "modernize" pages, webparts,... Nothing more (we get a 403 Not Allowed error when trying to get current site info for example). But even if I'm not allowed to query anything else, I still have a ClientContext initiated, so I should be able to overwrite it!

As I'm working on a migration from On-Premises to Online, I had a couple of scripts to test / run on both environments, which explains why I got this "idea".

As I didn't want to write down all the CSOM with PowerShell (you know, "assigning an object", "loading the object", "running the query",...), I found out that it was possible to replace the initiated context by another one that could be created with CSOM!

Warning

This trick was tested with version 1.12 of PnP PowerShell and SharePoint 2013. Of course, because of the scope of the module, some commands might not work as expected. In my case, I've tested some basic ones related to lists or site context.

Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$siteUrl = "https://portal.contoso.com"

# Start by authenticate to your On-Premises site collection
# The command will import the SharePoint CSOM objects
# Like this, you'll be able to consume those without explicitly importing DLLs 😉
Connect-PnPOnline -Url $siteUrl -TransformationOnPrem -CurrentCredential

# Now let's assume that your Windows credentials are stored in the Credentials Manager
# In that case, the username will be stored like this: "domain\user"
$credentials = Get-PnPStoredCredential -Name WindowsCredentials

# We're setting a new ClientContext object through CSOM
# and use NetWorkCredential instead of SharePointOnlineCredentials
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$ctx.Credentials = New-Object System.Net.NetworkCredential($credentials.UserName, $credentials.Password)

# Link the previously obtained context to PnP PowerShell
Set-PnPContext -Context $ctx

# Try some command
Get-PnPWeb

# Hurray!

Like this, you can take advantage of the most common PnP PowerShell commands on On-Premises environment and avoid to write everything like if you were on a C# console app.

Happy coding!