,

Setup CSOM within VSCode

Posted by

I recently ran across an interesting request where I had to dig in and provide some basics around setting up CSOM within VS Code so I thought this would be helpful. to cover the basics. 

This blog will answer the following:

  • What is CSOM
  • Why CSOM at this point?
  • Should I use a CS file or can I actually write the code in PowerShell
  • How do I setup VS Code so I can write CSOM

What is CSOM?

CSOM stands for Client-Side Object Model.  It’s an approach to write client-side managed code that runs on the client.  This was a huge win when it was introduced several years ago as most custom code processing was done server side back in the on-premises days.  You can retrieve the assembly either from download packages or even possibly stored in Nuget packages that you can retrieve with node package manager.

Why CSOM?

The PowerShell community has created so many modules, the need to write CSOM is likely not required.  The only reasons I can think an organization would write CSOM:

1. Need to create some functionality that doesn’t exists with any oob PowerShell Modules or the plethora of modules available on GitHub and the PowerShell gallery.  Again, this is not very likely. 

2. The organization is not permitted to use open-source PowerShell modules that are widely available on GitHub.  This is far more likely.

For item 1 above, if that is the case I would highly recommend looking into some other approaches as CSOM is not the most common use case anymore.   In a modern client-side development environment, I would instead look at the PnP Core SDK.  For Example, If I’m working with SharePoint Site Collections I would start here: 

Site Collections | PnP Core SDK

For item 2 above, you might want to talk to your internal teams and have those conversations to better understand why adopting open-source community is totally out of the question.  Microsoft and many other organizations worldwide have embraced open source.   Why reinvent the wheel?  Plus, Microsoft documentation provides links to several open-source examples out there.  Take SharePoint for example:

SharePoint developer documentation | Microsoft Learn

Should I write it in C# or can I write it using PowerShell?

This is up to you.  There are pros and cons for each scenario.  If you’re writing in C#, you’ll need to compile that code in order to run it which I assume would require close inspection and validation by an internal security team, etc.  I prefer writing in PowerShell for this very reason assuming no built-in modules exists.  Plus, PowerShell has built-in command-lets making this easy. 

How do I setup VS Code so I can write CSOM?

First, you’ll need to know what assembly (dll files) you’ll need to reference.  In my example, I’m going to leverage write CSOM to authenticate to a SharePoint Site in order to retrieve the Title and Description of that site. 

Download SharePoint Online Client Components SDK from Official Microsoft Download Center

  • download and install the SharePoint Client Components SDK (yes you could also go with the nuget package but I’m not doing that
  • within file explorer, create an empty folder which my folder name is csomfun.
  • Open csom folder and create a subfolder called assembly
  • Copy the  (Client, ISAPI, and Template Folders)  folders from here:  %ProgramFiles%\Common Files\Microsoft Shared\Web Server Extensions\16 .
  • Paste the three folders to c:\csomfun\assembly

It should look like:

within assembly directory

  • Open VS Code and go to File, Open Folder and I selected c:\csomfun
  • Click the +file icon to create new file and give it a name with the .ps1 extension. I called mine hello.ps1

Updated my code with the following:


Lines 1-3:   I’m adding my assembly.

Lines 6-8: providing credentials and site URL I wish to connect to.

Line 11:  converting the password to a secure string.

Line 14: creating new client context object passing in the site url.

Line 15: Applying the credentials to the client context object

Line 19, 20:  loading the web for the client context and executing the query. 

Line 23, 24: writing the title and description of the site to the console.

Final Thoughts

Writing CSOM has been around for a long time. I fortunately haven’t had to write any in VS Code so this is partially what inspired the blog.  I hope this helps.

Thank You,

Russ Maxwell