CSOMOffice 365PowerShellVisual Studio 2013

Tip: Create New List in SharePoint Online Using Exception Handling Scope and Set Properties | PowerShell | CSOM


While presenting about SharePoint Online Client Side Object Model a question popped up “Hey! How can I create a new list if it’s not exist and change properties if exists?”. Not a difficult one. However, we need to build codes with best practise suggested by Microsoft. In short answer is available here.


  • Create a List if it’s not existing.
  • Change Properties if it exists.
  • Allow to create multiple lists in one run
  • Allow users to choose the list template.


Build a binary cmdlet using C# and meet your needs by parameterizing the code. Look at the code below.

using System;
using System.Management.Automation;
using Microsoft.SharePoint.Client;
namespace xSharePointOnline
    [Cmdlet(VerbsCommon.New, "SPOList")]
    public class NewSPOList : PSCmdlet
        [Parameter(Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public Uri SPOurl;
        [Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, ValueFromPipeline = true)]
        public string SPOListName;
        [Parameter(Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public ListTemplateType SPOListTemplateType;
        [Parameter(Mandatory = true)]
        public PSCredential SPOCredential;
        protected override void ProcessRecord()
            using (ClientContext SPOClientContext = new ClientContext(SPOurl))
                SPOClientContext.Credentials = new SharePointOnlineCredentials(SPOCredential.UserName, SPOCredential.Password);
                ExceptionHandlingScope Scope = new ExceptionHandlingScope(SPOClientContext);
                using (Scope.StartScope())
                    using (Scope.StartTry())
                        List oList = SPOClientContext.Web.Lists.GetByTitle(SPOListName);
                    using (Scope.StartCatch())
                        ListCreationInformation oListInformation = new ListCreationInformation();
                        oListInformation.Title = SPOListName;
                        oListInformation.TemplateType = (int)SPOListTemplateType;
                        List oList = SPOClientContext.Web.Lists.Add(oListInformation);
                    using (Scope.StartFinally())
                        List oList = SPOClientContext.Web.Lists.GetByTitle(SPOListName);
                        oList.Hidden = true;
                        oList.Description = "PowerShell Rocks!";

How to use it?

  • Create a C# Class Library
  • Copy and paste the code. If required change the parameters as required.
  • Build it to get the DLL (Binary)
  • Using Import-Module load the binary DLL E.G. Import-Module C:\Location\Solution.DLL
  • Run the cmdlet New-SPOList -SPOUrl -SPOListName -SPOListTemplateType -SPOCredential

Here the SPOListTemplate populate the enum values of List Template – So, we can choose the one we need by tab completion

How it works?

We have used Exception handling scope which makes one call to the server and that means a lot of performance improvement. All three Try, catch and finally executes at one time.
This code simply checks the existence of the list and if exists it will update the hidden and description properties.
If try catch throws exception it will create a new list – If in case of the list not existing.
Update the lists properties.

Screen Shot


Leave a Reply

Your email address will not be published. Required fields are marked *