How to create readonly columns in MOSS 2007 custom list, there some methods:
1) Create a custom list definition where you can modify the schema.xml. You will have to find the field that you want not to show in the edit form and set the ShowInEditForm="FALSE" for field and the field ref. Below is a simple sample:
<Field
...
ShowInDisplayForm = "TRUE" | "FALSE"
ShowInEditForm = "TRUE" | "FALSE"
ShowInNewForm = "TRUE" | "FALSE"
.../>
<FieldRef
...
ShowInDisplayForm="TRUE" | "FALSE"
ShowInEditForm="TRUE" | "FALSE"
ShowInNewForm="TRUE" | "FALSE"></FieldRef>
2) Use the object Model. Through code you can access the specific SPfield object and set the ShowInEditForm property to False;
3) Modify the EditForm through SharePoint Designer and use javascript to find the element and hide the control.
4) Build your own Custom Rendering Template
Here I give the second approach demo.
// Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
namespace ModifyMossSPListFiledAsReadOnlyTool
{
public partial class ModifyMossSPListFiledAsReadOnlyTool : Form
{
private string siteUrl = "";
private string webUrl = "";
private string listName = "";
public ModifyMossSPListFiledAsReadOnlyTool()
{
InitializeComponent();
}
/// <summary>
///
/// </summary>
private void LoadFields()
{
SPSite site = null;
SPWeb web = null;
siteUrl = txtSite.Text.Trim();
webUrl = txtWeb.Text.Trim();
listName = txtList.Text.Trim();
gdFields.Rows.Clear();
try
{
site = new SPSite(siteUrl);
web = site.OpenWeb(webUrl);
SPList spList = web.Lists[listName];
SPFieldCollection spFields = spList.Fields;
gdFields.Rows.Add(spFields.Count);
for (int i = 0; i < spFields.Count; i++)
{
gdFields["FieldName", i].Value = spFields[i].InternalName;
gdFields["ShowInNewForm", i].Value = spFields[i].ShowInNewForm;
gdFields["ShowInEditForm", i].Value = spFields[i].ShowInEditForm;
gdFields["ShowInDisplayForm", i].Value = spFields[i].ShowInDisplayForm;
//ReadOnly,HideInList,FieldIndex,DisplayName
gdFields["ReadOnly", i].Value = spFields[i].ReadOnlyField;
gdFields["HideInList", i].Value = spFields[i].Hidden;
gdFields["FieldIndex", i].Value = spFields[i].Id;
gdFields["DisplayName", i].Value = spFields[i].Title;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message+":/n"+ex.StackTrace);
}
finally
{
if (site != null)
{
site.Dispose();
}
if (web != null)
{
web.Dispose();
}
}
}
/// <summary>
///
/// </summary>
private void SaveChanges()
{
SPSite site = null;
SPWeb web = null;
try
{
site = new SPSite(siteUrl);
web = site.OpenWeb(webUrl);
SPList spList = web.Lists[listName];
for (int i = 0; i < gdFields.Rows.Count-1; i++)
{
SPField spField = spList.Fields[new Guid(gdFields["FieldIndex", i].Value.ToString())];
spField.ShowInNewForm = gdFields["ShowInNewForm", i].Value == null ? false : (bool)gdFields["ShowInNewForm", i].Value;
spField.ShowInEditForm = gdFields["ShowInEditForm", i].Value == null ? false : (bool)gdFields["ShowInEditForm", i].Value;
spField.ShowInDisplayForm = gdFields["ShowInDisplayForm", i].Value == null ? false : (bool)gdFields["ShowInDisplayForm", i].Value;
spField.ReadOnlyField = gdFields["ReadOnly", i].Value == null ? false : (bool)gdFields["ReadOnly", i].Value == null;
spField.Update();
}
MessageBox.Show("Saved successfully!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + ":/n" + ex.StackTrace);
}
finally
{
if (site != null)
{
site.Dispose();
}
if (web != null)
{
web.Dispose();
}
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSearch_Click(object sender, EventArgs e)
{
LoadFields();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSave_Click(object sender, EventArgs e)
{
SaveChanges();
}
}
}
// Form1.Designer.cs
namespace ModifyMossSPListFiledAsReadOnlyTool
{
partial class ModifyMossSPListFiledAsReadOnlyTool
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.txtSite = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.txtWeb = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.txtList = new System.Windows.Forms.TextBox();
this.btnSearch = new System.Windows.Forms.Button();
this.btnSave = new System.Windows.Forms.Button();
this.gdFields = new System.Windows.Forms.DataGridView();
this.FieldName = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.ShowInNewForm = new System.Windows.Forms.DataGridViewCheckBoxColumn();
this.ShowInEditForm = new System.Windows.Forms.DataGridViewCheckBoxColumn();
this.ShowInDisplayForm = new System.Windows.Forms.DataGridViewCheckBoxColumn();
this.ReadOnly = new System.Windows.Forms.DataGridViewCheckBoxColumn();
this.HideInList = new System.Windows.Forms.DataGridViewCheckBoxColumn();
this.FieldIndex = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.DisplayName = new System.Windows.Forms.DataGridViewTextBoxColumn();
((System.ComponentModel.ISupportInitialize)(this.gdFields)).BeginInit();
this.SuspendLayout();
//
// txtSite
//
this.txtSite.Location = new System.Drawing.Point(59, 9);
this.txtSite.Name = "txtSite";
this.txtSite.Size = new System.Drawing.Size(260, 21);
this.txtSite.TabIndex = 0;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 12);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(29, 12);
this.label1.TabIndex = 1;
this.label1.Text = "Site";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(340, 12);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(23, 12);
this.label2.TabIndex = 2;
this.label2.Text = "Web";
//
// txtWeb
//
this.txtWeb.Location = new System.Drawing.Point(369, 9);
this.txtWeb.Name = "txtWeb";
this.txtWeb.Size = new System.Drawing.Size(180, 21);
this.txtWeb.TabIndex = 3;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(568, 12);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(29, 12);
this.label3.TabIndex = 4;
this.label3.Text = "List";
//
// txtList
//
this.txtList.Location = new System.Drawing.Point(603, 13);
this.txtList.Name = "txtList";
this.txtList.Size = new System.Drawing.Size(100, 21);
this.txtList.TabIndex = 5;
//
// btnSearch
//
this.btnSearch.Location = new System.Drawing.Point(192, 50);
this.btnSearch.Name = "btnSearch";
this.btnSearch.Size = new System.Drawing.Size(95, 23);
this.btnSearch.TabIndex = 6;
this.btnSearch.Text = "SearchFileds";
this.btnSearch.UseVisualStyleBackColor = true;
this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
//
// btnSave
//
this.btnSave.Location = new System.Drawing.Point(356, 49);
this.btnSave.Name = "btnSave";
this.btnSave.Size = new System.Drawing.Size(126, 23);
this.btnSave.TabIndex = 7;
this.btnSave.Text = "Save Changes";
this.btnSave.UseVisualStyleBackColor = true;
this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
//
// gdFields
//
this.gdFields.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.gdFields.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.FieldName,
this.ShowInNewForm,
this.ShowInEditForm,
this.ShowInDisplayForm,
this.ReadOnly,
this.HideInList,
this.FieldIndex,
this.DisplayName});
this.gdFields.Location = new System.Drawing.Point(14, 92);
this.gdFields.Name = "gdFields";
this.gdFields.RowTemplate.Height = 23;
this.gdFields.Size = new System.Drawing.Size(681, 349);
this.gdFields.TabIndex = 8;
//
// FieldName
//
this.FieldName.Frozen = true;
this.FieldName.HeaderText = "FieldName";
this.FieldName.Name = "FieldName";
this.FieldName.ReadOnly = true;
//
// ShowInNewForm
//
this.ShowInNewForm.HeaderText = "ShowInNewForm";
this.ShowInNewForm.Name = "ShowInNewForm";
//
// ShowInEditForm
//
this.ShowInEditForm.HeaderText = "ShowInEditForm";
this.ShowInEditForm.Name = "ShowInEditForm";
//
// ShowInDisplayForm
//
this.ShowInDisplayForm.HeaderText = "ShowInDisplayForm";
this.ShowInDisplayForm.Name = "ShowInDisplayForm";
//
// ReadOnly
//
this.ReadOnly.HeaderText = "ReadOnly";
this.ReadOnly.Name = "ReadOnly";
//
// HideInList
//
this.HideInList.HeaderText = "HideInList";
this.HideInList.Name = "HideInList";
//
// FieldIndex
//
this.FieldIndex.HeaderText = "FieldIndex";
this.FieldIndex.Name = "FieldIndex";
this.FieldIndex.ReadOnly = true;
//
// DisplayName
//
this.DisplayName.HeaderText = "DisplayName";
this.DisplayName.Name = "DisplayName";
this.DisplayName.ReadOnly = true;
//
// ModifyMossSPListFiledAsReadOnlyTool
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(707, 453);
this.Controls.Add(this.gdFields);
this.Controls.Add(this.btnSave);
this.Controls.Add(this.btnSearch);
this.Controls.Add(this.txtList);
this.Controls.Add(this.label3);
this.Controls.Add(this.txtWeb);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.txtSite);
this.MaximizeBox = false;
this.MaximumSize = new System.Drawing.Size(715, 480);
this.MinimumSize = new System.Drawing.Size(715, 480);
this.Name = "ModifyMossSPListFiledAsReadOnlyTool";
this.Text = "ModifyMossSPListFiledAsReadOnlyTool";
((System.ComponentModel.ISupportInitialize)(this.gdFields)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox txtSite;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtWeb;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox txtList;
private System.Windows.Forms.Button btnSearch;
private System.Windows.Forms.Button btnSave;
private System.Windows.Forms.DataGridView gdFields;
private System.Windows.Forms.DataGridViewTextBoxColumn FieldName;
private System.Windows.Forms.DataGridViewCheckBoxColumn ShowInNewForm;
private System.Windows.Forms.DataGridViewCheckBoxColumn ShowInEditForm;
private System.Windows.Forms.DataGridViewCheckBoxColumn ShowInDisplayForm;
private System.Windows.Forms.DataGridViewCheckBoxColumn ReadOnly;
private System.Windows.Forms.DataGridViewCheckBoxColumn HideInList;
private System.Windows.Forms.DataGridViewTextBoxColumn FieldIndex;
private System.Windows.Forms.DataGridViewTextBoxColumn DisplayName;
}
}