Using the Sharepoint CSOM with PowerShell

Michael Frederick picture Michael Frederick · Aug 20, 2013 · Viewed 20.4k times · Source

There are many examples online of how to access/use the SharePoint Client-Side Object Model with PowerShell. But of course, they don't seem to work for me. I seem to be having trouble accessing some of the credential code:

PS C:\Scripts> $webUrl = "https://abc.sharepoint.com>"
PS C:\Scripts> $username = "user3"
PS C:\Scripts> $password = "password"
PS C:\Scripts>
PS C:\Scripts> $ctx = new-object Microsoft.SharePoint.Client.ClientContext($webUrl)
PS C:\Scripts> $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
New-Object : Cannot find type Microsoft.SharePoint.Client.SharePointOnlineCredentials]: make sure the assembly containing this type is loaded.
At line:1 char:30
+ $ctx.Credentials = New-Object <<<<  Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
    + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

I am attempting to access a SharePoint 2010 server that we maintain which requires logon authentication. Does anyone know what I am doing wrong?

OK, so many responses have told me that I am using the incorrect credentialing type for this connection. So I have changed to:

$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$clientContext.AuthenticationMode = "FormsAuthentication"
$clientContext.FormsAuthenticationLoginInfo = New-Object Microsoft.SharePoint.Client.FormsAuthenticationLoginInfo("myDomain\myUser", "myPassword")

which seems to work fine. But then...

$web = $clientContext.Web
$properties = $web.AllProperties
$clientContext.Load($web)

gives me:

> Cannot find an overload for "Load" and the argument count: "1". At
> line:1 char:20
> + $clientContext.Load <<<< ($web)
>     + CategoryInfo          : NotSpecified: (:) [], MethodException
>     + FullyQualifiedErrorId : MethodCountCouldNotFindBest

and when I try to look at the $clientContent object:

PS C:\Scripts> $clientContent | get-member
Get-Member : No object has been specified to the get-member cmdlet.
At line:1 char:28
+ $clientContent | get-member <<<<
    + CategoryInfo          : CloseError: (:) [Get-Member], InvalidOperationException
    + FullyQualifiedErrorId : NoObjectInGetMember,Microsoft.PowerShell.Commands.GetMemberCommand

which makes no sense at all. Anyone have any help for this?

Answer

Joseph Saad picture Joseph Saad · Oct 18, 2013

This is working for me for SharePoint online

$siteUrl = “https://URL.sharepoint.com”
$password = Read-Host -Prompt "Enter password" -AsSecureString 
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl) 
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials(
                                                            "[email protected]"
                                                          , $password) 

$ctx.Credentials = $credentials

$web = $ctx.Web 
$ctx.Load($web) 
$ctx.ExecuteQuery()