Getting List Properties From SchemaXml

There are many ways in CSOM to get the list properties from a SharePoint site.  But there are a few properties which we can't get directly from the list object. In this blog, I will explain how to find those using the SchemaXml.

This below-mentioned code sample will help us to get those properties which we cannot access directly from the list object.

The code block for this is mentioned below.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Microsoft.SharePoint.Client;  
  7. using System.Net;  
  8. using System.Security;  
  9. using Microsoft.SharePoint.Client.Utilities;  
  10. namespace SchemaXml {  
  11.     class GetListSchema {  
  12.         static void Main(string[] args) {  
  13.             string listName = "CopyDoc";  
  14.             string username = string.Empty;  
  15.             string password = string.Empty;  
  16.             SecureString secureString = new SecureString();  
  17.             SharePointOnlineCredentials credentials = null;  
  18.             List list = null;  
  19.             System.Xml.Linq.XDocument xDocument;  
  20.             string modified = string.Empty;  
  21.             try {  
  22.                 username = "[email protected]";  
  23.                 password = "******";  
  24.                 foreach(char ch in password.ToCharArray())  
  25.                 secureString.AppendChar(ch);  
  26.                 ClientContext ctx = new ClientContext("siteUrl"); //creating ClientContext from given SharePoint Site  
  27.                 credentials = new SharePointOnlineCredentials(username, secureString); //creating creadential object for office365 site.  
  28.                 ctx.Credentials = credentials;  
  29.                 Web web = ctx.Web;  
  30.                 list = ctx.Web.Lists.GetByTitle(listName);  
  31.                 ctx.Load(list, l => l.SchemaXml, //loading schemaXml  
  32.                     l => l.EnableVersioning);  
  33.                 ctx.ExecuteQuery();  
  34.             } catch (Exception ex) {}  
  35.             try {  
  36.                 using(System.IO.StringReader reader = new System.IO.StringReader(list.SchemaXml)) //getting the data from SchemaXml  
  37.                 {  
  38.                     xDocument = System.Xml.Linq.XDocument.Load(reader); //converting StringReader to XDocument  
  39.                     List < ListpropertiesInfo > listSchemaXml = (from l in xDocument.Descendants("List") select new ListpropertiesInfo // class to store the properties  
  40.                         {  
  41.                             PropMajorVersionLimit = l.Attribute("MajorVersionLimit").Value,  
  42.                                 PropMajorWithMinorVerionLimit = l.Attribute("MajorWithMinorVersionsLimit").Value,  
  43.                                 PropExcludFromOfflineClient = l.Attribute("ExcludeFromOfflineClient").Value,  
  44.                                 PropEnableFolderCreation = l.Attribute("EnableFolderCreation").Value  
  45.                         }).ToList();  
  46.                     foreach(var item in listSchemaXml) {  
  47.                         int majorVersion;  
  48.                         int minorVersion;  
  49.                         if (list.EnableVersioning) {  
  50.                             if (Int32.TryParse(item.PropMajorVersionLimit, out majorVersion)) {  
  51.                                 Console.WriteLine("Major Version: " + majorVersion);  
  52.                             }  
  53.                             if (Int32.TryParse(item.PropMajorWithMinorVerionLimit, out minorVersion)) {  
  54.                                 Console.WriteLine("Minor Version : " + minorVersion);  
  55.                             }  
  56.                         } else {  
  57.                             Console.WriteLine("Major Version: 0");  
  58.                             Console.WriteLine("Minor Version : 0");  
  59.                         }  
  60.                         if (Convert.ToBoolean(item.PropExcludFromOfflineClient)) Console.WriteLine("Status: false");  
  61.                         else Console.WriteLine("Status: true");  
  62.                         Console.WriteLine("Folder Creation: " + item.PropEnableFolderCreation);  
  63.                         Console.ReadLine();  
  64.                     }  
  65.                 }  
  66.             } catch (Exception er) {}  
  67.         }#  
  68.         region ListpropertiesInfo  
  69.         public class ListpropertiesInfo {  
  70.             public string PropMajorVersionLimit {  
  71.                 get;  
  72.                 set;  
  73.             }  
  74.             public string PropMajorWithMinorVerionLimit {  
  75.                 get;  
  76.                 set;  
  77.             }  
  78.             public string PropExcludFromOfflineClient {  
  79.                 get;  
  80.                 set;  
  81.             }  
  82.             public string PropEnableFolderCreation {  
  83.                 get;  
  84.                 set;  
  85.             }  
  86.         }#  
  87.         endregion  
  88.     }  
  89. }   
In this blog, I have explained how you can get the properties of a SharePoint list from SchemaXml. I hope this information will help you out in a few scenarios.