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.

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 the list object. In this blog, I will explain how to find those using the WebRequest.example:-offline client availability, major version limit, major with mino 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 :

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;
using System.IO;

namespace EditListProperties
{
    class EditListPropertiesByWebRequest
    {
        public static string HEADER_CONTENT_APP = "application/x-vermeer-urlencoded";
        public static string USER_AGENT_APP = "FrontPage";
        public static string MESSAGE_SUCCESS_APP = "message=successfully";
        public static string MESSAGE_CONFLICT_APP = "msg=Save Conflict";
        public static string ENABLE_OFFLINE_CLIENT_AVAILABILITY_APP = "&ExcludeFromOfflineClient=False";
        public static string DISABLE_OFFLINE_CLIENT_AVAILABILITY_APP = "&ExcludeFromOfflineClient=True";
        public static string MAJOR_VERSION_LIMIT_APP = "&MajorVersionLimit=";
        public static string MAJOR_VERSION_WITH_MINOR_APP = "&MajorWithMinorVersionsLimit=";
        static void Main(string[] args)
        {
            string listName = "CopyDoc";
            string username = string.Empty;
            string password = string.Empty;
            NetworkCredential credentials = null;
            List list = null;
            string query = string.Empty;

            try
            {
                username = "userName";
                password = "******";

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

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

            try
            {
                //if we want to "SET OFFLINE CLIENT AVAILABILITY"              
                string value = string.Empty;
                value = "Enable"; //We can change it to Enable or Disable accoring to requirement

                if (value == "Enable") // if we want it to be enabled
                {
                    query += ENABLE_OFFLINE_CLIENT_AVAILABILITY_APP;
                }
                else if (value == "Disable") // if we want it to be disabled
                {
                    query += DISABLE_OFFLINE_CLIENT_AVAILABILITY_APP;
                }               

                //if we want to "SET MAJOR VERSION LIMIT"              
                value = string.Empty;
                value = "10"; //We can change it to any intiger accoring to requirement
                list.EnableVersioning = true;
                list.Update();
                query += MAJOR_VERSION_LIMIT_APP + value;

                //if we want to "SET MAJOR WITH MINOR VERSIONS LIMIT"              
                value = string.Empty;
                value = "5"; //We can change it to any intiger accoring to requirement
                list.EnableVersioning = true;
                list.Update();
                query += MAJOR_VERSION_WITH_MINOR_APP + value;

                GetHttpWebRequest(query, credentials);

            }
            catch (Exception er) { }
        }

        #region GetWebRequest
        /// <summary>
        /// 
        /// </summary>
        /// <param name="query"></param>
        /// <param name="credential"></param>
        private static void GetHttpWebRequest(string query, NetworkCredential credential)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(query);
                request.Method = "POST";
                request.Headers["Content"] = HEADER_CONTENT_APP;
                request.Headers["X-Vermeer-Content-Type"] = HEADER_CONTENT_APP;
                request.UserAgent = USER_AGENT_APP;
                request.UseDefaultCredentials = false;
                request.KeepAlive = true;

                request.Credentials = credential;

                using (System.IO.Stream requestStream = request.GetRequestStream())
                {
                    GetWebResponse(request);
                }
            }
            catch (Exception er) { }
        }
        #endregion

        #region GetResponse
        /// <summary>
        /// 
        /// </summary>
        /// <param name="webRequest"></param>
        /// <returns></returns>
        private static string GetWebResponse(HttpWebRequest webRequest)
        {
            string responseString = string.Empty;
            using (WebResponse webResponse = webRequest.GetResponse())
            {
                using (StreamReader reader = new StreamReader(webResponse.GetResponseStream()))
                {
                    responseString = reader.ReadToEnd();
                    byte[] fileBuffer = Encoding.UTF8.GetBytes(responseString);
                }
            }

            if ((responseString.IndexOf(MESSAGE_SUCCESS_APP) < 0) && (responseString.IndexOf(MESSAGE_CONFLICT_APP) < 0))
            {
                throw new Exception(responseString);
            }
            return responseString;
        }
        #endregion       
    }
}

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.

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