Set List Properties By WebRequest

There are many ways in CSOM to set the list properties of a SharePoint site but there are a few properties which we can’t set directly from list object. In this blog, I will explain how to set those properties using WebRequest, for example: offline client availability, major version limit, and major with minor version limit.

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

The code block for this is as 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. using System.IO;  
  11. namespace EditListProperties {  
  12.     class EditListPropertiesByWebRequest {  
  13.         public static string HEADER_CONTENT_APP = "application/x-vermeer-urlencoded";  
  14.         public static string USER_AGENT_APP = "FrontPage";  
  15.         public static string MESSAGE_SUCCESS_APP = "message=successfully";  
  16.         public static string MESSAGE_CONFLICT_APP = "msg=Save Conflict";  
  17.         public static string ENABLE_OFFLINE_CLIENT_AVAILABILITY_APP = "&ExcludeFromOfflineClient=False";  
  18.         public static string DISABLE_OFFLINE_CLIENT_AVAILABILITY_APP = "&ExcludeFromOfflineClient=True";  
  19.         public static string MAJOR_VERSION_LIMIT_APP = "&MajorVersionLimit=";  
  20.         public static string MAJOR_VERSION_WITH_MINOR_APP = "&MajorWithMinorVersionsLimit=";  
  21.         static void Main(string[] args) {  
  22.             string listName = "CopyDoc";  
  23.             string username = string.Empty;  
  24.             string password = string.Empty;  
  25.             NetworkCredential credentials = null;  
  26.             List list = null;  
  27.             string query = string.Empty;  
  28.             try {  
  29.                 username = "userName";  
  30.                 password = "******";  
  31.                 ClientContext ctx = new ClientContext("siteUrl"); //creating ClientContext from given SharePoint Site  
  32.                 credentials = new NetworkCredential(username, password); //creating creadential object for SharePoint site.  
  33.                 ctx.Credentials = credentials;  
  34.                 Web web = ctx.Web;  
  35.                 list = ctx.Web.Lists.GetByTitle(listName);  
  36.                 ctx.Load(list, l => l.EnableVersioning);  
  37.                 ctx.ExecuteQuery();  
  38.             } catch (Exception ex) {}  
  39.             try {  
  40.                 //if we want to "SET OFFLINE CLIENT AVAILABILITY"  
  41.                 string value = string.Empty;  
  42.                 value = "Enable"//We can change it to Enable or Disable accoring to requirement  
  43.                 if (value == "Enable"// if we want it to be enabled  
  44.                 {  
  45.                     query += ENABLE_OFFLINE_CLIENT_AVAILABILITY_APP;  
  46.                 } else if (value == "Disable"// if we want it to be disabled  
  47.                 {  
  48.                     query += DISABLE_OFFLINE_CLIENT_AVAILABILITY_APP;  
  49.                 }  
  50.                 //if we want to "SET MAJOR VERSION LIMIT"  
  51.                 value = string.Empty;  
  52.                 value = "10"//We can change it to any intiger accoring to requirement  
  53.                 list.EnableVersioning = true;  
  54.                 list.Update();  
  55.                 query += MAJOR_VERSION_LIMIT_APP + value;  
  56.                 //if we want to "SET MAJOR WITH MINOR VERSIONS LIMIT"  
  57.                 value = string.Empty;  
  58.                 value = "5"//We can change it to any intiger accoring to requirement  
  59.                 list.EnableVersioning = true;  
  60.                 list.Update();  
  61.                 query += MAJOR_VERSION_WITH_MINOR_APP + value;  
  62.                 GetHttpWebRequest(query, credentials);  
  63.             } catch (Exception er) {}  
  64.         }  
  65.         #region GetWebRequest  
  66.         /// <summary>  
  67.         ///  
  68.         /// </summary>  
  69.         /// <param name="query"></param>  
  70.         /// <param name="credential"></param>  
  71.         private static void GetHttpWebRequest(string query, NetworkCredential credential) {  
  72.             try {  
  73.                 HttpWebRequest request = (HttpWebRequest) WebRequest.Create(query);  
  74.                 request.Method = "POST";  
  75.                 request.Headers["Content"] = HEADER_CONTENT_APP;  
  76.                 request.Headers["X-Vermeer-Content-Type"] = HEADER_CONTENT_APP;  
  77.                 request.UserAgent = USER_AGENT_APP;  
  78.                 request.UseDefaultCredentials = false;  
  79.                 request.KeepAlive = true;  
  80.                 request.Credentials = credential;  
  81.                 using(System.IO.Stream requestStream = request.GetRequestStream()) {  
  82.                     GetWebResponse(request);  
  83.                 }  
  84.             } catch (Exception er) {}  
  85.         }  
  86.         #endregion  
  87.         #region GetResponse  
  88.         /// <summary>  
  89.         ///  
  90.         /// </summary>  
  91.         /// <param name="webRequest"></param>  
  92.         /// <returns></returns>  
  93.         private static string GetWebResponse(HttpWebRequest webRequest) {  
  94.             string responseString = string.Empty;  
  95.             using(WebResponse webResponse = webRequest.GetResponse()) {  
  96.                 using(StreamReader reader = new StreamReader(webResponse.GetResponseStream())) {  
  97.                     responseString = reader.ReadToEnd();  
  98.                     byte[] fileBuffer = Encoding.UTF8.GetBytes(responseString);  
  99.                 }  
  100.             }  
  101.             if ((responseString.IndexOf(MESSAGE_SUCCESS_APP) < 0) && (responseString.IndexOf(MESSAGE_CONFLICT_APP) < 0)) {  
  102.                 throw new Exception(responseString);  
  103.             }  
  104.             return responseString;  
  105.         }#endregion  
  106.     }  
  107. }  

In this blog, I have explained how you can set the properties of a SharePoint list using WebRequest. I hope this information will help you out in a few scenarios.