Creating a read-only field in a list programmatically RRS feed

  • Question

  • I have an existing Sharepoint list and I need to programmatically at runtime add a new field to it that will be read-only (or if that's not possible then at least make it not show up on the Edit Properties screen for a list item).

    I have the code set up as shown below but when I execute it and go to the edit properties screen, the field is both visible and editable.  Could you please let me know what I'm doing wrong?  Here is the code:

    web.AllowUnsafeUpdates = true;
    list.Fields.Add("IsMergedDocument", SPFieldType.Boolean, false);
    list.Fields["IsMergedDocument"].ReadOnlyField = true;

    list.Fields["IsMergedDocument"].ShowInEditForm = false;

    list.Fields["IsMergedDocument"].Required = false;
    web.AllowUnsafeUpdates = false;

    Another thing I don't quite understand is the difference between list.Fields and item.Fields.  I tried adding this property to an individual list item which resulted in this property being visible on the Edit Properties screen for each list item in this list so what is the difference between list.Fields.Add() and item.Fields.Add()?


    Wednesday, October 17, 2012 6:48 PM

All replies

  • In SharePoint 2007, there isn't the ability to set a field as Read-Only, at least not out of the box.  Bamboo has a custom widget that allows you to add columns with specific permissions. (There might be others in CodePlex, too.)  So, I don't know what's really happening there.

    This might help, too: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splist.fields(v=office.12).aspx

    Steve Clark, MCTS | Twin-Soft.com
    Easy Bins Dumpster Rentals of Northern VA. (Specializing in driveway-sized roll-off dumpsters.)
    Be sure to click one of them there forum buttons that artificially inflates my narcissitic ego... I mean gives me points.

    Wednesday, October 17, 2012 7:39 PM
  • It possible to make fields in a List readonly using a custom workflow.

    You have to modify it to use with your workflow

    SPSite osite = new SPSite(http://site);

    SPWeb oweb = osite.OpenWeb();

    SPList olist = oweb.Lists["listname"];

    olist.Fields["fieldname"].ReadOnlyField = true;


    Wednesday, October 17, 2012 9:52 PM
  • Ashish,

    Your suggestion seems to be using the same technique I'm using.  Specifically, you have a line:

    olist.Fields["fieldname"].ReadOnlyField = true;

    And I had it as:

    list.Fields["IsMergedDocument"].ReadOnlyField = true;

    Also, I need to add a new field and make it read only, not update an existing field.  I am already using this code in a custom workflow.  The user clicks a custom menu item and the following code executes (I gave more code this time in case the issue is somewhere else

    protected void Page_Load(object sender, EventArgs e)
            string listId = Request["ListId"];

                using (SPSite site = new SPSite(SPControl.GetContextSite(HttpContext.Current).ID))
                    SPWeb web = SPControl.GetContextWeb(HttpContext.Current);
                    Guid listGuid = new Guid(listId);
                    SPList list = web.Lists[listGuid];

                    web.AllowUnsafeUpdates = true;

                    list.Fields.Add("IsMergedDocument", SPFieldType.Boolean, false);

                    SPField f = list.Fields["IsMergedDocument"];
                    f.ReadOnlyField = true;
                    f.Required = false;
                    f.ShowInEditForm = false;
                    web.AllowUnsafeUpdates = false;

    Thursday, October 18, 2012 12:16 PM