[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:
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 |
|
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!