pnp / PnP-PowerShell

SharePoint PnP PowerShell CmdLets
https://pnp.github.io/powershell
Other
990 stars 663 forks source link

Unable to find type [Microsoft.SharePoint.Client.CollectionNotInitializedException]. #2520

Closed jprinceds closed 4 years ago

jprinceds commented 4 years ago

Hello,

We are using Azure DevOps for our Integration plateform with test automation.

In our release, we have one step which connect to Sharepoint , retrieve somme data files for the test and disconnect. Then the test run and after that we upload the result to the same Sharepoint Librairy.

But unfortunately, the 2nd attemp to Connect to Sharepoint is KO (before any other operation) with this message [error]Unable to find type [Microsoft.SharePoint.Client.CollectionNotInitializedException].

image

Steps to reproduce behavior

PS script : $cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username, $Password

Write-Host "Try Disconnecting before conenction to SharePoint Online. Site url: $($WebUrl)." Try { Disconnect-PnPOnline } catch { Write-Host "Any disconnection to make" }

Write-Host "Connecting to SharePoint Online. Site url: $($WebUrl)." Try { Write-Host "First connection try" Connect-PnPOnline -Url $WebUrl -Credentials $cred } catch { Write-Error "Upload Error, retry after 10s !" Start-Sleep -Seconds 10
Connect-PnPOnline -Url $WebUrl -Credentials $cred } Write-Host "Connected."

