Set Site Content Type Column Required in SharePoint using CSOM

The example in this topic show how to use CSOM to Set Site Content Type Column Required in SharePoint

  • using Microsoft.SharePoint.Client;
  • using System.Linq;
  •  
  • using (ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection"))
  • {
  • // Get all the content types from current site
  • ContentTypeCollection oContentTypeCollection = clientcontext.Web.ContentTypes;
  •  
  • // Load content type collection
  • clientcontext.Load(oContentTypeCollection);
  • clientcontext.ExecuteQuery();
  •  
  • // Give content type name over here
  • ContentType targetContentType = (from contentType in oContentTypeCollection where contentType.Name == "Content Type Name" select contentType).FirstOrDefault();
  •  
  • // Load target content type and content type fields
  • clientcontext.Load(targetContentType);
  • clientcontext.Load(targetContentType.Fields);
  • clientcontext.ExecuteQuery();
  •  
  • // Get required field from the target content type
  • // Fiver field internal name or display name over here
  • Field targetField = targetContentType.Fields.GetByInternalNameOrTitle("Column Name");
  •  
  • clientcontext.Load(targetField);
  • clientcontext.ExecuteQuery();
  •  
  • targetContentType.FieldLinks.GetById(targetField.Id).Required = true;
  • targetContentType.Update(false);
  • clientcontext.ExecuteQuery();
  • }

Similar examples using different APIs:

Thank you for reading this article. This code was tested in SharePoint 2013

We value your feedback:

Page URL:

Name:

 

Email:

 
 

Feedback:

 

© 2019 Code SharePoint