SharePoint: Setting value of readonly fields programmatically

When created items in SharePoint via the object model, you can convert a readonly property of that field to be false in order to allow you to set the value of that field. This is particularly useful for setting created and modified dates (all SharePoint lists retain these).

// get the list and set modified property to allow writing
SPWeb web = new SPSite("http://url/to/web").OpenWeb();
SPList selectedList = web.Lists["listname"];
selectedList.Fields["Modified"].ReadOnlyField = false;

// set the item
SPItem newItem = selectedList.Items[0];
newItem["Modified"] = DateTime.Now;

// Set readonly back to true
selectedList.Fields["Modified"].ReadOnlyField = true;

You May Also Like

About the Author: rnowik

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.