CSOM SharePoint Online PowerShell Exploring Web Class


CSOM SharePoint Online PowerShell Exploring Web Class


Introduction

In this TechNet Wiki let's explore SharePoint Web Class using CSOM. Web Class Supported in SharePoint 2010 / SharePoint 2013 and Online.
Reference MSDN Link

Summary

Most of the SharePoint IT Pro thinks that CSOM is for developers and it requires loads of development skills. Indeed not true, Client Server Object Model can be leveraged using PowerShell and IT Pro can use it at certain level and of course with little bit of Development practice we can achieve more.

C# Code in MSDN

#region - C # Code to get Web Information
using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointFoundation.Samples
{
    class WebExample
    {
        static void Main()
        {
            string siteUrl = "http://MyServer/sites/MySiteCollection";

            ClientContext clientContext = new ClientContext(siteUrl);
            Web site = clientContext.Web;

            clientContext.Load(site);
            clientContext.ExecuteQuery();

            Console.WriteLine("Site information: \n\n");
            Console.WriteLine("Title: {0}", site.Title);
            Console.WriteLine("ID: {0}", site.Id);
            Console.WriteLine("Language: {0}", site.Language);
            Console.WriteLine("UI Version: {0}", site.UIVersion);
            Console.WriteLine("Description: {0}", site.Description);
            Console.WriteLine("Created: {0}", site.Created);
        }
    }
}
#endregion

The above code retrieves the information as shown below

  1. Site information
  2. Title
  3. ID
  4. Language
  5. UI Version
  6. Description
  7. Created

CSOM PowerShell Code

Import-Module C:\Temp\CSOM\Microsoft.SharePoint.Client.dll
Import-Module C:\Temp\CSOM\Microsoft.SharePoint.Client.Runtime.dll

$Site = "https://chensoffice365.SharePoint.com"
$Admin = "chendrayan@chensoffice365.onmicrosoft.com"

$password = Read-Host 'Enter Password' -AsSecureString

$context = New-Object Microsoft.SharePoint.Client.ClientContext($Site)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Admin , $password)

$context.Credentials = $credentials

$web = $context.Web
$context.Load($web)
$context.ExecuteQuery()
[PSCustomObject]@{
"Title" = $web.Title
"Created" = $web.Created
"ID" = $web.Id
"UI Version" = $web.UIVersion
"Description" = $web.Description
"Language" = $web.Language
}

Considerations

We need to explore the Web Class to check the Proprties and Methods to leverage it in PowerShell. In this demo I have used PSCustom object to collate information. Use the below help to explore more

001
help about_Object_Creation

To explore about Methods and Properties use the below PowerShell code.

Import-Module C:\Temp\CSOM\Microsoft.SharePoint.Client.dll
Import-Module C:\Temp\CSOM\Microsoft.SharePoint.Client.Runtime.dll

$Site = "https://chensoffice365.SharePoint.com"
$Admin = "chendrayan@chensoffice365.onmicrosoft.com"

$password = Read-Host 'Enter Password' -AsSecureString

$context = New-Object Microsoft.SharePoint.Client.ClientContext($Site)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Admin , $password)

$context.Credentials = $credentials

$web = $context.Web
$context.Load($web)
$context.ExecuteQuery()

$web | GM -MemberType Method
$web | GM -MemberType Property

Output