Getting SharePoint Context With Client Side SharePoint PowerShell

If you like writing PowerShell script and you wanted to do certain updates or do some kind of reporting out of your SharePoint Site Collection or particular site, I would recommend, using Client Side SharePoint PowerShell. For this, you don't have to run any scripts on SharePoint Server. You can do it on remote basis, as long as you have all the pre-requisites installed on your local machine. The Client Side SharePoint PowerShell from CodePlex provides you basic PowerShell modules and also make sure that you have all the pre-requisites installed on your machine. Once you have this, you are ready to write your own Client Side PowerShell script.

Client Side SharePoint PowerShell basically uses SharePoint Client Object Model,  using local libraries installed on your machine. It is not useful, if your SharePoint environment is earlier than SharePoint 2010. Another thing to keep in mind is that SharePoint Client Object Model doesn't support all the properties/methods, which are there in Server Side Object Model. Thus, in certain cases, you will feel that you have been handicapped due to SharePoint Client Object Model.

Now, coming back to the main purpose of this article, the Client Side SharePoint PowerShell libraries from CodePlex has a generic function called Initialize-SPPS which takes certain parameters like Site URL, Is2010 or IsOnline etc and many others. But using this function is not intuitive unless you read entire blog about how to use those libraries. I have created another wrapper around Initialize-SPPS function which is more intuitive so any layman user can run the script and can be re-used to any other functions based on this wrapper. You can keep on building your own client side PowerShell functions based on it. I will post more such client side PowerShell script in future which will be depend on this wrapper.

Purpose

  • Working with SharePoint site collection or site remotely with PowerShell using Client Object Model.

Pre-requisites

  • Windows PowerShell installed on local machine.
  • Client Side SharePoint PowerShell libraries from CodePlex.

How to make it work

  • Download Client Side SharePoint PowerShell libraries from CodePlex.

  • Store in your local folder.

  • Open command prompt as an administrator.

  • Enter keyword “PowerShell”.

  • Change the directory where Client Side SharePoint PowerShell modules are installed.

  • Load Client Side SharePoint Module by “Import-Module ./spps.psm1”.

  • To test whether your machine has all the pre-requisites for running Client Side SharePoint PowerShell, enter “Test-SPPS” and press enter.

  • If your machine has all the pre-requisites, it shows message that it is ready to connect to SharePoint 2010, SharePoint 2013 and SharePoint Online.

  • If you are missing any of the pre-requisites, it opens browser with link to download it.

  • After installing all the pre-requisites you can again run “Test-SPPS” to test your connectivity.

  • Download the attached Get-SharePointContext.ps1 file (attached) and save in the same folder where Client Side SharePoint PowerShell modules are stored.

  • Enter “Get-SharePointContext.ps1” and Press Enter.

  • It clears the screen and display message to choose your target SharePoint environment.

  • It prompts for SharePoint site or web Url and checks whether entered Url is valid Url or not.

  • Then it prompts whether user has admin access to entered site or web url. If yes, it uses current user login credentials else it prompt for alternate credentials. You can create context by using another user credentials like service accounts etc.

  • Based on SharePoint environment, Url and user credentials, it create SharePoint Client Context Object ($SPPS), SPSite object ($Site) and SPWeb object ($Web).

  • You can use these local PowerShell variables to complete your desired task in SharePoint or build your own PowerShell functions which will use this generic wrapper for creating SharePoint Context.

Note:

  • Get-SharePointContext.ps1 internally uses Initialize-SPPS function which doesn’t have proper error handling. So you might see errors if you enter invalid SharePoint Url.

Let me know your comments/feedback/issues.

Updated on 11-May-2017 
 
You don't need to download anything from code plex now. Updated attachment contains all the update PowerShell module files which you can download and save to your local drive. Open the PowerShell window and follow below steps:
 
1. Import module .\spps.psm1
2. Test-SPPS
If Client libraries for SP 2010, SP 2013 and SharePoint Online are installed, it displays appropriate message. If not, it opens respective download pages in the default browser window. Install all the client libraries required for SharePoint, close and re-open PowerShell window and repeat above steps.
 
3. Run script: Get-SharePointContext.ps1 
 
This script provides user friendly interface to connect to your SharePoint site created global variables mentioned above. 
 

Up Next
    Ebook Download
    View all
    Learn
    View all