Recent Comments

Summary

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.

Requirement

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.

Solution

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

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.
Try
This code simply checks the existence of the list and if exists it will update the hidden and description properties.
Catch
If try catch throws exception it will create a new list – If in case of the list not existing.
Finally
Update the lists properties.

Screen Shot

Published by

Chen V

I am a SharePoint 2010 IT Pro. My area of interests are Workflows, Automation, Office 365 , Identity and Access Management, PowerShell and Windows Servers.
View all posts by Chen V