Share via


SharePoint 2013: Set field as read only on editform/newform during list provisioning

Requirement: Set a Text field 'Application Name' in a list as read only on edit form page load. The list is created during site provisioning.

Solution: Customize the editform of the list by adding a content editor web part and JavaScript to make the field as read only.Add the following code in list's schema.xml file

<Form Type="EditForm" Url="EditForm.aspx" Path="CustomEditForm.aspx" WebPartZoneID="Main" UseDefaultListFormWebPart="False" >
        <WebParts>
          <AllUsersWebPart WebPartZoneID="Main" WebPartOrder="1">
            <![CDATA[
              <WebPart xmlns="http://schemas.microsoft.com/WebPart/v2">
              <Assembly>Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>                       
              <TypeName>Microsoft.SharePoint.WebPartPages.ListFormWebPart</TypeName>
              <PageType>PAGE_EDITFORM</PageType>
            </WebPart>]]>
          </AllUsersWebPart>
          <AllUsersWebPart WebPartZoneID="Main" WebPartOrder="2">
            <![CDATA[
        <WebPart xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/WebPart/v2">
        <Title>Web part title</Title>
        <FrameType>None</FrameType>
        <Description>Web part description</Description>
        <FrameState>Normal</FrameState>
        <Assembly>Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
        <TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>
        <ContentLink xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />
        <Content xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor">
         
          <script>
         
       var prev_handler = window.onload;
            this.window.onload = function () {
               if (prev_handler) {
                 prev_handler();
               }
 
      var lists = document.getElementsByTagName('input');
        for ( var i = 0; i < lists.length; i++) {
              if (lists[i].getAttribute('title') === 'Application Name' ) {
                lists[i].readOnly = true;
               
              }
           }
         
          </script>     
           </Content>
            <PartStorage xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />
           </WebPart>
 
            ]]>
          </AllUsersWebPart>
        </WebParts>
      </Form>

PS. The script need to be updated if the field name is renamed

Other Languages

This article is also available in the following languages: