0

I need to figure out whether a document is modified in a document library (and not its properties). is it possible to use an event receiver to compute the hash of the document before it is updated and after its updated ? Please share the code along with the references if any.....

2 Answers 2

1

You can compare the BeforeProperties and AfterProperties of an event receiver to check for any changes in the properties.

Related question here

Code would be something like this:

    // ItemUpdated
    public override void ItemUpdated(SPItemEventProperties properties)
    {
        base.ItemUpdated(properties);
        if (!EqualProperties(properties.AfterProperties,properties.BeforeProperties))
        { 

        }
    }

Where EqualProperties is a method which compares the collections.

If you want to compare hashvalues you can create a text column to store the hash.

    public override void ItemAdded(SPItemEventProperties properties)
    {
        base.ItemAdded(properties);
        this.DisableEventFiring();
        properties.ListItem["myCustomField"] = this.CalculateHash(properties.ListItem.File);
        properties.ListItem.SystemUpdate();
        this.EnableEventFiring();
    }

    public override void ItemUpdated(SPItemEventProperties properties)
    {
        this.DisableEventFiring();
        properties.ListItem["myCustomField"] = this.CalculateHash(properties.ListItem.File);
        properties.ListItem.SystemUpdate();
        this.EnableEventFiring();
        base.ItemUpdated(properties);
        if (properties.ListItem["myCustomField"] != properties.BeforeProperties["myCustomField"])
        { 

        }
    }
4
  • Thanks Anima, but my major concern is to determine whether the document content is modified or not.
    – Vish
    Dec 4, 2013 at 9:50
  • Updated the code. You can use the OpenBinaryStream method on the SPFile object and use the stream to create a hash value with the type System.Security.Cryptography.MD5 which exposes a ComputeHash() method. Dec 4, 2013 at 10:17
  • Which reference/namespace is needed to use the CalculateHash() method ? or is it a user-defined method ?
    – Vish
    Dec 4, 2013 at 10:34
  • More information. The namespace is System.Security.Cryptography. Dec 4, 2013 at 10:39
0
    try
               {
                   this.EventFiringEnabled = false;
                   SPListItem CurrentItem = properties.ListItem;
                   SPFile doc = CurrentItem.File;
                   //Stream stream = doc.OpenBinaryStream(

                   MD5 md5 = new MD5CryptoServiceProvider();

                    /* Calculate Hash */
                   byte[] hash1 = md5.ComputeHash(doc.OpenBinary()); //doc.OpenBinary() returns byte[]




                   /* Show Hash in TextBoxes */
                   CurrentItem["CustomFieldName"] = BitConverter.ToString(hash1); 

                   CurrentItem.Update();
               }
               finally
               {
                   this.EventFiringEnabled = true;
               }

Need to include the System.Security.Cryptography namespace. Also include the following tag in the elements.xml file before end of Receiver tag :

    <Synchronization>Synchronous</Synchronization>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.