Search This Blog

Friday, November 2, 2012

Sharepoint SPItemEventReceiver Adding/Updating/deleting


I am in the process of writing a custom event receiver. The basic flow is as follows:

1. Document is added to Library

2. Based on metadata of document, we check to see if a folder within another document library exists.

3. If the folder does not exist, it is created.

4. The newly added document is copied to the folder residing in another document library.



Create WSP Project and Choose Event Receiver ->ItemsEventsList ->Document Library ->Check Adding,updating,deleting

/// <summary>
       /// An item is being added.
       /// </summary>
        public override void ItemAdding(SPItemEventProperties properties)
        {
            base.ItemAdding(properties);

            using (SPSite oSiteCollectionEvent = new SPSite(properties.SiteId))
            {
                SPWeb oSiteEvent = oSiteCollectionEvent.OpenWeb(properties.RelativeWebUrl);

                SPFolder sourceFolder = oSiteEvent.GetFolder("My Document");

                using (SPSite oSiteCollection = new SPSite("http://dvnap-spdev08:2020/"))
                {
                    SPWeb oWebsite = oSiteCollection.OpenWeb("nalco");

                    oWebsite.AllowUnsafeUpdates = true;
                    //get the folder to the destination
                    SPFolder destinationFolder = oWebsite.GetFolder("My Document");
                    RecursiveCopy(oSiteEvent.Lists["My Document"], sourceFolder, destinationFolder);
                    //CopyListItems("http://dvnap-spdev08:2020","http://dvnap-spdev08:2020/nalco", "My Document");
                }

            }


        }

        private static void RecursiveCopy(SPList objSourceList, SPFolder objSourceFolder, SPFolder objDestinationFolder)
        {
            SPListItemCollection objItems = ((SPDocumentLibrary)objSourceList).GetItemsInFolder(objSourceList.DefaultView, objSourceFolder);

            foreach (SPListItem objItem in objItems)
            {
                //If it's a file copy it.
                if (objItem.FileSystemObjectType == SPFileSystemObjectType.File)
                {

                    byte[] fileBytes = objItem.File.OpenBinary();
                    string DestinationURL = string.Format(@"{0}/{1}", objDestinationFolder.Url, objItem.File.Name);

                    //Copy the file.
                    SPFile objDestinationFile = objDestinationFolder.Files.Add(DestinationURL, fileBytes, true);
                    objDestinationFile.Update();
                }
                else
                {
                    string dirURL = string.Format(@"{0}/{1}", objDestinationFolder.Url, objItem.Folder.Name);
                    SPFolder objNewFolder = objDestinationFolder.SubFolders.Add(dirURL);
                    objNewFolder.Update();

                    //Copy all the files in the sub folder
                    RecursiveCopy(objSourceList, objItem.Folder, objNewFolder);
                }
            }
        }

        public static void CopyListItems(string SourceSiteURL, string DestinationSiteURL, string ListName)
        {
            string DestinationURL = string.Empty;

            using (SPSite SourceSite = new SPSite(SourceSiteURL))
            {
                using (SPWeb SourceWeb = SourceSite.OpenWeb())
                {
                    using (SPSite DestinationSite = new SPSite(DestinationSiteURL))
                    {
                        using (SPWeb DestinationWeb = DestinationSite.OpenWeb())
                        {
                            DestinationWeb.AllowUnsafeUpdates = true;

                            //Get the QA Forms Document libarary from the source web
                            SPList objSourceList = SourceWeb.Lists[ListName];

                            SPList objDestinationList = null;

                            try
                            {
                                objDestinationList = DestinationWeb.Lists[ListName];
                            }
                            catch
                            {
                                //Create a list in the destination web
                                DestinationWeb.Lists.Add(ListName, string.Empty, SPListTemplateType.DocumentLibrary);
                            }

                            objDestinationList = DestinationWeb.Lists[ListName];

                            //Recursively copy all the files and folders
                            RecursiveCopy(objSourceList, objSourceList.RootFolder, objDestinationList.RootFolder);



                            DestinationWeb.Update();
                            DestinationWeb.AllowUnsafeUpdates = false;
                        }
                    }
                }
            }
        }
    

       /// <summary>
       /// An item is being updated.
       /// </summary>
       public override void ItemUpdating(SPItemEventProperties properties)
       {
           base.ItemUpdating(properties);
       }

       /// <summary>
       /// An item is being deleted.
       /// </summary>
       public override void ItemDeleting(SPItemEventProperties properties)
       {
           base.ItemDeleting(properties);
       }

1 comment:

  1. Currently seeking a SharePoint developer who is skilled in both SharePoint 2010 and Master Pages.

    ReplyDelete