0

I am trying to connect to SharePoint 2013 site with CSOM using a function with 2 arguments. When i am giving only $x that is site URL as argument then code is working fine but when i am giving two arguments to function then it is not working.

Error is

New-Object : Cannot find an overload for "ClientContext" and the argument count: "2". At line:3 char:9 + $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($z) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [New-Object], MethodException + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand .

Please help as i am new to SharePoint Powershell.

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")

    function a($x,$y){
     $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($x)
        $ctx.Credentials = New-Object System.Net.NetworkCredential($loginname,$pwd) 
        $web=$ctx.web 
        $ctx.load($web)
        $ctx.ExecuteQuery()
        $web.Url
    }
    $pwd = Read-Host "Enter Password of SharePoint 2013 site owner "-AsSecureString
    $loginname = "username"
    $x = "https://cortona.sharepoint2013.com/sites/abc"
    $y = 2
    a($x,$y)
2
  • 1
    Call the function as a $x $y
    – Deepmala
    Apr 30, 2019 at 19:49
  • @deepmala it worked !! Thanks. Do you have any idea why PowerShell behaves like that for arguments? Apr 30, 2019 at 21:08

0

Your Answer

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