Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Connect-PnPOnline -lose connection in case there is whitespaces in web url #1250

Closed
1 of 5 tasks
tpertila opened this issue Nov 29, 2017 · 19 comments
Closed
1 of 5 tasks
Assignees

Comments

@tpertila
Copy link

Reporting an Issue or Missing Feature

Make connect with
Connect-PnPOnline $url -UseWebLogin
where
$url = "https://timopertila.sharepoint.com/sites/wiki/Light%20Beer/"

You can make one operation, but after that connection is lost and you get exception.

Like:
$contentType = Get-PnPContentType -Web $web -List Documents -Identity "Timo Doc"
is fine, but after that running same (or any else operation) like
$contentType = Get-PnPContentType -Web $web -List Documents -Identity "Timo Doc"

gives you:
Get-PnPContentType : Object reference not set to an instance of an object.
At line:2 char:16

  • ... ntentType = Get-PnPContentType -Web $web -List Documents -Identity "T ...
  •             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : NotSpecified: (:) [Get-PnPContentType], NullReferenceException
    • FullyQualifiedErrorId : System.NullReferenceException,SharePointPnP.PowerShell.Commands.ContentTypes.GetContentType

If i do same thing with site url having no whitespaces, then everything works fine.

Expected behavior

Excpected that i can run more than one command after establishing connection.

Actual behavior

pnp bug

Steps to reproduce behavior

can seen on screenshot above

Which version of the PnP-PowerShell Cmdlets are you using?

  • PnP PowerShell for SharePoint 2013
  • PnP PowerShell for SharePoint 2016
  • PnP PowerShell for SharePoint Online

What is the version of the Cmdlet module you are running?

2.20.17..

How did you install the PnP-PowerShell Cmdlets?

  • [x ] MSI Installed downloaded from GitHub
  • Installed through the PowerShell Gallery with Install-Module
  • Other means
@erwinvanhunen
Copy link
Member

Can you try this without the -Web parameter? This parameter is only required if you want to connect to a different subweb without wanting to issue a new Connect-PnPOnline.

So just use:

Connect-PnPOnline -Url $url -UseWebLogin
$contentType = Get-PnPContentType -List "Documents" -Identity "your ct name"

Optionally you can specify the -InSiteHierarchy parameter which will include content types in the root web of the site collection.

If I execute the cmdlets as shown in your screenshot I receive a different error message, related to different origins of the object. As such that's an issue to that needs to be addressed. Nevertheless, having spaces in the URL should not be a problem (and I am also not able to reproduce that).

@tpertila
Copy link
Author

tpertila commented Dec 5, 2017 via email

@steven-de-bruyne
Copy link

We have the same issue, but instead of using client credentials, we use AppID and AppSecret:
image

A Get-PnPWeb works fine. I can even run it multiple times:
image

I rebuild our PowerShell script in C# (using the OfficeDevPnP.Core) and I had no issues at all. So I assume the issue is to be found in how the context is passed from one cmdlet to the another.

While writing this, I found a workaround for the problem. Passing both the web and the connection to the cmdlets worked (middle part):
image

Regards,
Steven De Bruyne

@steven-de-bruyne
Copy link

Something else I noticed with my workaround: Switching from one cmdlet to another requires me to reopen the connection.

Example:
$code = Get-PnPPropertyBag -Web $web -Key $entityKeyValue -Connection $connection
Connect-PnPOnline -AppId $appID -AppSecret $appSecret -Url ($site.Url)
$FirstStageItems = Get-PnPRecycleBinItem -FirstStage -Connection $connection
$SecondStageItems = Get-PnPRecycleBinItem -SecondStage -Connection $connection

The example above throws an Unauthenticated exception on the Connect-PnPOnline

@tpertila
Copy link
Author

tpertila commented Dec 7, 2017

Passing the web + conn doesn't help me.

`$url ="https..."
Connect-PnPOnline $url -UseWebLogin
$web = Get-PnPWeb
$conn = Get-PnPConnection

$contentType = Get-PnPContentType -Web $web -Connection $conn -Identity "Document"
$contentType2 = Get-PnPContentType -Web $web -Connection $conn -Identity "Document"`

Everything goes fine until running Get-PnpContentType second time. Then i got error.

Maybe i should stop this and build console application using the OfficeDevPnP.Core like you did.

@wobba
Copy link
Contributor

wobba commented Jun 21, 2018

@Huuhaa500 Is this still an issue?

@DanielHTpg
Copy link

I have the same issue. I have installed via Install-Module from the gallery. Currently I have version "3.0.1808.1" installed.

I can also confirm that the error only happens if the subsite has spaces in the URL

@DanielHTpg
Copy link