Log Message 2020-02-17T16:02:09.7201182Z ##[debug]00:00:01.4409489 INVOKING PowerShell Fn Get-InstalledPackage with args SharePointPnPPowerShellOnline, , , that has length 4 2020-02-17T16:02:09.7358388Z ##[debug]00:00:01.4567436 In PowerShellGet Provider - 'Get-InstalledPackage'. 2020-02-17T16:02:09.7416021Z ##[debug]00:00:01.4624736 OPTION: ProviderName => PowerShellGet 2020-02-17T16:02:09.7434378Z ##[debug]00:00:01.4643108 OPTION: Type => Module 2020-02-17T16:02:09.7452342Z ##[debug]00:00:01.4661110 OPTION: MessageResolver => Microsoft.PowerShell.PackageManagement.Cmdlets.GetMessageString 2020-02-17T16:02:09.7471676Z ##[debug]00:00:01.4679321 OPTION: Name => SharePointPnPPowerShellOnline 2020-02-17T16:02:09.7491815Z ##[debug]00:00:01.4698590 OPTION: ErrorAction => SilentlyContinue 2020-02-17T16:02:10.5287124Z ##[debug]00:00:02.2467561 Done calling powershell «Get-InstalledPackage» «PSModule» 2020-02-17T16:02:10.7513691Z Found SharePointPnPPowerShellOnline module in version 3.18.2002.0 2020-02-17T16:02:10.7606212Z Try Disconnecting before conenction to SharePoint Online. Site url: https://xxx.sharepoint.com/sites/xxxDAS. 2020-02-17T16:02:10.8673376Z Any disconnection to make 2020-02-17T16:02:10.8685559Z Connecting to SharePoint Online. Site url: https://xxx.sharepoint.com/sites/xxxDAS.. 2020-02-17T16:02:10.8697916Z First connection try 2020-02-17T16:02:11.0017476Z ##[debug]Caught exception from task script. 2020-02-17T16:02:11.0063145Z ##[debug]Error record: 2020-02-17T16:02:11.0559987Z ##[debug]C:\vstsagent_work_tasks\DevTestLabUploadToSharePoint_b8e41a47-397c-40d2-abdf-cb47b390f20a\0.2.323\UploadToSharePointWrapper.ps1 : Unable to find type [Microsoft.SharePoint.Client.CollectionNotInitializedException]. 2020-02-17T16:02:11.0571634Z ##[debug]At line:1 char:1 2020-02-17T16:02:11.0582893Z ##[debug]+ . 'C:\vstsagent_work_tasks\DevTestLabUploadToSharePoint_b8e41a47-39 ... 2020-02-17T16:02:11.0594542Z ##[debug]+ ~~~~~~~~~~~~~~~~~ 2020-02-17T16:02:11.0606008Z ##[debug] + CategoryInfo : InvalidOperation: (Microsoft.Share...alizedException:TypeName) [UploadToSharePointWrappe r.ps1], RuntimeException 2020-02-17T16:02:11.0617097Z ##[debug] + FullyQualifiedErrorId : TypeNotFound,UploadToSharePointWrapper.ps1 2020-02-17T16:02:11.0628130Z ##[debug] 2020-02-17T16:02:11.0645192Z ##[debug]Script stack trace: 2020-02-17T16:02:11.0675587Z ##[debug]at , C:\vstsagent_work_tasks\DevTestLabUploadToSharePoint_b8e41a47-397c-40d2-abdf-cb47b390f20a\0.2.323\UploadToSharePointWrapper.ps1: line 23 2020-02-17T16:02:11.0685388Z ##[debug]at , : line 1 2020-02-17T16:02:11.0695952Z ##[debug]at , : line 22 2020-02-17T16:02:11.0707326Z ##[debug]at , : line 18 2020-02-17T16:02:11.0718138Z ##[debug]at , : line 1 2020-02-17T16:02:11.0737135Z ##[debug]Exception: 2020-02-17T16:02:11.0777700Z ##[debug]System.Management.Automation.RuntimeException: Unable to find type [Microsoft.SharePoint.Client.CollectionNotInitializedException]. 2020-02-17T16:02:11.0787310Z ##[debug] at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception) 2020-02-17T16:02:11.0797790Z ##[debug] at System.Management.Automation.Interpreter.ActionCallInstruction2.Run(InterpretedFrame frame) 2020-02-17T16:02:11.0808259Z ##[debug] at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) 2020-02-17T16:02:11.0818683Z ##[debug] at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) 2020-02-17T16:02:11.2281092Z ##[debug] at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame) 2020-02-17T16:02:11.2282753Z ##[debug] at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0) 2020-02-17T16:02:11.2283227Z ##[debug] at System.Management.Automation.PSScriptCmdlet.RunClause(Action1 clause, Object dollarUnderbar, Object inputToProcess) 2020-02-17T16:02:11.2283588Z ##[debug] at System.Management.Automation.PSScriptCmdlet.DoEndProcessing() 2020-02-17T16:02:11.2283882Z ##[debug] at System.Management.Automation.CommandProcessorBase.Complete() 2020-02-17T16:02:11.2329899Z ##[error]Unable to find type [Microsoft.SharePoint.Client.CollectionNotInitializedException]. 2020-02-17T16:02:11.2330520Z ##[debug]Processed: ##vso[task.logissue type=error]Unable to find type [Microsoft.SharePoint.Client.CollectionNotInitializedException%5D. 2020-02-17T16:02:11.2331846Z ##[debug]Processed: ##vso[task.complete result=Failed]

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

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

(you can retrieve this by executing Get-Module -Name *pnppowershell* -ListAvailable)

How did you install the PnP-PowerShell Cmdlets?

ghost commented 4 years ago

Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.

jprinceds commented 4 years ago

Hello MS,

No one for checking my issue??

Thanks in advance!

JeanPrince commented 4 years ago

Hello Microsoft,

Still no answer for my issue? I can add more information, after many retry, we have found that the issue doesn't happen with the version 3.17.2001.2 . But it happens with 3.19 and it is always KO with the version 3.20 .

Regards.

fastlaneb commented 4 years ago

Hi @JeanPrince What exactly are you trying to accomplish? I see a Connect-PnPOnline in your try statement and in your catch statement.

JeanPrince commented 4 years ago

Hi fastlaneb,

Thanks for your reply!

Yes as you can see, i try to connect to the Sharepoint library (after trying a disconnection to be sure to have a clean session) and the exception occurs during the connection phase.

That's the reason why i put the Connect-PnPOnline through a try/catch. That allows me to retry the connection one more time.

But as you can see in the log, the exception does not move through the catch as it looks like it was a fatal exception.

