PowerShell: How to develop interactive logs

In this article we will look for developing a logging mechanism for PowerShell automation components that can provide logs not just in form of a flat file but also as interactive html format. This best part of the story is we can design this html log format using whatever html/Css elements to make as intuitive as possible.

For the sake of clear understanding I am going to explain the complete process in various steps-

We need to download & install the “EnhancedHTML2” module.

In Step 1 we can see the command to download & install the module. We can use this command using PowerShell Console

Or we can download & install this module manually as shown in Step 2 from the PowerShell Gallery

0

In Step 3 we have added the code to query for all Lists in SharePoint Site, I keep it simple & straight for this demo

In Step 4 we have added code to build a collection keep list of SharePoint Lists using a PowerShell hash table as shown below-

1

In Step 5&6 we can see the implementation of “EnhancedHTML2” module

In Step 5 we will convert the data collection (Row & Column format) to “EnhancedHTMLFragment” by using “ConvertTo-EnhancedHTMLFragment” cmdlet. Similarly we can create any number of HTML Fragments as per our need

This cmdlet uses a switch “PreContent” that allows you to any HTML snippet before the fragment add to the HTML outcome

“-MakeTableDynamic” switch allows the cmdlet to embed basic html plumbing like Search, Pagination and so on to the HTML outcome

In Step 6 we will append all the HTML Fragments to build a single HTML file by using “ConvertTo-EnhancedHTML” cmdlet.

This cmdlet has got “HTMLFragments” parameter that will accept comma separated values of names of the Fragments that you want to include in the HTML document

Another important parameter “CssUri” accept the path of CSS file that will define the styling attributes for the HTML page

“Out-File” parameter defines the path to the save the HTML Log file

2

In Step 7 we will call the method to execute above steps

3

Step 8 the dlls that we need to include the script to make SharePoint Calls work

4

Step 9 shows the CSS file that we referred with “ConvertTo-EnhancedHTML” in Step 6

5

Step 10 shows the HTML outcome log file generated after we successfully executed the steps above

6

In Step 11 & 12 we can explore the html generated as part of this execution and it is amazing to see how this HTML is structured by adding references to the required JavaScript files and body elements.

It can be clearly noticed the generated html body content is in the form of table structure, this happened since we specified “-As Table” switch with “ConvertTo-EnhancedHTMLFragment” cmdlet in Step 5

Also we can notice the “<h1>” tag in the HTML body, it is the same content that we include to the fragment using “-PreContent” switch with “ConvertTo-EnhancedHTMLFragment” cmdlet in Step 5

78

And finally we can see the HTML based interactive logs in Step 13 shown below. This would be feature rich HTML Page and we can even further add more feature as per our needs, thought this would cost additional effort.

9

That is all for this demo.

Hope you find it helpful.

SharePoint Online: Working With List Settings Using PowerShell

In this article we will discuss the configuration of some of the important List Settings for SharePoint Online Lists. Though I have taken only a handful of operations to showcase in this demo, but remember there are lot more to explore.

Operation: How to enable “New Folder” Menu Command

We can see this setting available under “Advanced Settings” for the list as shown below-

1

We can play with this setting using PowerShell as described below-

2

In Step 1 we will get the object reference to the respective list by calling “GetByTitle” method

In Step 2 we will set “EnableFolderCreation” to True

In Step 3 we will update the list property by calling “Update” method

In Step 4 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method

In Step 5 we will display success message to the Users if Step 4 executes successfully

In Step 6 we will call function that we have explained in Step 1-5

3

Once this script executes successfully we can see list setting updated by navigating “Advanced Settings” of the list as shown below-

4

Operation: How to disable “List Item Attachments” Option

We can see this setting available under “Advanced Settings” for the list as shown below-

5

We can play with this setting using PowerShell as described below-

6

In Step 1 we will get the object reference to the respective list by calling “GetByTitle” method

In Step 2 we will set “EnableAttachments” to False

In Step 3 we will update the list property by calling “Update” method

In Step 4 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method

In Step 5 we will display success message to the Users if Step 4 executes successfully

In Step 6 we will call function that we have explained in Step 1-5

7

