Setting the Visibility Properties of SPField Objects
Each SPField object contains properties that determine if it should appear in the default new, edit, and display forms.
There are various methods to configure these properties, some of which are outlined below.
1. Custom Server-side Solution (C# or VB.net) developed by a programmer
Some organizations create a simple custom SharePoint solution using Visual Studio, providing an interface accessible from the List Settings menu to toggle the ShowInNewForm
, ShowInEditForm
, and ShowInDisplayForm
properties on fields within any list.
This method empowers end users to control field visibility without coding or special tools once set up.
However, it demands significant upfront investment from your organization's SharePoint developers, if available.
2. PowerShell execution by a server administrator
Your SharePoint administrators have the option to manually set these properties using PowerShell from any web front end server in your SharePoint farm for one-time fixes.
$web = get-spweb "http://your/web/url"
$list = $web.lists["Your List Title"]
$field = $list.fields["Your Field Title"]
$field.ShowInNewForm = $false
$field.ShowInEditForm = $true
$field.ShowInDisplayForm = $true
$field.Update()
$web.Dispose()
4. JavaScript execution by a site owner
You also possess the ability to modify these properties using JavaScript, executable from the console tab in the F12 developer tools.
By updating the SchemaXml
property of the SPField object, you can define these properties after retrieving and adding them to the XML as shown below.
SP.SOD.executeFunc('sp.js','SP.ClientContext');
var listName = "Your List Title";
var fieldName = "Your Field Name";
var clientContext = new SP.ClientContext();
var field = clientContext.get_web().get_lists().getByTitle(listName).get_fields().getByInternalNameOrTitle(fieldName);
clientContext.load(field);
clientContext.executeQueryAsync(
function(){
var schemaXml = field.get_schemaXml();
schemaXml = schemaXml.substring(0,schemaXml.length-2)
+ 'ShowInNewForm="FALSE" ShowInEditForm="FALSE" ShowInDisplayForm="TRUE" />';
field.set_schemaXml(schemaXml);
field.update();
clientContext.executeQueryAsync();
},
function(sender,args){
alert(args.get_message());
}
);
These properties are absent in the schema XML by default and need to be specified explicitly. Checking the current XML string before modification is essential for accurate adjustments.
Alternate Method
Modifying Forms with JavaScript/HTML/CSS/jQuery
If you open the form in a separate window and not a modal dialog box, you can manipulate the page contents by inserting web parts directly onto it.
Including a content editor web part beneath the existing list form web part grants the freedom to incorporate HTML, JavaScript, and CSS for further customization.
To safeguard against SharePoint altering JavaScript and correcting HTML/CSS, embed your code snippet in a text file, upload it to a document library, then link the content editor web part to the uploaded file's URL.