How to Create List, Add Site Columns and Add Content Types in SharePoint Office 365 using C# CSOM Patterns and Practices


Sathish Nadarajan
SharePoint MVP
Published On :   24 May 2017
Visit Count
Today :  4    Total :   795
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

SharePoint Office 365 Tool
Simple & Powerful Tool for Migration, Security & Reporting. Free Trial


 

As part of our PNP series, let us see how to create the List in Office 365 using Patterns and Practices (PNP) and add few columns and Content Types to that list dynamically.

Basically, these things will be very useful, when preparing for an end to end deployment. Sometimes, the requirements can be the site itself will be generated dynamically. At that time, we need to create all the lists, master pages, content types etc., as part of the site creation process itself. During those kind of scenarios, the below code will be very handy.

The Code snippets are very straight forward, hence let us see the code directly.

1. Create the List

 public static void CreateList()
         {
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
 
             string siteUrl = "https://*******.sharepoint.com/sites/communitysite";
             string userName = "Sathish@**********.onmicrosoft.com";
             string password = "************";
 
             using (var clientContext = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
             {
                 Web web = clientContext.Web;
                 clientContext.Load(web);
                 clientContext.Load(web.Lists);
                 clientContext.ExecuteQueryRetry();
                 List list = null;
                 if (!clientContext.Web.ListExists("DemoList"))
                 {
                     list = clientContext.Web.CreateList(ListTemplateType.GenericList, "DemoList", false, true, string.Empty, true);
                 }
                 else
                 {
                     list = web.Lists.GetByTitle("DemoList");
                 }
                 clientContext.Load(list);
                 clientContext.ExecuteQuery();
             }
         }
 

2. Add Content Types to List

 public static void AddContentTypeToList()
         {
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
 
             string siteUrl = "https://*******.sharepoint.com/sites/communitysite";
             string userName = "Sathish@*********.onmicrosoft.com";
             string password = "***********";
 
             using (var clientContext = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
             {
                 Web web = clientContext.Web;
                 clientContext.Load(web);
                 clientContext.Load(web.Lists);
                 clientContext.ExecuteQueryRetry();
                 List list = null;
 
                 list = web.Lists.GetByTitle("DemoList");
 
                 clientContext.Load(list);
                 clientContext.Load(list.ContentTypes);
                 clientContext.ExecuteQuery();
 
                 if (!list.ContentTypeExistsByName("DemoContentType"))
                 {
                     list.AddContentTypeToListByName("DemoContentType");
                 }
 
                 list.Update();
 
                 clientContext.Load(list);
                 clientContext.ExecuteQuery();
             }
         }
 

3. Add Fields to the List

 public static void AddFields()
         {
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
 
             string siteUrl = "https://***.sharepoint.com/sites/communitysite";
             string userName = "Sathish@********.onmicrosoft.com";
             string password = "**********";
 
             using (var clientContext = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
             {
                 Web web = clientContext.Web;
                 clientContext.Load(web);
                 clientContext.Load(web.Lists);
                 clientContext.Load(web.Fields);
                 clientContext.ExecuteQueryRetry();
                 
                 List list = web.Lists.GetByTitle("DemoList");
 
                 clientContext.Load(list);
                 clientContext.Load(list.Fields);
                 clientContext.ExecuteQuery();
 
                 Field field = web.Fields.GetByInternalNameOrTitle("DemoField");
 
                 list.Fields.Add(field);
 
                 list.Update();
 
                 clientContext.Load(list);
                 clientContext.ExecuteQuery();
             }
         }
 
 

Happy Coding,

Sathish Nadarajan.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

KWizCom Forms App