Once this script executes successfully we can see list setting updated by navigating “Advanced Settings” of the list as shown below-

8

Operation: How to enable “Quick Launch” Option for List

We can see this setting available under “General Settings” for the list as shown below-

9

We can play with this setting using PowerShell as described below-

10

In Step 1 we will get the object reference to the respective list by calling “GetByTitle” method

In Step 2 we will set “OnQuickLaunch” to True

In Step 3 we will update the list property by calling “Update” method

In Step 4 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method

In Step 5 we will display success message to the Users if Step 4 executes successfully

In Step 6 we will call function that we have explained in Step 1-5

11

Once this script executes successfully we can see list setting updated by navigating “General Settings” of the list as shown below-

12

Operation: How to enable “Version History” for List Item Attachments

We can see this setting available under “Versioning Settings” for the list as shown below-

13

We can play with this setting using PowerShell as described below-

14

In Step 1 we will get the object reference to the respective list by calling “GetByTitle” method

In Step 2 we will set “EnableVersioning” to True

In Step 3 we will update the list property by calling “Update” method

In Step 4 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method

In Step 5 we will display success message to the Users if Step 4 executes successfully

In Step 6 we will call function that we have explained in Step 1-5

15

Once this script executes successfully we can see list setting updated by navigating “Versioning Settings” of the list as shown below-

16

We may work on additional version settings as well as shown below-

17

For example let try to enable Minor Versions for a document library “ProductDesigns” by using PowerShell

18

In Step 1 we will get the object reference to the respective list by calling “GetByTitle” method

In Step 2 we will set “EnableMinorVersions” to True

In Step 3 we will update the list property by calling “Update” method

In Step 4 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method

In Step 5 we will display success message to the Users if Step 4 executes successfully

In Step 6 we will call function that we have explained in Step 1-5

19

Once this script executes successfully we can see list setting updated by navigating “Versioning Settings” of the list as shown below-

20

Operation: How to enable “Require Check Out” Option

We can see this setting available under “Versioning Settings” for the list as shown below-

21

We can play with this setting using PowerShell as described below-

22

In Step 1 we will get the object reference to the respective list by calling “GetByTitle” method

In Step 2 we will set “ForceCheckOut” to True

In Step 3 we will update the list property by calling “Update” method

In Step 4 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method

In Step 5 we will display success message to the Users if Step 4 executes successfully

In Step 6 we will call function that we have explained in Step 1-5

23

Once this script executes successfully we can see list setting updated by navigating “Versioning Settings” of the list as shown below-

24

Operation: How to enable “Require Content Approval” Option

We can see this setting available under “Versioning Settings” for the list as shown below-

25

We can play with this setting using PowerShell as described below-

26

In Step 1 we will get the object reference to the respective list by calling “GetByTitle” method

In Step 2 we will set “EnableModeration” to True

In Step 3 we will update the list property by calling “Update” method

In Step 4 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method

In Step 5 we will display success message to the Users if Step 4 executes successfully

In Step 6 we will call function that we have explained in Step 1-5

27

Once this script executes successfully we can see list setting updated by navigating “Versioning Settings” of the list as shown below-

28

Operation: How to configure “Draft Item Security” Option

We can see this setting available under “Versioning Settings” for the list as shown below-

29

We can play with this setting using PowerShell as described below-

30

In Step 1 we will get the object reference to the respective list by calling “GetByTitle” method

In Step 2 we will set “DraftVersionVisibility” to “Approver”. This setting will limit access to the draft items for the Approvers only.

In Step 3 we will update the list property by calling “Update” method

In Step 4 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method

In Step 5 we will display success message to the Users if Step 4 executes successfully

In Step 6 we will call function that we have explained in Step 1-5

31

Once this script executes successfully we can see list setting updated by navigating “Versioning Settings” of the list as shown below-

32

That is all for this demo.

Hope you find it helpful.

SharePoint Migration – Large List Details

This is article is next in the series of articles on “SharePoint Migration & Planning” Strategies. You can reach out to the previous articles in this series using the following links:

