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

Sathish Nadarajan
 
Solution Architect
May 24, 2017
 
Rate this article
 
Views
9814

 

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.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment