Read only field in SharePoint EditForm.aspx

Posted: January 30, 2009 in SharePoint
Tags:

Using JavaScript

First find out the tag corresponding to the input field which would like to set as read only

For this open up the editform.aspx page right click it and select view source

Say this is the tag of the input field

<input name=”TextField” type=”text” value=”Approved” maxlength=”255″ id=”4_ctl00_ctl00_TextField” title=”Status of Idea” class=”ms-long” />

Now open your editform.aspx page in SharePoint designer and add the following script to it

<script type=”text/javascript”>

function SetReadOnly()

{

// 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==“Status of Idea”)

{

elements[index].readOnly=true;

}

}

}

}

_spBodyOnLoadFunctionNames.push(“SetReadOnly()”);

</script>

Or

<script type=”text/javascript”>

function SetReadOnly()

{

var elements=document.getElementById(’4_ctl00_ctl00_TextField’);

elements.readOnly=true;

}

_spBodyOnLoadFunctionNames.push(“SetReadOnly()”);

</script>

Or using event handler as mentioned over here

http://blogs.msdn.com/sowmyancs/archive/2008/03/25/creating-a-read-only-field-with-default-value-in-a-sharepoint-list.aspx

Or

Using CAML

http://forums.asp.net/t/1166224.aspx

And to hide button say OK button than

function SetHidden()
{
alert(‘Hi’);
var x=document.getElementsByTagName(“input”);
for (var i=0;i<x.length;i++)
{
if (x.item(i).type==”button”&&x.item(i).value==”OK”)
{
x.item(i).style.display = “none”
};
}
}
_spBodyOnLoadFunctionNames.push(“SetHidden()”);


Bye…


Comments
  1. Anonymous says:

    Nice Article. Its really helpful.

  2. SharePointNew says:

    It’s very helpful post. Thank you.
    May I have a question…?
    If I need to re-write / modify the read-only field in the list of SharePoint, how should I do?
    For now, I use MS Access to do data query and want to sync back to SharePoint.
    Some of read-only fields in the list will occur error message to tell me it can’t be modified.
    For example: Content Type, File Type, Modified, Created, Modified By, Created By…etc.
    Thank you

  3. Doc says:

    I have tried every variant of the javascript examples above that I can imagine and cannot get this to work. I even tried modyifing a similar “hide field” function(see below_ that is working but to no success.

    _spBodyOnLoadFunctionNames.push(“hideFields”);

    function findacontrol(FieldName) {
    var arr = document.getElementsByTagName(“!”);
    // get all comments
    for (var i=0;i 0)
    { return arr[i]; }
    }
    }
    function hideFields() {
    var control = findacontrol(“Owner Email Sent”);
    control.parentNode.parentNode.style.display=”none”;
    }

    I’m desperate to make some fields on my editform.aspx read only using javascript – any help\recommendations would be greatly appreciated!!!

  4. [...] to Nishant Rana as I took it from his blog. Article here. Filed under Uncategorized Click here to cancel [...]

  5. naveen says:

    can we make a drop down list using the above script

  6. Luis Rodrigo says:

    Dont work with person picker

  7. Carolyn says:

    Wow this is exactly what I needed to see. Now I can access any field on the screen inside Sharepoint designer aspx.

  8. Tyres says:

    I was just thinking about Nishant Rana’s Weblog and you’ve really helped out. Thanks!

  9. Tyre says:

    I didn’t know that Nishant Rana’s Weblog.

  10. Great post, some of my customers have used this scripts to add collaboration content to LOB data in SharePoint lists with the help of the SharePoint Business Data List Connector.

    Thanks again, Regards.
    Frank

  11. [...] Read only field in SharePoint EditForm.aspx Posted by Kit Filed in JavaScript, SharePoint Tags: Example, JavaScript, Prototype, SharePoint [...]

  12. Jenkins says:

    Nice post.
    Thanks
    For me its helps lot

    regards
    Jenkins

  13. Ali Raza says:

    Amazing !!!

    Literally works

  14. larry says:

    this was a great post. I have found it difficult to find a script that does not lose its value on post back, so now I am curious. How can this be applied to other field types like Select (lookup and dropdown). i have tried using the by ID, but not luck. Also tried to add multiple fields, but got an error.

  15. [...] Read only field in SharePoint EditForm.aspx [...]

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s