SharePoint Migration: Planning & Guidance On SharePoint Objects
SharePoint Migration – Export IIS Settings
SharePoint Migration – Export Alternate Access Mapping
SharePoint Migration – Export Content Database Details

In this article we will look for the PowerShell Scripts to export “SharePoint Large Lists” Report from source SharePoint Farm. This information will be helpful to track all the lists which contains more than “2000” items in it and provisioned in SharePoint Farm.

In Step 1 we will add the PowerShell Snapin to PowerShell Script as usual.

1

In Step 2 we define a function and initiate the export CSV file with Column Headers. For this demo I am exporting a few important properties like “WebApp Name, WebApp Url, Site Collection Url, Site Name, Site URL, List Name, List Item Count” but you may query all possible properties as you deemed fit

In Step 3 we execute the “Get-SPWebApplication” cmdlet to query the “Sites” & “Webs” Properties

In Step 4 we loop through the Sites Collection for a specific Web Application

In Step 5 we loop through the Webs Collection for a specific Site

In Step 6 we loop through the Lists Collection for a specific Web

In Step 7 we will filter all those lists which are having over “2000” items in them

In Step 8 we add the content of properties for each of the list to the CSV file

Always remember to dispose SharePoint Site & Web objects to avoid memory leaks. In Step 9 we will call the “dispose()” method

2

In Step 10 we will initialize path variables for export file & web applications. You can further extend this step by adding little bit of more automation flavor to make it more dynamic by reading parameters from input settings file

3

Once this script get executed successfully, it will export the Large List Details in a CSV File as shown below in Step 11

4

We can see the exported details as shown below in Step 12

5

Code Reference:

Add-PSSnapin "Microsoft.SharePoint.PowerShell"Add-PSSnapin "Microsoft.SharePoint.PowerShell"
function Get-Large-List-Report(){ Try {        if (Test-Path $settingsFilePath)        {            Remove-Item $settingsFilePath        }
        Add-Content $settingsFilePath "WebApp Name,WebApp Url,Site Collection Url,Site Name,Site URL,List Name,List Item Count"
        $spWebApplication = Get-SPWebapplication $webApplicationUrl
        foreach($spSite in $spWebApplication.sites)        {            foreach($spWeb in $spSite.AllWebs)            {                foreach($spList in $spWeb.Lists)                {                    if($spList.ItemCount -gt 2000)                    {                        $content = $spWebApplication.Name + "," + $spWebApplication.Url + "," + $spSite.Url + "," + $spWeb.Title + "," + $spWeb.Url + "," + $spList.Title + "," +  $spList.ItemCount                        Add-content $settingsFilePath $content                    }                }                $spWeb.dispose()            }            $spSite.dispose()        }    }    Catch {         Write-Host $Error -ForegroundColor Yellow }}
Clear-Host
$settingsFilePath = "<CSV File Path>"$webApplicationUrl = "<Web Application Url>"
Get-Large-List-Report

That is all for this demo.

Hope you find it helpful.

SharePoint Migration – Export Content Database Details

This is article is next in the series of articles on “SharePoint Migration & Planning” Strategies. You can reach out to the previous articles in this series using the following links:

SharePoint Migration: Planning & Guidance On SharePoint Objects
SharePoint Migration – Export IIS Settings
SharePoint Migration – Export Alternate Access Mapping

In this article we will look for the PowerShell Scripts to export “SharePoint Content Databases” details from source SharePoint Farm. This information will be helpful to track all the “Content Databases” provisioned in SharePoint Farm.

In Step 1 we will add the PowerShell Snapin to PowerShell Script as usual

1

In Step 2 we define a function and initiate the export CSV file with Column Headers. For this demo I am exporting a few important properties like “Id, Content Database Name, Web Application Name, Server Name, Current Site Count” but you may query all possible properties as you deemed fit

In Step 3 we execute the “Get-SPContentDatabase” cmdlet to query the required properties

In Step 4 we loop through the properties collection for all Content Databases and list out the queried properties for each database

In Step 5 we add the content of properties for each of the Content Database to the CSV file

2

In Step 6 we will set the settings file path and call the function to export the Content Database Details

3

Once this script get executed successfully, it will export the Content Database Details in a CSV File as shown below in Step 7

