Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a field that I want to make not editable for the users. It's a field of type Text.

I have read about doing it with javascript and content editor webbpart, but I want to do it programmatically because the field is part of my Feature.

I have tried this code, but it hides the field in forms view:

SPList list = web.lists["caseslist"]; 
SPField field = list.Fields.GetField("casenumbers"); 
field.ReadOnlyField = true; 
field.ShowInDisplayForm = true; 
field.ShowInEditForm = true; 
field.ShowInNewForm = true; 
field.Update(); 

What am I doing wrong, why does this code hide the field in the forms?

I am not sure but is it possible to make a field "non editable" programmatically?

share|improve this question
    
Are you using SP2010 or 2013? Also are the new/edit forms are OOB list forms? –  Asad Refai Jun 18 at 8:28
    
I'm using SP2013. the lists are OOB. –  avrahamcool Jun 18 at 8:37
    
Do you plan to deploy site columns through feature? or by any other ways like powershell or c# server code? –  Asad Refai Jun 18 at 8:38
1  
yes, the site columns are created Programmatically, and deployed to the site with a feature. the column is created as a regular column, and I want to make it readonly when some events occur (via EventReceiver) –  avrahamcool Jun 18 at 8:40

4 Answers 4

In SharePoint 2013, client-side rendering (CSR) provides a way to control the output/design of your fields and set of controls by leveraging HTML and JavaScript technologies for example how should the fields render in list views (edit, new or quick edit), search, list forms etc. Now, every field definition comes with JSLink property that can be overridden, by providing your own JS file, which means completely control the rendering of that field i.e. hidden, disabled, etc.

I have provided a solution to similar question, but it can easily be modified to suit your needs. Of course, the whole solution can be packaged and deployed using feature without any hassles of adding content editor web part =)

Happy SharePointing!

share|improve this answer

The simplest way to make any field read only would be to use SPutility Jquery You can use it like below:

SPUtility.GetSPField('Column Name').MakeReadOnly();

For the comment made by David on Dmitry Kozlov answer, to make People picker readonly, you can use the below JQuery code:

$(".sp-peoplepicker-delImage").css({ 'display' : 'none'});
$(".sp-peoplepicker-editorInput").css({ 'display' : 'none'});

You can also apply them with the help of css:

<style>

.sp-peoplepicker-delImage{

display:none;

}
.sp-peoplepicker-editorInput{

display:none;

}

</style>

This is the easiest and fastest way to make people picker fields read only in SharePoint 2013/online, but it will make every people picker field on the form read only. So please let me know if you want for a specific column.

share|improve this answer
    
The question is about a field of type Text, not a people picker –  Robert Lindgren Jun 18 at 9:10
    
Please see 3rd comment on Dmitry Kozlov answer, David asked to make people picker field read only. I just gave an answer to that. Maybe I am wrong. –  Taran Goel Jun 18 at 9:14
    
I'm just refering to the body of the question, which is what should be answered ;) –  Robert Lindgren Jun 18 at 9:15
    
Just updated my answer! Thanks :) –  Taran Goel Jun 18 at 9:17
    
this doesn't help me. a client side solution can be overridden by the client. I need to mark that field as readonly from the server side code. [read the bounty description again] –  avrahamcool Jun 18 at 9:36

If you make field readonly it will not be displayed on edit or new forms. You can do it with custom form only. There are not needs to use JS-code, you can do it with xslt but in custom form only. But you can deploy this form with your feature.

share|improve this answer
    
Thans for the reply. i solved it with using javascript, it works well. I added a content editor webpart in the different forms and added javascript to that cewp. But have do i hide fields of type Person or Group with javascript?? it doesnt work with my code. And something more that i cant find information about, is it possible to add my javascript code in a feature and load it on the forms when the feature is activated?? –  David Oct 7 '12 at 18:44
    
Yes, you can hide any field with javascript, person or group as well. Just hide table rows with such fields. Also I can recommend you to use Forms Designer tool: spform.com. It allows you to make fields readonly on edit and new forms. As about deploying your form, yes, you can add your form into the feature. You can define template for your list with links to your custom form and deploy custom form page as module. –  Dmitry Kozlov Oct 7 '12 at 19:26
    
i cant figure out how to hide a person or group field with javascrit. because i have the same js code that i use for text field, multipletext field, chooice field. all works fine but not for a Person or Group field. But i dont want to make a custom form, i want to use the built in sharepoint form. in that case is it possible to add the javascript to the feature and activate it that way? –  David Oct 7 '12 at 19:58
    
Could you publish js-code you use to hide other fields? You already created custom form since you added CEWP web part. You have to deploy page with feature, not only javascript-code. –  Dmitry Kozlov Oct 8 '12 at 5:51

As forms are part of lists, PushChangesToList property should be set to true. Can you please try implementing the below line of code and let me know the result

SPList list = web.lists["caseslist"]; 
SPField field = list.Fields.GetField("casenumbers"); 
field.PushChangesToLists = true;
field.ReadOnlyField = true; 
field.ShowInDisplayForm = true; 
field.ShowInEditForm = true; 
field.ShowInNewForm = true; 
field.Update();

If PowerShell is an option, you can try the solution provided here.

share|improve this answer
    
I tried with field.Update(true); witch is equivalent (right?). and the field indeed became readonly, but it disappeared from the display form as well. –  avrahamcool Jun 18 at 9:52
    
field.update will work for any new list where this column would be used, but to pass on this to old lists, you'll require to set PushChangesToLists propert to True. –  Taran Goel Jun 18 at 9:55
    
you didn't pay attention to the true i pass to the function. more details here –  avrahamcool Jun 18 at 9:58
    
You are correct, My bad..sorry :) –  Taran Goel Jun 18 at 10:01
    
Did the powershell solution worked? –  Taran Goel Jun 22 at 5:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.