Some thoughts on programming stuff

List operations with CSOM and C# in SharePoint

Sometimes we have to query a SharePoint list through remote connections using C#. It can happen in a SharePoint App, a Console Application or maybe on an Azure Function. This post will help on performing list operations with CSOM and C# in SharePoint.

Below you can find some code snippets that will help you on integrating SharePoint lists with your application.

Basically, you will have to add the Microsoft.SharePoint.Client namespace.

using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;

The required DLLs for programming these type of solution are:

Microsoft.SharePoint.Client.dll
Microsoft.SharePoint.Client.Runtime.dll

You can find them inside those folders:

SharePoint 2010: 14 Hive -> Folder: ISAPI

SharePoint 2013: 15 Hive -> Folder: ISAPI

Or just download it from MSDN, inside the SharePoint Server 2013 Client Components SDK!

After adding these references to our project, we can start coding and accessing the data in the SharePoint List you want!

A custom list creation screen

Soon you will notice that everything depends on the ClientContext object, which manages the requests and the data that will be loaded.

In our examples we are going to use the variable siteUrl, that represents the URL for the Site Collection. The variable listTitle will also be used and it represents the Title of the list being used.

string siteUrl = "http://sitesharepoint";
string listTitle = "SharePointList";

So, let’s do some real stuff here:

Read operation in SharePoint lists

Filtering and selecting specific fields

The example below presents a simple code that queries only one list item from the List and specifies some specific columns to be returned. It’s important to say that exists lots of best practices to build good queries in SharePoint.

Creation operation in SharePoint lists

 Update operation in SharePoint lists

 Delete operation in SharePoint lists

I believe these are the basics for consuming SharePoint lists. In the future I’m planning to show some complex examples, more focused on Office 365 and Azure integrations. Thanks for reading and I hope it can be useful to you when you need to perform list operations with CSOM and C# in SharePoint.

References:
MSDN

4 Comments

  1. paula

    wellDone!

    • Wiliam Rocha

      Thanks, Paula!

  2. ruzaib saiyed

    How to Multiple list update ?

    • Wiliam Rocha

      You need to call them in batches and transform the code in a sequential foreach for example.

Leave a Reply

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

© 2024 wiliammbr's blog

Theme by Anders NorenUp ↑