Getting List Properties from SchemaXml

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

In this blog, I will explain how to find few list properties using SchemaXml l. There are many ways in CSOM to get the properties from SharePoint list but few properties are there which we can’t get directly from list object. For example, “MajorVersionLimit” is not present directly.

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

The code block for this is as mentioned below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using System.Net;
using System.Security;
using Microsoft.SharePoint.Client.Utilities;

namespace SchemaXml
{
    class GetListSchema
    {
        static void Main(string[] args)
        {
            string listName = "CopyDoc";
            string username = string.Empty;
            string password = string.Empty;
            SecureString secureString = new SecureString();
            SharePointOnlineCredentials credentials = null;
            List list = null;
            System.Xml.Linq.XDocument xDocument;
            string modified = string.Empty;

            try
            {
                username = "userName@domain.com";
                password = "******";

                foreach (char ch in password.ToCharArray())
                    secureString.AppendChar(ch);

                ClientContext ctx = new ClientContext("siteUrl"); //creating ClientContext from given SharePoint Site
                credentials = new SharePointOnlineCredentials(username, secureString); //creating creadential object for office365 site.

                ctx.Credentials = credentials;
                Web web = ctx.Web;
                list = ctx.Web.Lists.GetByTitle(listName);
                ctx.Load(list, l => l.SchemaXml,
                               l => l.EnableVersioning);
                ctx.ExecuteQuery();
            }
            catch (Exception ex) { }

            try
            {
                using (System.IO.StringReader reader = new System.IO.StringReader(list.SchemaXml))//getting the data from SchemaXml
                {
                    xDocument = System.Xml.Linq.XDocument.Load(reader); //converting StringReader to XDocument

                    List<ListpropertiesInfo> listSchemaXml =  
                        (
                            from l in xDocument.Descendants("List")
                            select new ListpropertiesInfo // class to store the properties
                            {
                                PropMajorVersionLimit = l.Attribute("MajorVersionLimit").Value,
                                PropMajorWithMinorVerionLimit = l.Attribute("MajorWithMinorVersionsLimit").Value,
                                PropExcludFromOfflineClient = l.Attribute("ExcludeFromOfflineClient").Value,
                                PropEnableFolderCreation = l.Attribute("EnableFolderCreation").Value
                            }
                        ).ToList();  

                    foreach (var item in listSchemaXml)
                    {
                        int majorVersion;
                        int minorVersion;

                        if (list.EnableVersioning)
                        {
                            if (Int32.TryParse(item.PropMajorVersionLimit, out majorVersion))
                            {
                                Console.WriteLine("Major Version: " + majorVersion);
                            }
                            if (Int32.TryParse(item.PropMajorWithMinorVerionLimit, out minorVersion))
                            {
                                Console.WriteLine("Minor Version : " + minorVersion);
                            }
                        }
                        else
                        {
                            Console.WriteLine("Major Version: 0");
                            Console.WriteLine("Minor Version : 0");
                        }

                        if (Convert.ToBoolean(item.PropExcludFromOfflineClient))
                            Console.WriteLine("Status: " + item.PropExcludFromOfflineClient);
                        else
                            Console.WriteLine("Status: " + item.PropExcludFromOfflineClient);

                        Console.WriteLine("Folder Creation: " + item.PropEnableFolderCreation);
                        Console.ReadLine();
                    }
                }
            }
            catch (Exception er) { }
        }

        public class ListpropertiesInfo
        {
            public string PropMajorVersionLimit { get; set; }
            public string PropMajorWithMinorVerionLimit { get; set; }
            public string PropExcludFromOfflineClient { get; set; }
            public string PropEnableFolderCreation { get; set; }
        }
    }
}

In this blog, I have explained how you can get properties of SharePoint list from SchemaXml I hope this information will help you out in a few scenarios.

This solution is brought to you by our SharePoint professionals.

Softree Consulting employs SharePoint consultants; we are a technology services provider with the aim to help companies achieve exceptional performance through SharePoint. Our dedicated team of SharePoint consultants has the right bent of mind to understand and execute customer requirements.

Be it SPFx or SharePoint add-in developments, SharePoint 2019 developments, web part developments, migrating from SharePoint 2010/2013 to SharePoint 2013/2016/Office 365, Office 365, SharePoint hosted apps development or something else in SharePoint, we strive to deliver the best.

Softree Consulting is an award-winning SharePoint consultancy that has been working with clients from across the globe to transform workplaces with tools and platforms from Microsoft technology. As a widely recognized organization, we are commonly approached to incorporate technical excellence and innovative design into all our customer actions.....

Site Footer