4

We can see the exported details as shown below in Step 8

5

Code Reference:

Add-PSSnapin "Microsoft.SharePoint.PowerShell"Add-PSSnapin "Microsoft.SharePoint.PowerShell"
function Get-Content-Databases(){ Try {        if (Test-Path $settingsFilePath)        {            Remove-Item $settingsFilePath        }
        Add-Content $settingsFilePath "Id,Content Database Name,web Application Name,Server Name,Current Site Count"
        $contentDBSettings = Get-SPContentDatabase | Select Id,Name,webApplication,Server,CurrentSiteCount
        foreach ($contentDBSetting in $contentDBSettings)        {            $id = $contentDBSetting.Id            $name = $contentDBSetting.Name            $webApplication = $contentDBSetting.webApplication            $server = $contentDBSetting.Server            $currentSiteCount = $contentDBSetting.CurrentSiteCount
            $settings = "$id,$name,$webApplication,$server,$currentSiteCount"             Add-content $settingsFilePath $settings        }    }    Catch {         Write-Host $Error -ForegroundColor Yellow }}
Clear-Host
$settingsFilePath = "<CSV File Path>"
Get-Content-Databases

That is all for this demo.

Hope you find it helpful.

 

 

 

 

SHAREPOINT MIGRATION – EXPORT Alternate Access Mapping

This is article is next in the series of articles on “SharePoint Migration & Planning” Strategies. You can reach out to the previous articles in this series using the following links:

  1. SHAREPOINT MIGRATION: PLANNING & GUIDANCE ON SHAREPOINT OBJECTS
  2. SHAREPOINT MIGRATION – EXPORT IIS SETTINGS

In this article we will look for the PowerShell Scripts to export “Alternate Access Mapping (AAMs)” Settings from source SharePoint Farm. This information will be helpful to track all the “AAMs” defined in SharePoint Farm.

In Step 1 we will add the PowerShell Snapin to PowerShell Script as usual

1

In Step 2 we define a function and initiate the export CSV file with Column Headers. For this demo I am exporting a few important properties like “Incoming Url, Zone, Public Url” but you may query all possible properties as you deemed fit

In Step 3 we execute the “Get-SPAlternateURL” cmdlet to query the required properties

In Step 4 we loop through the properties collection for all AAM Mapping and list out the queried properties for each mapping

In Step 5 we add the content of properties for each of the AAM Mapping to the CSV file

2

In Step 6 we will set the settings file path and call the function to export the AAM Mappings

3

Once this script get executed successfully, it will export the AAM Mappings in a CSV File as shown below in Step 7

4

We can see the exported mappings as shown below in Step 8

5

Code Reference:

Add-PSSnapin "Microsoft.SharePoint.PowerShell"Add-PSSnapin "Microsoft.SharePoint.PowerShell"
function Get-Alternate-Access-URLs(){ Try {        if (Test-Path $settingsFilePath)        {            Remove-Item $settingsFilePath        }
        Add-Content $settingsFilePath "Incoming Url, Zone, Public Url"
        $aamSettings = Get-SPAlternateURL | Select IncomingUrl,Zone,PublicUrl
        foreach ($aamSetting in $aamSettings)        {            $incomingUrl = $aamSetting.IncomingUrl            $zone = $aamSetting.Zone            $publicUrl = $aamSetting.PublicUrl
            $settings = "$incomingUrl, $zone, $publicUrl"             Add-content $settingsFilePath $settings        }    }    Catch {         Write-Host $Error -ForegroundColor Yellow }}
Clear-Host
$settingsFilePath = "C:\Prashant\PowerShell\SharePoint Migration\PowerShell - Get-Alternate-Access-URLs\Alternate-Access-Urls.csv"
Get-Alternate-Access-URLs

That is all for this demo.

Hope you find it helpful.

SharePoint Migration – Export IIS Settings

This is article is next in the series of articles on “SharePoint Migration & Planning” Strategies. You can reach out to the previous articles in this series using the following links:

SharePoint Migration: Planning & Guidance On SharePoint Objects

