How to Execute the Client Object Model PowerShell Script and get the Client Context on SharePoint 2010


Sathish Nadarajan
SharePoint MVP
Published On :   29 Dec 2017
Visit Count
Today :  4    Total :   682
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

Sharegate: Kick-Ass Tool
Think Your SharePoint & Office 365 Are Secure ? Find Out Now!


Recently for one of the requirement, we were supposed to write the PowerShell Client Object Model Script to run against SharePoint 2010. We have written a lot of code on the SSOM for SP2010 and CSOM for SP2013 or above. But, for the first time, met with this strange requirement stating CSOM Script for SP2010. Though it does not support few functionalities, we can achieve some of the basic functionalities with this.

In this article, we are going to see, how to add the Client DLL and how to create the client context. In the upcoming articles, let us see, how to do various functionalities using CSOM PowerShell in SP2010.

 
 cls
 
  
 $Host.UI.RawUI.WindowTitle = "-- Get Site Context --"
 
 $StartDate = Get-Date
 Write-Host -ForegroundColor White "------------------------------------"
 Write-Host -ForegroundColor White "| Get Site Context  |"
 Write-Host -ForegroundColor White "| Started on: $StartDate |"
 Write-Host -ForegroundColor White "------------------------------------"
 
 $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm
 
 ################# Set the Current Path as Execution Path ####################
 
 $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
 Set-Location $scriptBase
 
 ############# set the Error Preference ################
 
 $ErrorActionPreference = "SilentlyContinue"
 
 
 # Create Log File Folder
 if(!(TEST-PATH ".\Logs-$LogTime")) 
 {
    NEW-ITEM ".\Logs-$LogTime" -type Directory
 }
 
 # Create Report File Folder
 if(!(TEST-PATH ".\Reports-$LogTime")) 
 {
    NEW-ITEM ".\Reports-$LogTime" -type Directory
 }
 
 # Assign the Log and Progress Files
 $TranscriptFile = ".\Logs-$LogTime\GetSiteInfo.Transcript.rtf"
  
 
 try{
   stop-transcript|out-null
 }
 catch [System.InvalidOperationException]{}
 
 start-transcript $TranscriptFile
 
 function AddCSOM(){
      #Load SharePoint client dlls 
      $a = [System.Reflection.Assembly]::LoadFile(    "$scriptBase\ClientLibraries\Microsoft.SharePoint.Client.dll") 
      $ar = [System.Reflection.Assembly]::LoadFile(    "$scriptBase\ClientLibraries\Microsoft.SharePoint.Client.Runtime.dll") 
      
      if( !$a ){
          $a = [System.Reflection.Assembly]::LoadWithPartialName(        "Microsoft.SharePoint.Client")
      }
      if( !$ar ){
          $ar = [System.Reflection.Assembly]::LoadWithPartialName(        "Microsoft.SharePoint.Client.Runtime")
      }
      
      if( !$a -or !$ar ){
          throw         "Could not load Microsoft.SharePoint.Client.dll or Microsoft.SharePoint.Client.Runtime.dll"
      }
      
      
      #Add overload to the client context.
      #Define new load method without type argument
      $csharp =     "
       using Microsoft.SharePoint.Client;
       namespace SharepointClient
       {
           public class PSClientContext: ClientContext
           {
               public PSClientContext(string siteUrl)
                   : base(siteUrl)
               {
               }
               // need a plain Load method here, the base method is a generic method
               // which isn't supported in PowerShell.
               public void Load(ClientObject objectToLoad)
               {
                   base.Load(objectToLoad);
               }
           }
       }"
      
      $assemblies = @( $a.FullName, $ar.FullName,     "System.Core")
      #Add dynamic type to the PowerShell runspace
      Add-Type -TypeDefinition $csharp -ReferencedAssemblies $assemblies
 }
 
 AddCSOM
 
 $credentials = Get-Credential
 
  
 cls
 
  
     $SubSiteURL = "https://sppalsmvp.sharepoint2010.com/sites/myteamsite"
 
     Write-Host "Processing the Site - " $SubSiteURL  -Foreground Yellow
 
     $context = New-Object SharepointClient.PSClientContext($_.SubSiteURL)
     $context.Credentials = $credentials
 
     
     
     $web = $context.Web
      
     $context.Load($web)
      
     $context.Load($web.Lists)
     
     $context.ExecuteQuery() 
 
      Write-Host "Web Title " $web.Title
 
  
    
 Write-Host "Process Completed..  Press Enter to Exit" -ForeGroundColor Green
 
  try{
   stop-transcript|out-null
 }
 catch [System.InvalidOperationException]{}       
 
The above code will write the Web Title by CSOM.


DOWNLOAD THE DLLs HERE



Happy Coding,

Sathish Nadarajan.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

Remote List Viewer