Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Huuhaa500 opened this Issue Nov 29, 2017 · 19 comments

Comments

Projects
None yet
9 participants
@Huuhaa500
Copy link

Huuhaa500 commented Nov 29, 2017

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

This comment has been minimized.

Copy link
Collaborator

erwinvanhunen commented Dec 5, 2017

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).

@Huuhaa500

This comment has been minimized.

Copy link
Author

Huuhaa500 commented Dec 5, 2017

@steven-de-bruyne

This comment has been minimized.

Copy link

steven-de-bruyne commented Dec 6, 2017

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

This comment has been minimized.

Copy link

steven-de-bruyne commented Dec 6, 2017

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

@Huuhaa500

This comment has been minimized.

Copy link
Author

Huuhaa500 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

This comment has been minimized.

Copy link
Contributor

wobba commented Jun 21, 2018

@Huuhaa500 Is this still an issue?

@DanielHTpg

This comment has been minimized.

Copy link

DanielHTpg commented Sep 14, 2018

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

This comment has been minimized.

Copy link

DanielHTpg commented Sep 14, 2018

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.

if (SPOnlineConnection.CurrentConnection.Context.Url != SPOnlineConnection.CurrentConnection.Url)
{
SPOnlineConnection.CurrentConnection.RestoreCachedContext(SPOnlineConnection.CurrentConnection.Url);
}
web = ClientContext.Web;

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

Url = (new Uri(url)).AbsoluteUri;

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.

public void RestoreCachedContext(string url)
{
Context = ContextCache.FirstOrDefault(c => HttpUtility.UrlEncode(c.Url) == HttpUtility.UrlEncode(url));
}

So "HttpUtility.UrlEncode(url)" will encode an already encoded URL messing it up and than "ContextCache.FirstOrDefault" will return "null" resetting the context.

SPOnlineConnection.CurrentConnection.Context.ExecuteQueryRetry();

Would than throw the NullReferenceException

@wobba

This comment has been minimized.

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

This comment has been minimized.

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:

if (SPOnlineConnection.CurrentConnection.Context.Url != SPOnlineConnection.CurrentConnection.Url)
{
SPOnlineConnection.CurrentConnection.RestoreCachedContext(SPOnlineConnection.CurrentConnection.Url);
}

if (SPOnlineConnection.CurrentConnection.Context.Url != SPOnlineConnection.CurrentConnection.Url)
{
SPOnlineConnection.CurrentConnection.RestoreCachedContext(SPOnlineConnection.CurrentConnection.Url);
}

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

public void RestoreCachedContext(string url)
{
Context = ContextCache.FirstOrDefault(c => HttpUtility.UrlEncode(c.Url) == HttpUtility.UrlEncode(url));
}
internal void CacheContext()
{
var c = ContextCache.FirstOrDefault(cc => HttpUtility.UrlEncode(cc.Url) == HttpUtility.UrlEncode(Context.Url));
if (c == null)
{
ContextCache.Add(Context);
}
}
public ClientContext CloneContext(string url)
{
var context = ContextCache.FirstOrDefault(c => HttpUtility.UrlEncode(c.Url) == HttpUtility.UrlEncode(url));
if (context == null)
{
context = Context.Clone(url);
ContextCache.Add(context);
}
Context = context;
return context;
}
internal static ClientContext GetCachedContext(string url)
{
return ContextCache.FirstOrDefault(c => HttpUtility.UrlEncode(c.Url) == HttpUtility.UrlEncode(url));
}

@wobba

This comment has been minimized.

Copy link
Contributor

wobba commented Sep 17, 2018

@DanielHirschTpg care to submit a PR on this?

@DanielHTpg

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

Copy link

firozozman commented Sep 28, 2018

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

@sachinwalunjb4

This comment has been minimized.

Copy link

sachinwalunjb4 commented Oct 23, 2018

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

Copy link

Frost-on-Web commented Mar 5, 2019

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

This comment has been minimized.

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 Mar 5, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.