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.
- 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 = "[email protected]";
- password = "******";
- foreach(char ch in password.ToCharArray())
- secureString.AppendChar(ch);
- ClientContext ctx = new ClientContext("siteUrl");
- credentials = new SharePointOnlineCredentials(username, secureString);
- 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))
- {
- xDocument = System.Xml.Linq.XDocument.Load(reader);
- List < ListpropertiesInfo > listSchemaXml = (from l in xDocument.Descendants("List") select new ListpropertiesInfo
- {
- 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: false");
- else Console.WriteLine("Status: true");
- Console.WriteLine("Folder Creation: " + item.PropEnableFolderCreation);
- Console.ReadLine();
- }
- }
- } catch (Exception er) {}
- }#
- region ListpropertiesInfo
- public class ListpropertiesInfo {
- public string PropMajorVersionLimit {
- get;
- set;
- }
- public string PropMajorWithMinorVerionLimit {
- get;
- set;
- }
- public string PropExcludFromOfflineClient {
- get;
- set;
- }
- public string PropEnableFolderCreation {
- get;
- set;
- }
- }#
- endregion
- }
- }
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.