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 custom list that I am using as a Frequently asked questions list for a number of subsites. I have 3 columns, Question, Answer and category. I want to make the question field readonly as well as the category field because these fields will be pre-populated.

Using the inline edit view of the ListViewWbPart, how can I set these fields to "readonly"? I tried setting the ControlMode to "display", but this affects all of my columns for some reason.

Also, how can I set the width of the textbox for each column in edit mode? I tried using style="width:250px;" in the SharePoint:FormField, but again, it affected the textboxes for all of the columns instead of only one.

Am I missing something?

JM

share|improve this question
add comment

3 Answers

You could create 2 calculated columns and set their values to be the columns you wish to be readonly. Then show these calculated columns in your view instead of Question and Category.

e.g. Calculated column: DisplayQuestion, Formula: =[Question]

As far as the width of the input fields, you could create a custom new and edit form for your list. Then you can modify specific fields on your form. However, if you do that and subsequently add new columns to your list they will not show up on your custom form. Instead, you could use jQuery on your page to find the textbox and set its width:

<script src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
<script type="text/javascript">
  $(function() {
    $("input[Title='Question']").css("width", "250px");
  });
</script>
share|improve this answer
    
Thanks for the suggestions Laurie. Where in the code should I place the script? It doesn't seem to work for me when I add it in a WikiContentWebPart. As far as the calculated column, this works, but is there a way to rename the display name of the column so that it says "Question" rather than "Display Question" ? –  JaMBox Jun 20 '11 at 16:02
    
To add the script you can insert a Content Editor web part to the page and click Format Text > HTML > Edit HTML Source. To change the column header, you need to open the view in SharePoint Designer and edit the page. Or you could rename your existing columns to something else and save Question and Category for the calculated column names. –  Laurie Jun 20 '11 at 17:12
add comment

Have you tried setting ReadOnlyField property of SPField object to true?

This can be done with a simple PowerShell script or using SharePoint Console Application project.

PowerShell script sample:

$w = Get-SPWeb http://localhost
$l = $w.GetList("/Lists/QA");
$f = $l.Fields["Category"];
$f.ReadOnlyField = $true
$f.Update()
$f = $l.Fields["Question"];
$f.ReadOnlyField = $true
$f.Update()

And when you will pre-populate these fields, you can turn ReadOnlyField flag off programmatically, set the values, and then turn it on back again.

share|improve this answer
add comment
up vote 0 down vote accepted

These are good suggestions, however, I managed to get the results I was looking for by using a DataViewWebPart in the default view rather than a ListViewWebpart. Apparently a DataViewWebpart is a lot more flexible when it comes to formatting sharepoint:formFields. For anyone else struggling with a ListViewWebPart, I recommend switching to a Dataview WebPart instead.

Here's how I did it: Open your site in Sharepoint designer 2010, Click Lists and Libraries, double click the list that you want to add a view for. Then in the "Views" box to the right, click new, name your new view and click ok. Now open the newly created view. you will need to hide the ListViewWebPart that is currently on the form (not sure why you wouldn't just delete it, but that's what I've heard). To hide the webpart, find the "isvisible" attribute (ctrl F) and set it to false. Now in the WYSIWYG split view, click below the ListViewWebPart. Now in the ribbon click the insert tab, click DataView and choose empty DataView. Now back to the WYSIWYG editor. Click the link to choose a datasource, and click the list that you want to attach to. Now drag in the columns that you would like to include in your view.

If you need inline editing as I did, click the options tab in the ribbon, click inline editing and choose the links that you would like to appear in your view (insert, edit or delete).

Now you should be able to make changes like I did for example, to set a column to read-only when in the edit mode, click on the DataviewWebPart and then click the design tab in the ribbon. On the far right under "Data View Preview, choose "Edit Template". now in the WYSiWYG editor, double click the field that needs to be read-only. This should take you right to the code that needs to be modified. In the Sharepoint:formfield tag, change the ControlMode attribute from "Edit" to "Display". now your field is read only.

If you want to change the width of the textbox in edit mode, follow the same proceedure to get to the field and then play with the DisplaySize attribute or if you know CSS, you could add a cssclass to the field and style it with an external stylesheet (search the web for info on the sharepoint:CssRegistration control).

share|improve this answer
add comment

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.