Skip to main content
Blogging about SharePoint

Blogging about SharePoint

Search
Home
Blogging about SharePoint
Public Speaking
  
Michael Blumenthal's BlumenthalIT.NET > Blogging about SharePoint > Posts > Using PowerShell and the .NET CSOM to Query SharePoint 2013 Online  

November 15
Using PowerShell and the .NET CSOM to Query SharePoint 2013 Online

Hello from the Microsoft SharePoint Conference 2012!

One of the things I wanted to do while here was learn how to use PowerShell to interact with SharePoint 2013 remotely.  Now remotely can mean two separate things here: 1) Using PowerShell remoting to send POSH commands to a server to be run locally, or 2) using remote APIs (in this case the .NET client side object model, aka .NET CSOM).  Since I want to have POSH that can work with both SharePoint on premise and SharePoint online, I decided to use the .NET client side object model.  I was developing against a free developer instance of SP2013 online, which you can request at http://msdn.microsoft.com/en-US/library/office/apps/fp179924.  At first, I tried using the example on how to retrieve a list that I found here: http://msdn.microsoft.com/en-us/library/ee538683.aspx After converting it to POSH, it was:

$loadInfo1 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
$loadInfo2 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
$webUrl = Read-Host -Prompt "HTTPS URL for your SP Online 2013 site"

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($webUrl)
$web = $ctx.Web
$lists = $web.Lists
$ctx.Load($lists)

I ran it and got this error:

Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (403) Forbidden."
At line:11 char:1
+ $ctx.ExecuteQuery()
+ ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException

That kind of made sense, as I wasn’t passing any identity information, and the Microsoft Account associated with my Windows 8 domain account was not my SP Online Microsoft Account.

I looked and looked on MSDN, and didn’t find much helpful.  I tried setting the $ctx.FormsAuthenticationLoginInfo but that didn’t work.

Eventually, I gave up and decided to catch up on my reading of the Product Team Blog, in particular this post: http://sharepoint.microsoft.com/blog/Pages/BlogPost.aspx?pID=1048

At the end of the post is a PowerShell script that passes SharePointOnline credentials!

$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)

So now, I included that in my script, and I have:

#This works on SharePoint Online sites!

$loadInfo1 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
$loadInfo2 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
$webUrl = Read-Host -Prompt "HTTPS URL for your SP Online 2013 site" #"
https://michaelbl.sharepoint.com"
$username = Read-Host -Prompt "Email address for logging into that site" #michaelbl@michaelbl.onmicrosoft.com"
$password = Read-Host -Prompt "Password for $username" -AsSecureString


$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($webUrl)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
$web = $ctx.Web
$lists = $web.Lists
$ctx.Load($lists)
$ctx.ExecuteQuery()

$lists| select -Property Title

which outputs:

Title                                                                                                                                                  -----

App Packages 

appdata

Apps in Testing

Composed  Looks     

Content type publishing error log

Converted Forms

Documents

Form

Templates

Get started with Apps for Office and SharePoint

List Template Gallery

Master Page Gallery 

Project Policy Item List                    

Site Assets

Site Pages       

Solution Gallery

Style Library

TaxonomyHiddenList

Theme Gallery

User Information List

Web Part Gallery

wfpub

There you have all the lists in this particular SharePoint Online (Office 365, SharePoint 2013) site!

--Michael

Comments

Re: Using PowerShell and the .NET CSOM to Query SharePoint 2013 Online

 on 1/28/2013 12:41 AM

 Formal Evening Dresses

 on 2/28/2013 12:17 AM

halterpromdresses

http://www.good4dresses.com/one-shoulder-night-moves-dress-classic-prom-nm-a537a.html   one shoulder night moves dress classic prom nm a537a
http://www.good4dresses.com/one-shoulder-night-moves-dress-one-shoulder-nm-6437wr.html   one shoulder night moves dress one shoulder nm 6437wr
 on 3/21/2013 6:54 PM

replica breguet watches

http://www.hotswisswatches.co.uk/breguet-watches.html  M like look at, A considerable amount m like look at of your cruiselines generate a Bahamas an example of its can stop and also "ports with call" for the reason that find the fact that persons really like passing time listed here.
 on 4/7/2013 10:25 PM

Re: Using PowerShell and the .NET CSOM to Query SharePoint 2013 Online

 on 10/11/2013 5:50 PM

Re: Using PowerShell and the .NET CSOM to Query SharePoint 2013 Online

 on 10/18/2013 1:28 AM

What about proxy authentication?

My current client has a proxy server and how do I pass this information to the client context in PowerShell?
 on 12/20/2013 7:00 AM

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Title


Body *


Please put your name at the end of your comment as the Name field doesn't display in the main view of the comments.

Name: *


Your name.  If you first name is Michael, please provide initials or the like to clearly distinguish your name from Michael Blumenthal's.
Also, this field does not appear in the main view of the comments.

Need Paid Consulting Services?

Are you interested in engaging a SharePoint consultant for fee-based consulting services?  If so, I can have an Account Executive from my employer contact you.  Other site visitors will not see this column.

Additional Contact Information

Please provide additional contact information if you need a direct response.  Other site visitors will not see this column.

What is tomorrow's date? *

Select a date from the calendar.
This is a weak CAPTCHA based on advice from a tweet from @ToddKlindt.

Attachments