0

am trying to get all the fields in the document library in my site collection. here is my code . but after entering my username/password , it throws error:

       $SiteURL = "http://vmsrvr1:28516/"

     Write-Host "Loading CSOM libraries" -foregroundcolor black -
       backgroundcolor 
      yellow
       Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web 
     Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
      Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web 
      Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
      Write-Host "Lib. successfully loaded !" -foregroundcolor black -
    backgroundcolor Green 

     $scriptPath = "D:\PoCSolutions"


   $User =    "mydomain\adminuser1" #Read-Host -Prompt "username"
      $password =  Read-Host -Prompt "Enter password" -AsSecureString


      Write-Host "Trying to reach $SiteURL // Handling ClientContext..." -
    foregroundcolor black -backgroundcolor yellow  
     $Context = New-Object 
     Microsoft.SharePoint.Client.ClientContext($SiteURL) 
      $credentials = New-Object 
          Microsoft.SharePoint.Client.SharePointOnlineCredentials($User, 
     $password) 
    $Context.Credentials = $credentials 
     $context.RequestTimeOut = 5000 * 60 * 10;
      $web = $context.Web
      $site = $context.Site 
       $context.Load($web)
      $context.Load($site)
       try
       {
          $context.ExecuteQuery()
        Write-Host "Connected !" -foregroundcolor black -backgroundcolor 
        Green
       }
       catch
         {
        Write-Host "Impossible to reach $SiteURL : bad password ?" -
       foregroundcolor black -backgroundcolor Red
           return
          }

   $list = $web.Lists["APT"];
      $fields = $list.Fields;

   $list.Fields | select InternalName  |  Export-Csv -path ./blabla.csv;
      Write-Host "Done !" -foregroundcolor black -backgroundcolor Green 

error is

New-Object : Exception calling ".ctor" with "2" argument(s): "The 'username' argument is invalid." At E:\PoCSolutions\getallfields1.ps1:18 char:16 + $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredential ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

username-invalid1

1

you are using the SharePoint Online (Office 365) Credential. Thats why your are getting this error. You are on a SharePoint on-premise, you need to use an other type of Credential.

You should use NetworkCredential

Like :

$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) 
$credentials = New-Object New-Object System.Net.NetworkCredential($User, $password)
$Context.Credentials = $credentials 

After, you need to load your fields of your list. Try something like :

$list = $web.Lists.GetByTitle('APT')
$fields = $list.Fields
$context.Load($list)
$context.Load($fields)
$context.ExecuteQuery()

$fields | select InternalName  |  Export-Csv -path ./blabla.csv;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.