FMTFMT

How To Read Various Objects Using SharePoint Online Client Side Object Model (CSOM)

SharePoint developers have been using server side object model up until the latest release of SharePoint on premise but that approach does not work with Office 365 based SharePoint online. For C# developers Microsoft introduced .NET based API for reading and writing data to SharePoint. Developers may use Client Side Object Model to integrate SharePoint with other systems like ERP or CRM. CSOM can be used to build custom apps on top of SharePoint online. In this blog, we are just going to focus on reading various high-level objects in SharePoint Online.

SharePoint Client Side Object Model (CSOM) allows developers to retrieve, update and manage data in SharePoint Online. SharePoint Online makes the CSOM available in several forms.

  1. .NET Framework redistributable assemblies
  2. Javascript library
  3. REST/OData endpoints
  4. Windows Phone assemblies
  5. Silverlight redistributable assemblies

In this blog post, we will focus on .NET framework based approach of reading data from SharePoint.

SharePoint Client Side Object Model Intro

SharePoint Online Client Side Object Model (CSOM) allows developers to interact with SharePoint online objects like web, list, library, and fields.

There are two methods of getting access on the SharePoint online CSOM:

For on-premises SharePoint there are different client components SDK available:

SharePoint Server 2016 Client Components SDK

SharePoint Server 2013 Client Components SDK

How to connect to SPO site

In order to read from SharePoint online site first step is to authenticate with Office 365 using a username and password. Following code snippet will authenticate client application with SPO.

           Console.WriteLine("Enter password for site collection");

           SecureString password = GetPasswordFromConsoleInput();

           string userName = "araheem@sfpxdev.onmicrosoft.com";

           var ctx = new ClientContext("https://sfpxdev.sharepoint.com");

           ctx.Credentials = new SharePointOnlineCredentials(userName, password);

How to retrieve sub sites

Once program has loaded clientcontext object, we can load objects that we want to query from SPO. In this case we want to read sub sites under a top level site.

           ctx.Load(ctx.Web);

           ctx.Load(ctx.Web.Webs);

           ctx.ExecuteQuery();

           Console.WriteLine(ctx.Web.Title);

           foreach (var subweb in ctx.Web.Webs)

           {

                    Console.WriteLine(subweb.Title);

           }

How to retrieve all libraries

SPO client side object model gives you a list of all list and libraries, then we can filter on BaseTemplate property to display document libraries.

    ctx.Load(ctx.Web.Lists);

           ctx.ExecuteQuery();

           foreach (var list in ctx.Web.Lists)

           {

               if (list.BaseTemplate == 101)

               {

                   Console.WriteLine("List Title: {0}", list.Title);

               }

           }

    

How to retrieve a specific document library

GetByTitle method of Lists collection requires a document library name in order to retrieve a specific list or library.

           List docLib = ctx.Web.Lists.GetByTitle("Documents");

           ctx.Load(docLib);

           ctx.ExecuteQuery();

           Console.WriteLine(docLib.Title);

           Console.WriteLine(docLib.Id);

How to retrieve all lists

SharePoint Online CSOM object model requires loading lists collection in clientcontext in order to retrieve all lists under a site.

    ctx.Load(ctx.Web.Lists);

           ctx.ExecuteQuery();

           foreach (var list in ctx.Web.Lists)

           {

               Console.WriteLine("List Title: {0}", list.Title);

           }

How to retrieve a specific list

           List businessContacts = ctx.Web.Lists.GetByTitle("Business Contacts");

           CamlQuery camlQuery = new CamlQuery();

           camlQuery.ViewXml = "" +

                                   "" +

                                       "" +

                                           "" +

                                               "" +

                                               "1"+

                                           "" +

                                       "" +

                                   "" +

                                   "1" +

                               "";

           ListItemCollection collListItem = businessContacts.GetItems(camlQuery);

           ctx.Load(collListItem);

           ctx.ExecuteQuery();

           foreach (ListItem oListItem in collListItem)

           {

               Console.WriteLine("ID: {0} nFull Name: {1} nWork Phone: {2}", oListItem.Id, oListItem["FullName"], oListItem["WorkPhone"]);

           }

Download full source code here.

Developers can now bring existing C# and .NET development skills to interact with cloud-based systems. This blog is just touching the surface on CSOM based development in SharePoint Online but developers can do a lot more than that. For full descriptions of supported operation, please visit Office Microsoft Documentation here. If you have any questions please feel free to let us know by submitting the contact form below.

©
2023
 FMT Consultants, LLC.
|
Privacy Policy
X
FMT

Contact Us

X
FMT

Newsletter Sign-up

menu linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram