3

Does anyone have an example of Get-pnpweb with connection parameter please? I'm trying to get to a different web in my script and I believe the only way to achieve this is using this parameter, but I can't seem to find an example.

Thanks Ova

3 Answers 3

4

Connection needs to be returned from PnP command when you connect to a site/web you can implement this as below.

#Web 1 already connected
Connect-PnPOnline -Url "https://mytenant.sharepoint.com/sites/web1"  -Credentials $creds -ReturnConnection

#Connecting Web2 with connection
$connection = Connect-PnPOnline -Url "https://mytenant.sharepoint.com/sites/web2"  -Credentials $creds -ReturnConnection

#get Web2 using connection
$webs = Get-PnPWeb -Connection $connection
2
  • I need to connect 2 other webs(web2 and web3) in my script to add group of web1 to them, just thinking if it's best practice to throw so many connections around?
    – ova
    Commented Sep 18, 2020 at 8:36
  • You can dispose the connection after done with it using Disconnect-PnPOnline -Connection $connection Commented Sep 18, 2020 at 9:33
2

While I think this works, you might want to look into Set-PnPContext that I've added later in my answer.

$connectionA = Connect-PnPOnline -Url https://siteA.com -CurrentCredentials -ReturnConnection

$connectionB = Connect-PnPOnline -Url https://siteB.com -CurrentCredentials -ReturnConnection

Get-PnPWeb -Connection $connectionA
Get-PnPWeb -Connection $connectionB

Set-PnPContext

Description

Sets the Client Context to use by the cmdlets, which allows easy context switching. See examples for details.

Connect-PnPOnline -Url $siteAurl -Credentials $credentials
$ctx = Get-PnPContext
Get-PnPList # returns the lists from site specified with $siteAurl
Connect-PnPOnline -Url $siteBurl -Credentials $credentials
Get-PnPList # returns the lists from the site specified with $siteBurl
Set-PnPContext -Context $ctx # switch back to site A
Get-PnPList # returns the lists from site A
1
  • do you know if the command closes the connection after being used as in the first example? $web2= Connect-PnPOnline -Url $url -Credentials $Cred Set-PnPGroup -Identity $owners -AddRole "Read" -connection $web2 I need to run something like this and wondering if it will close the connection automatically.
    – ova
    Commented Sep 18, 2020 at 8:42
0

Many/most/all PnP SharePoint cmdlets do seem to require that the web you're working with match the one last referenced in the call to Connect-PnPOnline. Making two Connect calls and assigning each to a var does not seem to work for context switching between webs. Explicitly setting the Connection parameter in a call may still be a good idea to help trigger an error when a connection isn't available - the cmdlet does then seem to always return a null and the cmdlet will fail.

Hope this helps! -e.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.