The goals of the connection is to download the library files for the following test automation purpose.

Regards.

fastlaneb commented 4 years ago

Would you be able to simplify things and just do one Connect? Also, is this running in an Azure Automation function? Which version of PnP do you have uploaded?

JeanPrince commented 4 years ago

Yes of course. The first version of the task is this :

$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username, $Password Write-Host "Connecting to SharePoint Online. Site url: $($WebUrl)." Connect-PnPOnline -Url $WebUrl -Credentials $cred Write-Host "Connected."

But unfortunately, the exception occurs on the Connect-PnPOnline line.

And Yes, it's running in Azure DevOps Automation pipeline with DevTestLab VM.

Here are the version of Pnp we used: SharePointPnPPowerShellOnline 3.20.2004.0 SharePointPnPPowerShellOnline 3.19.2003.0 SharePointPnPPowerShellOnline 3.17.2001.2

All of those versions have the bug we encounter but less with 3.17.2001.2.

Regards.

JeanPrince commented 4 years ago

Hi @fastlaneb,

As suggested, here is the simplified version of the code : $cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username, $Password Write-Host "Connecting to SharePoint Online. Site url: $($WebUrl)." Connect-PnPOnline -Url $WebUrl -Credentials $cred Write-Host "Connected."

And here is the result :

2020-04-21T06:59:56.8699186Z Connecting to SharePoint Online. Site url: https://...365.sharepoint.com/sites/Espa......... 2020-04-21T06:59:57.0364764Z ##[error]Unable to find type [Microsoft.SharePoint.Client.CollectionNotInitializedException]. 2020-04-21T06:59:57.1943769Z ##[section]Finishing: [EXPERIMENTAL] Upload test attachments to SharePoint

This error happens with the last 3 versions of PnP.

Thanks again for your help.

Regards.

fastlaneb commented 4 years ago

Hi again @JeanPrince Okay, I can try to reproduce and debug this but I don't exactly know what you mean when you say you're running in an Azure DevOps Automation pipeline. What are the steps to set something like that up?

JeanPrince commented 4 years ago

Thanks for your quick reply. Here are the steps :

image

Best Regards.

fastlaneb commented 4 years ago

Okay, I need slightly more detail on this piece:

Use of custom azure task to launch the PnP script in the VMs for downloading files from sharepoint

What exactly is a custom azure task?

JeanPrince commented 4 years ago

Hi @fastlaneb ,

We create the script in powershell i exposed you earlier to connect to Sharepoint via PnP: In the script, we install the PnP module :

$pnpModuleName = "SharePointPnPPowerShellOnline"
$pnpVersion = "3.17.2001.2"
Install-Module $pnpModuleName -Force -Scope CurrentUser -RequiredVersion $pnpVersion -Verbose

Then we try to connect:

$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username, $Password
Write-Host "Connecting to SharePoint Online. Site url: $($WebUrl)."
Connect-PnPOnline -Url $WebUrl -Credentials $cred
Write-Host "Connected."

And then we build and release it in Azure DevOps as it explained in Microsoft site here : https://docs.microsoft.com/en-us/azure/devops/pipelines/process/tasks?view=azure-devops&tabs=yaml

Regards.

fastlaneb commented 4 years ago

Hi @JeanPrince I'm sorry but I simply haven't had the time to set something like this up. I may be able to try next week. I do know there are lots of authentication fixes that will be integrated into the May PnP release.

JeanPrince commented 4 years ago

Hi @fastlaneb,

Thanks for your reply. No problem, i understand :). I will keep you updated when i will use the may version, thanks for the information.

Regards.

jprinceds commented 4 years ago

Hi @fastlaneb,

I hope everthing is good for you.

I come with some news.

As expected, i install the latest version of PnP but unfortunatley, I have the same issue. Here is the debug log with a stacktrace coming directly from the module. Hope it's helps.