In this article we will look for the PowerShell Scripts to export IIS Settings from source SharePoint Farm. This information will be helpful to track all the “Application Pool” accounts used to configure Web Applications in the Farm.

There could be more properties that you can export but for this demo I am considering following important properties:

Site Name, Site Path, Application Pool, Bindings, Identity, Password

In Step 1 we will import module “WebAdministration” which gives us the methods to work with “IIS Manager” Objects

1

In Step 2 we will retrieve items from IIS directory using wildcard path search “IIS:\Sites\*”

In Step 3 we will enlist the header of the output Settings file by using the name of properties that we are pulling from IIS

In Step 4 we will fetch properties “Site Name, Site Path, Application Pool, Bindings, Identity, Password” for each IIS Site

In Step 5 we will append the properties to the output file for each of the IIS sites we have in Farm

2

In Step 6 we are specifying the path for output file which would be in the form of CSV

In Step 7 we are calling the function executing Step 1-6

3

Once the script executed successfully it will generate the output file as shown below-

4

And here is the data that was added to the output file during the execution of the above script

We can see IIS details for each Site (Web Application) added to the SharePoint Farm and this information is really crucial to document during the Migration planning phase.

5

Code Reference:

Import-Module WebAdministration
function Get-IIS-Settings(){    param($ListName,$ScriptPath,$FileDateTimeStamp) Try {        if (Test-Path $settingsFilePath)        {            Remove-Item $settingsFilePath        }
        $iisSites = Get-Item IIS:\Sites\*
        Add-Content $settingsFilePath "Site Name , Site Path , Application Pool , Bindings , Identity , Password"
        foreach ($iisSite in $iisSites)        {            $appPoolName = $iisSite.ApplicationPool            $appPool = get-item "IIS:\AppPools\$appPoolName"            $processModel = $appPool.processModel            $appPoolIdentity = $processModel.username            $appPoolIdentityPassword = $processModel.password            $iisBindings = $iisSite.Bindings            $iisBindingCollection = $iisBindings.Collection             $siteName = $iisSite.Name            $sitePath = $iisSite.physicalPath
            $settings = "$siteName, $sitePath, $appPoolName,$iisBindingCollection,$appPoolIdentity, $appPoolIdentityPassword"             Add-content $settingsFilePath $settings        }
    }    Catch {         Write-Host $Error -ForegroundColor Yellow }}
Clear-Host
$settingsFilePath = "C:\Prashant\PowerShell\SharePoint Migration\IISSettings.csv"
Get-IIS-Settings

Hope you find it helpful.

SharePoint Migration: Planning & Guidance on SharePoint Objects

1

While performing SharePoint Migration we need to do lot of planning before actually starting even with the basic step on migration.

Planning includes well defined list of SharePoint Artifacts, Settings, or Configurations that must be identified beforehand and accordingly migration strategy can be worked out.

SharePoint Migration involves the following stages:

  • Inventory Audits
  • Configuration Migration
  • Schema Migration
  • Data Migration
  • Validation

In this article I will explain you detailed list of SharePoint Inventory that must be pulled out and kept stored as backup safely atleast during the migration phase.

 SharePoint Inventory Map

Below is the small Inventory Map for SharePoint Objects & Artifacts that we should always consider during the Planning Phase for SharePoint Migration.

In the upcoming sections of this article we discuss each of the Object/Artifact in more details from data collection stand point.

2

SharePoint Objects & Properties

Here is list of objects that should be queried during the initiation phase of migration.

SharePoint Farm

We should query SharePoint Farm Object to get it Properties, Configurations settings, Level of SharePoint Logging and Server details which are associated to this farm

 

3

Configuration & Settings

We should query SharePoint to get configuration settings for IIS, SharePoint Designer, Alternate Access and Common Web Service (SPServiceHostConfig)

4

Databases

We should query SharePoint to get details on databases designated as Content & Configuration databases associated with current Farm

5

Web Application, Sites, (Site Map)

We should query SharePoint to get details on Web Applications, Site Collections, Sites Subsites (Site Map), Site Templates especially the custom templates if you want to have sites based on these custom templates at destination too

6

Lists & Libraries

