Posted by: Nishant Rana | January 30, 2009

Read only field in SharePoint EditForm.aspx

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…


Responses

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

  2. 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.

  3. Amazing !!!

    Literally works

  4. Nice post.
    Thanks
    For me its helps lot

    regards
    Jenkins

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

  6. 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

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

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


Leave a response

Your response:

Categories