none
Starting a second message loop on a single thread is not a valid operation. Use Form.ShowDialog instead. RRS feed

  • Question

  • I am trying to connect to SharePoint online using CSOM powershell. I have created a basic PowerShell GUI (form) that will have 2 textbox - username and password. Site URL is hardcoded in script. When I run the script form opens up, asks for username and password, It is connecting and displaying site title in another Label. Below is the code for connecting.

    $clientContext=New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
    $clientContext.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $AdminPassword)

    But the problem is, when I try to connect to sharepoint using below lines of code, and display title in GUI form, it is giving me below error. Please help me on connecting to sharepoint online and printing Site Title in GUI form (without password)

    $clientContext = [MSDN.Samples.ClaimsAuth.ClaimClientContext]::GetAuthenticatedContext($siteUrl)

    Thanks

    Thandapani

    Monday, September 23, 2019 3:27 AM

All replies

  • Please find error message as below :

    "Starting a second message loop on a single thread is not a valid operation. Use Form.ShowDialog instead."
    System.InvalidOperationException: Starting a second message loop on a single thread is not a valid operation. Use Form.ShowDialog instead.
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at MSDN.Samples.ClaimsAuth.ClaimsWebAuth.Show()
       at MSDN.Samples.ClaimsAuth.ClaimClientContext.GetAuthenticatedCookies(String targetSiteUrl, Int32 popUpWidth, Int32 popUpHeight)
       at MSDN.Samples.ClaimsAuth.ClaimClientContext.GetAuthenticatedContext(String targetSiteUrl, Int32 popUpWidth, Int32 popUpHeight)
       at CallSite.Target(Closure , CallSite , Type , Object )



    Script I am using:

    $array=@()
    $Form = New-Object System.Windows.Forms.Form   
    $Form.TopMost = $false #If true, the folder browse in the search button is displayed behind the form
    #$Form.Size = New-Object System.Drawing.Size(600,600)
    $Form.StartPosition = "CenterScreen" # loads the window in the center of the screen
    $Form.Text = "Automatic Terminal-Server deployment" # window description
    $Form.WindowState ="Maximized"
    $Form.Add_Shown({$Form.Activate()})
    $Button1                         = New-Object system.Windows.Forms.Button
    $Button1.BackColor = "#0C2D71"
    $Button1.ForeColor = "white"
    $Button1.text                    = "Show Title"
    $Button1.width                   = 116
    $Button1.height                  = 50
    $Button1.location                = New-Object System.Drawing.Point(30,510)
    $Button1.Font                    = 'Microsoft Sans Serif,10'
    $Label4                          = New-Object system.Windows.Forms.Label
    #$Label4.BackgroundImage = $Image
    $Label4.AutoSize                 = $true
    $Label4.width                    = 25
    $Label4.height                   = 10
    $Label4.location                 = New-Object System.Drawing.Point(30,220)
    $Label4.Font                     = 'Microsoft Sans Serif,10'
    $Form.controls.AddRange(@($Button1,$Label4))
    $Button1.Add_Click({
            $clientContext = [MSDN.Samples.ClaimsAuth.ClaimClientContext]::GetAuthenticatedContext($siteURL)
            $web = $clientContext.Web
            $clientContext.Load($web)
            $clientContext.ExecuteQuery()
            $Label4.Text= $web.title
     })

    $form.ShowDialog()
    Monday, September 23, 2019 4:08 AM