Create Number Type Site Column using CSOM

Create Number Type Site Column using CSOM in SharePoint Programmatically

In this post i describe you to Create Number Type Site Column using CSOM as standard number field where the user can set positive and negative numbers, and where the number of decimals is generated automatically.You need to Create Boolean type Site column. follow my post Create Boolean Type Site Column using CSOM.

[AdSense-A]

standard number field

 try
            {
                ClientContext clientContext = new ClientContext(SiteUrls);
                Web rootWeb = clientContext.Site.RootWeb;
                string schemaNumberColumn = "<Field Type='Number' Name='NbrOfEmployees' StaticName='NbrOfEmployees' DisplayName='Number of employees' />";
                Field multilineTextField = rootWeb.Fields.AddFieldAsXml(schemaNumberColumn, true, AddFieldOptions.AddFieldInternalNameHint);
                clientContext.ExecuteQuery();
            }
            catch (Exception ex)
            {
            }

If you want to declare a number field that only allows positive numbers, you will have to declare it this way

try
            {
                ClientContext clientContext = new ClientContext(SiteUrls);
                Web rootWeb = clientContext.Site.RootWeb;
                string schemaNumberField = "<Field ID='<GUID>' Type='Number' Name='NbrOfEmployees' StaticName='NbrOfEmployees' DisplayName='Number of employees' Min='0' />";
                Field multilineTextField = rootWeb.Fields.AddFieldAsXml(schemaNumberColumn, true, AddFieldOptions.AddFieldInternalNameHint);
                clientContext.ExecuteQuery();
            }
            catch (Exception ex)
            {
            }

if you want to restrict the range of values that can be entered by the user, you have to declare the number field the following way

try
            {
                ClientContext clientContext = new ClientContext(SiteUrls);
                Web rootWeb = clientContext.Site.RootWeb;
                string schemaNumberField ="<Field ID='<GUID>' Type='Number' Name='NbrOfEmployees' StaticName='NbrOfEmployees' DisplayName='Number of employees' Min='0' Max='5000'/>";
                Field multilineTextField = rootWeb.Fields.AddFieldAsXml(schemaNumberColumn, true, AddFieldOptions.AddFieldInternalNameHint);
                clientContext.ExecuteQuery();
            }
            catch (Exception ex)
            {
            }

Also you can define the number of decimals as

try
            {
                ClientContext clientContext = new ClientContext(SiteUrls);
                Web rootWeb = clientContext.Site.RootWeb;
                string schemaNumberField ="<Field ID='<GUID>' Type='Number' Name='NbrOfEmployees' StaticName='NbrOfEmployees' DisplayName='Number of employees' Min='0' Decimals='0' />";
                Field multilineTextField = rootWeb.Fields.AddFieldAsXml(schemaNumberColumn, true, AddFieldOptions.AddFieldInternalNameHint);
                clientContext.ExecuteQuery();
            }
            catch (Exception ex)
            {
            }

create a percentage field by using the Number type. In that case you have to add an addition attribute Percentage, which you have to set to true

try
            {
                ClientContext clientContext = new ClientContext(SiteUrls);
                Web rootWeb = clientContext.Site.RootWeb;
                string schemaNumberField = "<Field ID='<GUID>' Type='Number' Name='NbrOfEmployees' StaticName='NbrOfEmployees' DisplayName='Number of employees' Percentage='True' Decimals='2' />";
                Field multilineTextField = rootWeb.Fields.AddFieldAsXml(schemaNumberColumn, true, AddFieldOptions.AddFieldInternalNameHint);
                clientContext.ExecuteQuery();
            }
            catch (Exception ex)
            {
            }

 


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!