次の方法で共有


Creating a read-only field with default value in a SharePoint list

Whenever we create a column with type “Single line of text” in SharePoint lists we will get an option for providing default value. If we provide the default value and once if we add any item to that list with that field , then we can see the default value in a text box in the edit or new form and even we can edit it at that time. So, if we want to create a read-only column with default value and we can’t edit it, then you can implement it by using the FieldAdded event.

FieldAdded event is list specific synchronous event and it will get from SPListEventReciver class. Once you attached the event to a list then whenever you add a new column to the list, this event will fire and we can customize it in this hook point. Below I am giving the detailed steps on how we can accomplish this requirment.

1. Create a class library add SharePoint dlls and the a class with the following code.

using System;

using System.Collections.Generic;

using System.Text;

using System.Web;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Utilities;

namespace TestEvent

{

class ReadonlyFieldTest : SPListEventReceiver

{

public override void FieldAdded(SPListEventProperties properties)

{

SPField oField = properties.Field;

if (oField.Title == "MyTestColumn")// you can think about a better check point here.

{

oField.DefaultValue = "MyDefaultValue";

oField.ReadOnlyField = true;

oField.Update();

}

}

}

}

2. Deploy the dll in GAC after compiling the class library with a strong name

3. You can register the event handler using a feature or using SharePoint APIs. If you want to register the event handler to a particular site collection or farm level, then you can go with a custom feature. Since I am testing it with only one List, here I have registered FieldAdded event using SharePoint APIs. I have created a .Net console application to do that.

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.SharePoint;

namespace EventRegistrationTool

{

class Program

{

static void Main(string[] args)

{

using (SPSite oSite = new SPSite("https://<sitename>"))

{

using (SPWeb oWeb = oSite.OpenWeb())

{

SPList oList = oWeb.Lists["Shared Documents"];

string strAssembly = "TestEvent, Version=1.0.0.0, Culture=neutral, PublicKeyToken=fac0274b1d81d1fd";

string strClass = "TestEvent.ReadonlyFieldTest";

oList.EventReceivers.Add(SPEventReceiverType.FieldAdded,strAssembly,strClass);

oList.EventReceivers.Add(SPEventReceiverType.FieldAdding,strAssembly, strClass);

}

}

}

}

}

Comments

  • Anonymous
    October 13, 2008
    1>How to edit the readonly  field. 2>What is the difference betwen readonly n hidden fields (since both are not seen in the editform.aspx)

  • Anonymous
    October 15, 2008

  1. You should explicitly change the readonly   property to false. 2.http://msdn.microsoft.com/en-     us/library/microsoft.sharepoint.spfield_properties.aspx  Readonly :   ReadOnlyField property Gets or sets a   Boolean value that specifies whether values   in the field can be modified.    Hidden :   Hidden  Gets or sets a Boolean value     that specifies whether the field is   displayed in the list.  
  • Anonymous
    November 20, 2008
    why put this in event handler? Just build a console exe application program to run it once! see my blog http://hi.baidu.com/freying/blog/item/f036e5002fc75f011c9583e3.html

  • Anonymous
    January 15, 2009
    Thanks! I've just been thinking about something like this, how to get an event receiver registered. Do you think using a console app to do this one time is a good way to deploy something? I guess a feature is the best way, but it seems like a lot of work. Do you use these "do-the-dirty-work" console apps in production environments? I worry about how to document that I've done that if my site/server ever goes down and I need to recreate things. Thanks again! -Tom

  • Anonymous
    January 15, 2009
    Hi Tom, Yes,your thoughts are correct. The best way would be using a feature. Thus it will be available in your server and whenever you need it, then you can activate it. A one time executiion of console application is not a good method, instead go with feature. Thanks, Sowmyan

  • Anonymous
    February 26, 2009
    To follow up on a previous question: How do I make my field readonly AND have it be displayed on the editForm.aspx?  Sounds silly I know, but we are auto-populating some field values based on the user's uploaded file contents (XML) using ItemAdded.  I want them to see their field values on the edtiForm when they upload, as a sanity check of their XML contents. Thanks, KevinHou

  • Anonymous
    February 26, 2009
    I hope that you can accomplish it by creating a custom field to render the control as a Label. Please refer the following post for getting some sample code. Refer this post and create a custom field which will render a label control. http://blogs.msdn.com/sowmyancs/archive/2008/11/14/work-around-to-render-the-custom-field-in-list-view.aspx

  • Anonymous
    April 06, 2009
    Here I got a tool called SharePoint column view permission from http://www.sharepointboost.com/columnpermission.html. I do not know which one is better since I have not used neither of them.

  • Anonymous
    July 12, 2009
    HI Tom I come again. If u dont like console app, I can give u another option. Download the SPM from codeplex, modify the  field  properties. Remeber modify it from List -> field (do not from contenttype) http://spm.codeplex.com/ Frey

  • Anonymous
    July 12, 2009
    Hi KevinHou U can regeist some javascript code  on the editForm.aspx Frey

  • Anonymous
    May 21, 2011
    djavascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$content$ctl00$w_31575$_a3f425$ctl00$ctl00$ctl00$ctl05$bpCommentForm$ctl05$btnSubmit", "", true, "BlogPostCommentForm-ctl00_content_ctl00_w_31575__a3f425_ctl00_ctl00", "", false, true))

  • Anonymous
    March 11, 2014
    With Meta data field option we can do the same.

  • Anonymous
    October 25, 2015
    how to make this readonly field for dropdown list and checkboxes using  javascript /jQuery in my editform by using content editor web part. help is appreciated!