locked
Issue with Eventhandler to check-in a document in a library RRS feed

  • Question

  • I wrote this code to check-in a document in a library.

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.SharePoint;

    namespace CheckInEventHandlerJ
    {
        class CheckInEventHandlerRx : SPFeatureReceiver
      {
            public override void FeatureActivated(SPFeatureReceiverProperties properties)
            {
                //throw new Exception("The method or operation is not implemented
                using (SPWeb web = properties.Feature.Parent as SPWeb)
                {
                    try
                    {
                        

                       web.Lists["Folder Name"].EventReceivers.Add(SPEventReceiverType.ItemAdded, "CheckInEventHandlerJ, Version=1.0.0.0, Culture=neutral, PublicKeyToken=bf95e83472418a73", "CheckInEventHandlerJ.Class1");

                        web.Lists["Folder Name"].EventReceivers.Add(SPEventReceiverType.ItemUpdated, "CheckInEventHandlerJ, Version=1.0.0.0, Culture=neutral, PublicKeyToken=bf95e83472418a73", "CheckInEventHandlerJ.Class1");
                    }
                    catch
                    {

                    }

                }
            }

            public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
            {
                //  throw new Exception("The method or operation is not implemented.");
            }

            public override void FeatureInstalled(SPFeatureReceiverProperties properties)
            {
                //throw new Exception("The method or operation is not implemented.");
            }

            public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
            {
                //throw new Exception("The method or operation is not implemented.");
            }
        }
    }

    Here is code in Class1.cs

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.SharePoint;

    namespace CheckInEventHandlerJ
    {
        class Class1: SPItemEventReceiver
        {
            public override void ItemAdded(SPItemEventProperties properties)
            {
                base.ItemAdded(properties);
                try
                {
                    CHeckInDOcuments(properties);
                }
                catch
                {
                }


            }
            public override void ItemUpdated(SPItemEventProperties properties)
            {
                base.ItemUpdated(properties);
                try
                {
                    CHeckInDOcuments(properties);
                }
                catch
                {
                }

            }
            public void CHeckInDOcuments(SPItemEventProperties properties)
            {

                using (SPSite site = new SPSite(properties.SiteId))
                {
                    using (SPWeb web = site.OpenWeb(properties.RelativeWebUrl))
                    {
                        SPSecurity.RunWithElevatedPrivileges(delegate()
                        {
                            using (SPSite targetsite = new SPSite(site.ID))
                            {
                                using (SPWeb targetweb = targetsite.AllWebs[web.ID])
                                {
                                    targetweb.AllowUnsafeUpdates = true;
                                    SPList doclib = targetweb.Lists[properties.ListId];
                                    if (doclib != null)
                                    {
                                        SPListItem uploadedDocmnt = properties.ListItem;
                                        if (uploadedDocmnt.File.CheckOutStatus != SPFile.SPCheckOutStatus.None)
                                        {
                                            uploadedDocmnt.File.CheckIn("document been uploaded and checkedin");
                                            uploadedDocmnt.SystemUpdate();
                                            
                                        }
                                    }
                                    targetweb.AllowUnsafeUpdates = false;
                                }
                            }
                        });

                    }
                }
            }


        }
    }

    But after uploading a document and filling the data it is generating the following error "The file XXXX.docx has been modified by XXXXXXXXXXXX on (current date and time)."

    I am thinking the error may be in the code. Can someone help me in correcting the code.

    • Edited by Sasisai Monday, June 18, 2012 5:06 PM
    Thursday, May 10, 2012 3:04 PM

All replies