Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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:

Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.

PS C:\Scripts> [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")

GAC    Version        Location
---    -------        --------
True   v2.0.50727     C:\Windows\assembly\GAC_MSIL\Microsoft.SharePoint.Client\14.0.0.0__71e9bce111e9429c\Microsoft....


PS C:\Scripts> [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")

GAC    Version        Location
---    -------        --------
True   v2.0.50727     C:\Windows\assembly\GAC_MSIL\Microsoft.SharePoint.Client.Runtime\14.0.0.0__71e9bce111e9429c\Mi...


PS C:\Scripts>
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?

share|improve this question

3 Answers 3

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 = ClientAuthenticationMode.FormsAuthentication;
$ctx.FormsAuthenticationLoginInfo = new FormsAuthenticationLoginInfo("domain\username", "password");

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

share|improve this answer

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()
share|improve this answer

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
...
share|improve this answer
    
I am finally ready to try this again. I have updated my approach and am using some of this reply: PS C:\Scripts> AddCSOM PS C:\Scripts> $context = New-Object SharepointClient.PSClientContext("siteCollection") PS C:\Scripts> $credentials = Get-Credential # Here I was prompted for and supplied my domain\username and password PS C:\Scripts> $context.Credentials = $credentials PS C:\Scripts> $context.ExecuteQuery() Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (403) Forbidden." –  Michael Frederick Apr 1 at 20:52
    
I am finally ready to try this again. I have updated my approach and am using some of this reply: >PS C:\Scripts> AddCSOM >PS C:\Scripts> $context = New-Object SharepointClient.PSClientContext("siteCollection") >PS C:\Scripts> $credentials = Get-Credential # Here I was prompted for and supplied my domain\username and password >PS C:\Scripts> $context.Credentials = $credentials >PS C:\Scripts> $context.ExecuteQuery() >Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (403) Forbidden." What now? –  Michael Frederick Apr 1 at 21:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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