We should query SharePoint to get details on List Schema, Item Count, List Permissions, Item Specific Permissions if any and also report of large lists. This information will very helpful while migrating Schema & data at later stage of Migration process

7

Accounts, Authentication & Claim Providers

We should query SharePoint to get details on Managed Accounts, Application Pool Accounts, Authentication Providers and Installed Certificates Information.

8

Custom Solutions

We should query SharePoint to get details on custom WebParts, Features, Time Jobs, Layout Pages and other solutions (WSPs). This information is really important to migrate or rewrite functionalities as deemed fit. We also need to gather source code for each of the custom solutions as it might be required during migration issues encountered during/after migration.

9

Service Applications

We should query SharePoint to get details on Service Applications, Application Instances, Load Balancer Configuration. Information on Server Applications is important to help understand and troubleshoot issues during/after migration

10

Business Connectivity Service Applications

We should query SharePoint to get details on configuration settings for BCS data connection files. This will be really important to know all connection details for LOBs connected using BCS.

11

Search Service Applications

Search is one of the most complex Service applications when it comes to migration. So it really important that we should detailed out each granular piece of information associated with Search service applications.

We should query SharePoint to get details on configuration settings for Search Service Applications, Query Keywords, Query Scope, Query Rules, Query Suggestion, Ranking Model, Security Trimmer, and Application Proxies.

12

Application Specific Properties & Settings for Service Applications

Most Service applications expose a set of properties specific to each of them. It is important detailed out all possible details on each of application specific properties that we can query from SharePoint as shown below

13

I will keep on updating this article as I get more relevant information to include so that it could be proved as one stop reference for SharePoint Migration Guidance.

In the upcoming articles on SharePoint Migration I will show you implementation details on how to gather the inventory for each of the above mention objects.

So stay tuned.

Hope you find it helpful.

SharePoint Online: Working With List Content Types Using PowerShell

In this article we will discuss the configuration of some of the important operations on Content Type for SharePoint Online Lists. Though I have taken only a handful of operations to showcase in this demo, but remember there are lot more to explore.

Operation: How to enable “Content Types” for List

We can see this setting available under “Advanced Settings” for the list as shown below-

1

We can play with this setting using PowerShell as described below-

2

In Step 1 we will get the object reference to the respective list by calling “GetByTitle” method

In Step 2 we will set “ContentTypesEnabled” to True

In Step 3 we will update the list property by calling “Update” method

In Step 4 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method

In Step 5 we will display success message to the Users if Step 4 executes successfully

In Step 6 we will call function that we have explained in Step 1-5

3

Once this script executes successfully we can see list setting updated by navigating “Advanced Settings” of the list as shown below-

4

Operation: How to “Add Existing Content Type To List”

We can see this setting available under “List Settings” for the list as shown below-

5

We can play with this setting using PowerShell as described below-

6

In Step 1 we will get the object reference to the respective Web

In Step 2 we will get the object reference to the respective list by calling “GetByTitle” method

In Step 3 we will get the object reference to the respective list by calling “GetById” method on Content Types collection

In Step 4 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method

In Step 5 we will display success message to the Users if Step 4 executes successfully

In Step 6 we will call function that we have explained in Step 1-5

7

Once this script executes successfully we can see a new content type added by navigating “List Settings” of the list as shown below-

We can notice the new content type added to the List Content Types Collection

8

Operation: How to “Get List of All Content Types”

We can see this setting available under “List Settings” for the list as shown below-

9

We can play with this setting using PowerShell as described below-

10

In Step 1 we will get the object reference to the respective Web in context of which this code is executing

In Step 2 we will get the object reference to the respective list by calling “GetByTitle” method

In Step 3 we will get List Content Types Collection

In Step 4 we call the “Load” function to retrieve List Content Types Collection properties from server

In Step 5 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method

In Step 6 & 7 we will loop through the Content Types Collection and display relevant properties (ex. Name, ID and so on) from the collection

In Step 8 we will call function that we have explained in Step 1-7

11

Once this script executes successfully we can see list of content types by navigating “List Settings” of the list as shown below-

12

Operation: How to “Delete Existing Content Types” From List

We can see the List of Content Types already added to List under “List Settings” as shown below-

