Freigeben über


How To : Set calculated field to read-only using Object Model

(A) Create an EventHandler using the ClassLibraryTemplate in the visual Studio 2005.

using Microsoft.SharePoint;

public class [ClassName] : SPListEventReceiver

{

public override void FieldUpdating(SPListEventProperties properties)

{

properties.ErrorMessage = "You can't changed the column.";

properties.Cancel = true;

}

}

(B) Register the Event Handler using the following Code.

// https://tangov39:47242/sites/TestCase/ is the site URL.

SPSite collection = new SPSite("https://tangov39:47242/sites/TestCase/");

SPWeb site = collection.OpenWeb();

// Here test2 is document List Name.

SPList test2 = site.Lists["test2"];

string asmName = "DaveHandlers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b5e8677dbb56bf06";

string className = "DaveHandlers.ReadOnlyHandlers";

test2.EventReceivers.Add(SPEventReceiverType.FieldUpdating, asmName, className);

STEPS TO REPRO:

====================

1. Create a list or document library

2. Add/create a new column, select Calculated (calculation based on other columns) field with formula eg = 3*4, be sure to select number as the return data type.

3. Add a few new entries to the list or library.

4. Select edit in datasheet.

5. Attempt to edit the value of the calculated field with some numberic value.

6. You may be prompted for a username/password if the signed in user does not have security permissions. If so sign in with a username/password of an account with a username / password with adequate permissions

7. When the preview/warning message box appears, click continue.

8. Notice the calculated value was changed for each row and if you go back into list settings. You'll see the forumla has changed.

Comments