locked
Event Receiver not firing for all items RRS feed

  • Question

  • Hello,

    in our development environment I have created a standard image library.

    To this image library I have attached a event receiver that fires when an item was added.

    This is the elements.xml from the event receiver:

    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
      <Receivers ListUrl="NSAPictures/Forms" >
        <Receiver>
          <Name>EventReceiver1ItemAdded</Name>
          <Type>ItemAdded</Type>
          <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
          <Class>SharePointProject7.EventReceiver1.EventReceiver1</Class>
          <SequenceNumber>10000</SequenceNumber>
        </Receiver>
      </Receivers>
    </Elements>

    My problem is that the event receiver does not fire on every item.

    For example: I drag'n'drop 10 images to the library. The event receiver fires for 7 images, but not for the other 3. I can't really explain why.

    This is the code I've been using:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Utilities;
    using System.Drawing;
    using System.IO;
    
    namespace SharePointProject7.EventReceiver1
    {
        /// <summary>
        /// Listenelementereignisse
        /// </summary>
        public class EventReceiver1 : SPItemEventReceiver
        {
            /// <summary>
            /// ItemAdded
            /// </summary>
            public override void ItemAdded(SPItemEventProperties properties)
            {
                try
                {
                    SPFile file = properties.ListItem.File;
                    if (null != file)
                    {
                        properties.OpenWeb().AllowUnsafeUpdates = true;
                        try
                        {
                            MemoryStream msOut = new MemoryStream();
                            MemoryStream msIn = new MemoryStream(file.OpenBinary(), true);
    
                            int width = Image.FromStream(msIn).Width / 2;
                            int height = Image.FromStream(msIn).Height / 2;
    
                            using (Image img = Image.FromStream(msIn))
                            {
    
                                using (Bitmap bitmap = new Bitmap(img, width, height))
                                {
    
                                    bitmap.Save(msOut, System.Drawing.Imaging.ImageFormat.Jpeg);
    
                                }
                            }
                            file.SaveBinary(msOut.GetBuffer(), false);
    
                            file.CheckOut();
    
                            file.Update();
    
                            file.CheckIn(string.Empty);
    
                        }
                        finally
                        {
                            properties.OpenWeb().AllowUnsafeUpdates = false;
                        }
                    }
                }
                catch
                {
                    //Do some exception handling 
                    throw;
                }
            }
        }
    }

    Can anyone help me?:)

    Thanks in advance!


    • Edited by Twychopath Tuesday, April 3, 2018 8:46 PM
    Tuesday, April 3, 2018 8:44 PM

Answers

  • If I let the event receiver change a column I can see that it not fires for all items. For testing I changed Synchronization to "Synchronous" and then it works for all items.

    It also works great (with Synchronization set to "Asynchronous") when all items smaller than < 40MB.


    Friday, April 6, 2018 8:01 AM

All replies

  • Hi Twychopath,

    I understand in this event receiver, you want to change the image width and height to half, I tested the code snippet in my side and drag 10 images to a Picture library and it seems work for all images like below, both change the height width to half:

    I would suggest you can debug event receiver to check if the problem images has been triggered into event receiver and do a simple test with the 3 problem images, more possible this issue is related with the images file itself.

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Wednesday, April 4, 2018 10:41 AM
  • If I let the event receiver change a column I can see that it not fires for all items. For testing I changed Synchronization to "Synchronous" and then it works for all items.

    It also works great (with Synchronization set to "Asynchronous") when all items smaller than < 40MB.


    Friday, April 6, 2018 8:01 AM
  • Hi Twychopath,

    Synchronous Events are executed before the action is executed on the content database While Asynchronous Events are executed after the action is executed on the content database. I think when image file size is large, than Asynchronous won't execute for some items while Synchronous Events will be executed finished before submitted to Content Database.

    And Thanks for your sharing the solution, I would suggest you can Mark it as answer so that it could help others who stuck with the same issue in the forum.

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Friday, April 6, 2018 10:16 AM