12

We can delete any Content Type from list by using PowerShell by using PowerShell as described below-

13

In Step 1 we will get the object reference to the respective Web in context of which this code is executing

In Step 2 we will get the object reference to the respective list by calling “GetByTitle” method

In Step 3 we will get List Content Types Collection

In Step 4 we call the “Load” function to retrieve List Content Types Collection properties from server

In Step 5 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method

In Step 6 we will loop through the Content Types Collection

In Step 7 we will look for required content types that are intended to delete. In this example we will compare the Content Type Name to ensure that only intended content type are deleted

In Step 8 we will call “DeleteObject” method to delete the respective content type from Content Types Collection of List

In Step 9 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method

In Step 10 we will display success message after Step 9 got executed successfully

In Step 11 we will call function that we have explained in Step 1-10

14

Once this script is executed successfully the respective content type will be deleted from Content Types List and this can we verified from the UI by navigating through “List Settings”

15

That is all for this demo.

Hope you find it helpful.

SharePoint Online: Working With List Fields Using PowerShell

In this article we will discuss the operations on List Fields (Columns), which involves Getting All Columns, Adding New Columns, Updating Existing Columns and so on.

To start with this demo we will start with a list called “Products” and perform all operations on this list.

1

Operation: How to Add New Columns To List

We can add a new column to the list by making use of the following code.

2

In Step 1 we will get the object reference to the current Web using Client Context properties

In Step 2 we will get the object reference to the respective list by calling “GetByTitle” method

In Step 3 we will define the xml of the List Column schema. You can get this XML by prototyping the list using SharePoint UI and then by using SharePoint Client Browser to look for Schema XML for Lists & Fields.

In Step 4 we will call “AddFieldAsXml” method to add field as xml schema to the list

In Step 5 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method

In Step 6 we will display the success message to the users

In Step 7 we will call function that we have explained in Step 1-5

We can see this field added to the list by browsing the list

4

We can see the details of the new column (Datatype and others) by browsing List Settings

5

Operation: How Get All Columns of List

We can get all columns used in a list by making use of the following code.

6

In Step 1 we will get the object reference to the current Web using Client Context properties

In Step 2 we will get the object reference to the respective list by calling “GetByTitle” method

In Step 3 we will get the object reference to Fields collection of the list

In Step 4 we call the “Load” function to retrieve Fields collection properties from server

In Step 5 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method

In Step 6 we will loop through the collection and display Field details to the users

In Step 7 we will call function that we have explained in Step 1-6

We can see the fields collection to the list by browsing the list

7

Operation: How to Update column of List

Let’s consider that we have to add little description the field Title, which is blank currently

8

We can update an existing column to the list by making use of the following code.

9

In Step 1 we will get the object reference to the current Web using Client Context properties

In Step 2 we will get the object reference to the respective list by calling “GetByTitle” method

In Step 3 we will get the object reference to the respective field by calling “GetByTitle” method

In Step 4 we will set the Description property of List object with the required value

In Step 5 we will call “Update” method to save these changes back to SharePoint List

In Step 6 we will call “Load” method to retrieve updated properties (Description) of the field from Server

In Step 7 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method

In Step 8 we will display Field details to the users to the users

In Step 7 we will call function that we have explained in Step 1-8

10

We can see description of Title field is update to the list by browsing the field properties under list settings

11

Operation: How To Add Existing Site Columns To List

Since we are planning to add an existing Site Column to the list, it is necessary to ensure the existence of Site Column. We can verify this by navigating “Site Settings > Site Columns”.

For this demo I already have added a Site Column “ProductOwner” that we can see under “Custom Columns” group as shown below-

12

And we can also verify the list settings to ensure that “ProductOwner” Column is not added to the list earlier

13

Now we will look into code to add existing Site Column to the list as explained below-

14

In Step 1 we will get the object reference to the current Web using Client Context properties

In Step 2 we will get the object reference to the respective list by calling “GetByTitle” method

In Step 3 we will get the object reference to Fields collection of the Web. It is important to note that Site Columns are the part of Web Fields Collection not List Fields Collection. So we have to make use of Web object reference to look for existing Site Columns.

