Errors and solutions when using SharePoint EventHandler added and updated events

Source: Internet
Author: User

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}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.