2020-06-03T13:29:37.2242763Z ##[debug]InstallPackageLocal' - name='SharePointPnPPowerShellOnline', version='3.21.2005.2',destination='C:\Users\uitestsagent\AppData\Local\Temp\692137299' 2020-06-03T13:29:37.2268021Z ##[debug]00:00:05.8085379 C:\Users\uitestsagent\AppData\Local\Temp\692137299\SharePointPnPPowerShellOnline\SharePointPnPPowerShellOnline.nupkg ... 2020-06-03T13:29:39.1272132Z ##[debug]00:00:07.7069397 For publisher validation, current module 'SharePointPnPPowerShellOnline' with version '3.21.2005.2' with publisher name 'CN=Microsoft Root Certificate Authority 2011, O=Microsoft Corporation, L=Redmond, S=Washington, C=US'. Is this module signed by Microsoft: 'True'. 2020-06-03T13:29:51.8768340Z ##[debug]Module 'SharePointPnPPowerShellOnline' was installed successfully to path 'C:\Users\uitestsagent\Documents\WindowsPowerShell\Modules\SharePointPnPPowerShellOnline\3.21.2005.2'. 2020-06-03T13:29:51.9688051Z ##[debug]00:00:20.5509720 Done calling powershell «Install-Package» «PSModule» 2020-06-03T13:29:52.0484515Z Installation of module in version done. 2020-06-03T13:29:52.0492137Z Force TLS 11 12 13 2020-06-03T13:29:52.0708101Z Connecting to SharePoint Online. Site url: https://axa****.sharepoint.com/**. 2020-06-03T13:29:52.1107410Z An error occurs while connecting : ... 2020-06-03T13:30:02.1874674Z ##[debug]Error record: ... 2020-06-03T13:30:02.2494153Z ##[debug] + CategoryInfo : InvalidOperation: (Microsoft.Share...alizedException:TypeName) [DownloadFromSharePointWr apper.ps1], RuntimeException ... 2020-06-03T13:30:02.2658610Z ##[debug]Exception: 2020-06-03T13:30:02.2702350Z ##[debug]System.Management.Automation.RuntimeException: Unable to find type [Microsoft.SharePoint.Client.CollectionNotInitializedException]. 2020-06-03T13:30:02.2716013Z ##[debug] at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception) 2020-06-03T13:30:02.2729990Z ##[debug] at System.Management.Automation.Interpreter.ActionCallInstruction 2.Run(InterpretedFrame frame) 2020-06-03T13:30:02.2743046Z ##[debug] at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) 2020-06-03T13:30:02.2757909Z ##[debug] at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) 2020-06-03T13:30:02.2772895Z ##[debug] at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame) 2020-06-03T13:30:02.2788474Z ##[debug] at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0) 2020-06-03T13:30:02.2802324Z ##[debug] at System.Management.Automation.PSScriptCmdlet.RunClause(Action 1 clause, Object dollarUnderbar, Object inputToProcess) 2020-06-03T13:30:02.2815963Z ##[debug] at System.Management.Automation.PSScriptCmdlet.DoEndProcessing() 2020-06-03T13:30:02.2831457Z ##[debug] at System.Management.Automation.CommandProcessorBase.Complete() 2020-06-03T13:30:02.3187459Z ##[error]Unable to find type [Microsoft.SharePoint.Client.CollectionNotInitializedException]. 2020-06-03T13:30:02.3199582Z ##[debug]Processed: ##vso[task.logissue type=error]Unable to find type [Microsoft.SharePoint.Client.CollectionNotInitializedException%5D. 2020-06-03T13:30:02.3201274Z ##[debug]Processed: ##vso[task.complete result=Failed]**

jprinceds commented 4 years ago

Hi, I have good news!

After using a trap instead of try catch, we've seen that the real error behind the Collection...Exception was The term 'Connect-PnPOnline' is not recognized as the name of a cmdlet.

That clearly shows the miss of loading of pnp module despite the installation.

We change the way of installation by downloading the module package(.nupkg), unzip it and use Import-Module -Name "/SharePointPnPPowerShellOnline.psd1" inside the script before connection.

And voilà, no more issue!

We think that, may be using an azure devops task script to install the module and then connect, impact the visibilty of that module for the connect-pnp script.