I haven't debugged the cmdlets but I belive the error might be in the following code. I've also checked this theory with Get-PnPConnection and Get-PnPContext.
https://github.com/SharePoint/PnP-PowerShell/blob/25a81012962fa721ab1897e8820bbb602e4f4a59/Commands/Base/PnPWebCmdlet.cs#L59-L63

The ClientContext.Url is URL decoded. So "SPOnlineConnection.CurrentConnection.Context.Url" is URL decoded but "SPOnlineConnection.CurrentConnection.Url" is URL encoded since that value is initialized with the AbsoluteUri value here, which get's URL encoded https://github.com/SharePoint/PnP-PowerShell/blob/25a81012962fa721ab1897e8820bbb602e4f4a59/Commands/Base/SPOnlineConnection.cs#L96

So the "SPOnlineConnection.CurrentConnection.Context.Url != SPOnlineConnection.CurrentConnection.Url" condition evaluates to true and will be called and the already URL encoded URL will be passed to the function.
https://github.com/SharePoint/PnP-PowerShell/blob/25a81012962fa721ab1897e8820bbb602e4f4a59/Commands/Base/SPOnlineConnection.cs#L154-L157
So "HttpUtility.UrlEncode(url)" will encode an already encoded URL messing it up and than "ContextCache.FirstOrDefault" will return "null" resetting the context.

https://github.com/SharePoint/PnP-PowerShell/blob/25a81012962fa721ab1897e8820bbb602e4f4a59/Commands/Base/PnPWebCmdlet.cs#L66
Would than throw the NullReferenceException

@wobba
Copy link
Contributor

wobba commented Sep 14, 2018

@DanielHirschTpg good work, and feel free to submit a PR for fixing this before we get a chance to look at it further :)

@DanielHTpg
Copy link

DanielHTpg commented Sep 17, 2018

So i was able to debug this now. The issue only occures with using the web login. As suspected, the error lies in the following lines:
https://github.com/SharePoint/PnP-PowerShell/blob/25a81012962fa721ab1897e8820bbb602e4f4a59/Commands/Base/PnPWebRetrievalsCmdlet.cs#L63-L66
https://github.com/SharePoint/PnP-PowerShell/blob/25a81012962fa721ab1897e8820bbb602e4f4a59/Commands/Base/PnPWebCmdlet.cs#L59-L62

When using web login, the "SPOnlineConnection.CurrentConnection.Context.Url" is URL decoded but without the web login it's URL encoded. Since "SPOnlineConnection.CurrentConnection.Url" is always URL encoded (because of using Uri.AbsoluteUri) the IF condition evaluates TRUE and the context will be set NULL in "SPOnlineConnection.CurrentConnection.RestoreCachedContext" because of the reencoding of the passed URL encoded URL.

Generally we should compare URI instances instead of strings so we don't have this mess of double encoding or comparing encoded urls to the same unencoded one.

Also here we should use Uris for comparisons
https://github.com/SharePoint/PnP-PowerShell/blob/25a81012962fa721ab1897e8820bbb602e4f4a59/Commands/Base/SPOnlineConnection.cs#L154-L183

@wobba
Copy link
Contributor

wobba commented Sep 17, 2018

@DanielHirschTpg care to submit a PR on this?

@DanielHTpg
Copy link

DanielHTpg commented Sep 17, 2018

@wobba I only applied a "band-aid" kinda fix by comparing URI instances at the relevant positions that caused problems. The proper way would be to change the code to have Uri properties where it was strings before and generally always work with URIs. I sadly don't have anymore time to spend on this issue and to improve the code.

@wobba
Copy link
Contributor

wobba commented Sep 17, 2018

@DanielHirschTpg No problem, and we'll keep the issue open until someone have time to fix i :)

@firozozman
Copy link

I came across this same bug. This happens under WebLogin and with a URL having a space on it.

@sachinwalunjb4
Copy link

any update on this issue

This happens under WebLogin and with a URL having a space on it.
i am trying to connect online site here
any fix done or any patch release for the same ??

@wobba
Copy link
Contributor

wobba commented Oct 23, 2018

@sachinwalunjb4 this issue is still open to be fixed, and we'll close it once done.

@TKUA
Copy link

TKUA commented Jan 17, 2019

We've been faced with that problem after we moved to ADFS on an SP 2016 Server. On that configuration Connect-PnPOnline runs with switch -UseAdfs only. But then that issue appears on sites having blank spaces in their url. Almost none of the PnP commands work.
Add-PnPFile, Enable-PnPFeature, Remove-PnPNavigationNode etc.

Is there any progress on that issue?

@Frost-on-Web
Copy link

I still face the same issue.

My site URL contains a "%20" like "/sites/test-activity%20test" and PnP Powershell breaks the connection after some commands.

@wobba
Copy link
Contributor

wobba commented Mar 5, 2019

@Huuhaa500 finally got around to this one and should be included in the next release.

@wobba wobba closed this as completed Mar 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants