3
votes

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?

4
Thanks everyone. I have tried all of these approaches, and still none work, although we do use Forms/Claims authentication. This is what I get no matter what I try: PS C:\Scripts> $clientContext.Load($web) Cannot find an overload for "Load" and the argument count: "1". At line:1 char:20 + $clientContext.Load <<<< ($web) + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodCountCouldNotFindBestMichael Frederick

4 Answers

4
votes

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()
3
votes

SharePointOnlineCredentials is used for authenticating to Office365/ SharePoint 2013 Online services. If you don't have a hosted 2013 instance, it's not a surprise that PS can't find the type.

If you're working on a server you maintain in your own domain and you are using Windows Authentication, your context will pick up your existing security token.

If you're doing forms or claims based authentication, you'll have to tell your context:

$ctx.AuthenticationMode = "FormsAuthentication"
$ctx.FormsAuthenticationLoginInfo = New-Object Microsoft.SharePoint.Client.FormsAuthenticationLoginInfo("domain\username", "password")

There's also a ClientContext.Credentials property if you need to enter alternate credentials.

2
votes

At the beginning of your powershell script, specify the full path to the CSOM DLLs you need like this:

Add-Type -path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
Add-Type -path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'

That resolves the 403 error for me. If I load the dlls using an environment variable as part of the path it doesn't work on my server, but does on my workstation.

0
votes

I'm using the following successfully against a SharePoint 2013 on-premise installation. It is a little different to your example in that it prompts for credentials - but I think that's generally a better approach than entering the password in the script.

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = Get-Credential
$ctx.Credentials = $credentials
...