Previously, we encountered such a functional requirement: when adding/updating data in the database, we also need to update some of its own columns to find occasional errors. After constantly searching for materials and debugging, we can chew on the code, finally, find the problem and solution, and share it with you.
Added problem: For example, after adding a wiki page, add its URL access address to the specified column of the current Item data. (To read the URL, make the <a> href and click to access it)
Practice: Add EventHandler, listen to the added event, get the url, and save the url in the specified column.
Occasionally error message: the document is modified by the SharePoint \ System user. Update information failed
Updated Problems: After debugging, it is found that both listItem. Update () and listItem. SystemUpdate () call the updated event recursively to form an endless loop.
Explanations and solutions for these two situations:
Added Problem Analysis and Solution:
By default, a SharePoint event is executed in synchronous mode, and an added event is executed asynchronously. Database events are used to modify data and lock the event to improve data consistency. Therefore, added uses the Asynchronous Method by default. When the lock of the previous data is not released, added reports an error. Therefore, the synchronization mode is changed. After the lock is released, it can be executed again.
Modify feature XML as follows:
<Cycler>
<Name> NoticeEventReceiverItemAdded </Name>
<Type> ItemAdded </Type>
<Assembly> $ SharePoint. Project. AssemblyFullName $ </Assembly>
<Class> TianjinProject. NoticeEventReceiver. NoticeEventReceiver </Class>
& Lt; SequenceNumber & gt; 10000 & lt;/SequenceNumber & gt;
<Synchronization> Synchronous </Synchronization>
</Cycler>
UpdatedProblem AnalysisSolution:
Pay attention to the 13 and 16 lines of code.
1 public override void ItemUpdated (SPItemEventProperties properties) 2 {3 try 4 {5 using (SPWeb web = properties. openWeb () 6 {7 web. allowUnsafeUpdates = true; 8 SPList list = properties. list; 9 SPListItem listItem = properties. listItem; 10 string url = list. fields. getField ("webpage address "). internalName; 11 listItem [url] = "http://aa.com"; 12 // truncation event listening, resolving loop self-calling, 13 this. eventFiringEnabled = false; 14 listItem. systemUpdate (); 15 // Add the event 16 after the update. this. eventFiringEnabled = true; 17 web. allowUnsafeUpdates = true; 18} 19} 20}