Pages

Monday, August 8, 2011

Read only field in SharePoint EditForm.aspx via Javascript


When we need to make some fields as read only when users wants to edit the item.This thing we can do very easily by SharePoint Designer , open that edit form and create one custom form and edit the field in which we want to make as read only we can change displaymode as edit. But what if do not want to change by SharePoint Designer.
We have very easy way which named as “JavaScript”.
I need to make one field as read-only, here is sample code
Open the editform.aspx page for a list and add one “Content Editor Webpart”, add the below javascript code.
<script type="text/javascript">

function MakeReadOnly()
{
// find all the elements with tag Name as INPUT
var elements=document.body.getElementsByTagName("INPUT");
// loop through all the elements till we find an element with type text and title as name of our field
for (index=0; index < elements.length;++index)
{
if(elements[index].type=="text")
{
if(elements[index].title=="Address") //Field name
{
elements[index].readOnly=true;
}
}
}
}
_spBodyOnLoadFunctionNames.push("MakeReadOnly()");</script>

Hope this helps!!!
Disha Shah

7 comments:

  1. HI Disha,
    I have tried above your JS script for sharepoint webpart page. Its working great for text type custom fields. But tt doesn't work for system text fields.
    Also, when field type changed to Number, Multitext, Date, Cost. It doesn't work for that.

    Your help will be really appreciated. Thanks.

    ReplyDelete
    Replies
    1. Hi Badal

      Thank you for appreciation! You can modify your javascript like below.

      //this gets the field based on title identifier and tagname - a new function
      function getTagFromIdentifierAndTitle(tagName, identifier, title) {
      var len = identifier.length;
      var tags = document.getElementsByTagName(tagName);
      for (var i=0; i < tags.length; i++) {
      var tempString = tags[i].id;
      if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {
      return tags[i];
      }
      }
      return null;
      }


      Then write this line...
      var control = getTagFromIdentifierAndTitle("input","TextField","FieldDisplayName");
      control.readOnly=true;

      Below table give you the idea what you have to mention for SharePoint Field Type

      Single Line of Text- TextField- input
      Multiple Lines of Text - TextField - input
      Number - TextField - input
      Currency - TextField - input
      Choice (dropdown) -DropDownChoice - select
      Lookup (single)* - Lookup - select
      Lookup (multiple) - SelectCandidate; SelectResult - select
      Yes/No - BooleanField - input

      Hope this helps!
      Thank You
      Disha Shah

      Delete
    2. Thanks Disha for the prompt reponse. I have tried above function with the passing the multitext field Display and identifier in the function but unfortunately it is not working for me. Am I Missing something here? Please see below the details of function with passing parameter values:



      //this gets the field based on title identifier and tagname - a new function
      function getTagFromIdentifierAndTitle(tagName, identifier, title) {
      var len = identifier.length;
      var tags = document.getElementsByTagName(tagName);
      for (var i=0; i < tags.length; i++) {
      var tempString = tags[i].id;
      if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {
      return tags[i];
      }
      }
      return null;
      }
      var control = getTagFromIdentifierAndTitle("input","TextField","MultifieldColumnDisplayName");
      control.readOnly=true;

      Delete
    3. Hi Badal

      Here how your javscript code should look like inside script tag

      //this gets the field based on title identifier and tagname - a new function
      function getTagFromIdentifierAndTitle(tagName, identifier, title) {
      var len = identifier.length;
      var tags = document.getElementsByTagName(tagName);
      for (var i=0; i < tags.length; i++) {
      var tempString = tags[i].id;
      if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {
      return tags[i];
      }
      }
      return null;
      }


      function MakeReadOnly()
      {
      var control = getTagFromIdentifierAndTitle("input","TextField","MultifieldColumnDisplayName");
      control.readOnly=true;


      }

      _spBodyOnLoadFunctionNames.push("MakeReadOnly()");


      Thank You
      Disha Shah

      Delete
  2. There are many ways to make Read only fields in SharePoint:

    1. Using PowerShell to set SharePoint column read only.

    2. Using jQuery/Javascript to make read only fields

    3. Using SharePoint Designer to make SharePoint list field read only.

    How to Make SharePoint List Column (Form Field) Read Only

    ReplyDelete
  3. what about readonly property in Newform

    ReplyDelete
  4. Hi,

    I have a Sharepoint List View with a couple of Lookup columns. I want to disable the hyperlinks in the view and make them read only. How can I achieve it?

    Thanks

    ReplyDelete