In Step 4 we call the “Add” function on “Fields” Collection of the list to add the reference of the Site Column from Step 3

In Step 5 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method

In Step 6 we will call function that we have explained in Step 1-5

We can see the field collection to the list by browsing the list settings-

16

We can further look into column details by clicking it

17

Operation: How Set Default Value For Field

We can set default values to SharePoint List Fields programmatically by using the following code as explained below-

18

In Step 1 we will get the object reference to the current Web using Client Context properties

In Step 2 we will get the object reference to the respective list by calling “GetByTitle” method

In Step 3 we will get the object reference to Fields collection of the list

In Step 4 we will call “DefaultValue” property of Field Object and assign it a value of our choice

In Step 5 we will call “Update” method of Field Object, which will update the “DefaultValue” property back into database

In Step 6 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method

In Step 7 we will display the a success message which is informing users about the status of operation

In Step 8 we will call function that we have explained in Step 1-7

19

We can see this value shown as default value whenever a new Item has been created (programmatically or using browser) as shown below-

20

That is all for this demo.

Hope you find it helpful.

SharePoint Online: Working with Lists Using PowerShell

In this article we will discuss SharePoint List operations using PowerShell for SharePoint Online.

To start with this demo, I have created a demo SharePoint Online Site that I will use to perform SharePoint operations in this and few of the upcoming articles in this series.

1

In case you are not aware of on how to start with PowerShell development for SharePoint Online, I would rather recommend you to first go through one of my previous articles. This article will help you to setup development environment and get you started.

SharePoint Online: How To Install SharePoint Online Management Shell

Provide SharePoint Online Site credential when it is asked for

2

Operation: How To Get Lists Collection

3

In Step 1 we will initialize Web Object using SharePoint Client Context

In Step 2 we initialize SharePoint Lists Collection

In Step 3 we call the “Load” function to retrieve SharePoint List Collection properties from server

In Step 4 we call “ExecuteQuery” method to send the request to SharePoint Server

In Step 5 we iterate the lists collection returned back from the server and display required list properties as output

In Step 6 we call function that we have explained in Step 1-5

And here is the output for this operation

4.1

Operation: How To Add New List

4

In Step 1 we will initialize “ListCreationInformation” object which allows to specify the metadata details for this new list

In Step 2 we will specify Title for new list

In Step 3 we will specify Description for new list

In Step 4 we will specify List Template for new list. In this example we will use Custom List Template

In Step 5 we will call “Add” method of “Lists” collection for SharePoint Web

In Step 6 we will call “Load” method, to retrieve the properties of list object from Server

In Step 7 we call “ExecuteQuery” method to send the request to SharePoint Server

In Step 8 we call function that we have explained in Step 1-7

On successful execution of Step 1-7 we will get list added to SharePoint and Internal ID of list as Output

And here is the output for this operation

5

We can also see to the SharePoint Site to ensure that a new list has been added

67

Operation: How To Update Existing List

8

In Step 1 we will initialize “List” object which allows to modify the metadata details for this existing list

In Step 2 we will update the description of the existing list

In Step 3 we will call “Update” function of list object and then call “Load” method, to retrieve the properties of list object from Server

In Step 4 we call “ExecuteQuery” method to send the request to SharePoint Server

In Step 5 we will retrieve the list description to ensure that it is updated successfully

In Step 6 we call function that we have explained in Step 1-6

And here is the output for this operation

9

We can also see to the SharePoint Site to ensure that the list description has been updated

10

Operation: How To Delete Existing List

11

In Step 1 we will initialize “List” object by calling a method “GetByTitle” on the Web Object

In Step 2 we will call “DeleteObject” method on List Object that will delete the reference of respective list from lists collection

In Step 3 we call “ExecuteQuery” method to send the request to SharePoint Server

In Step 4 we will display success message to inform successfully deletion of the list

In Step 5 we call function that we have explained in Step 1-4

And here is the output for this operation

12

We can also see to the SharePoint Site to ensure that the list has been deleted successfully from SharePoint

13

That is all for this demo.

Stay tuned for the upcoming articles in this series.

Hope you find it helpful.