.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Adding/Updating/deleting in SharePoint Event Receiver

Posted By:abhays       Posted Date: October 29, 2013    Points: 200    Category: SharePoint    URL: http://www.dotnetspark.com  

Hi, below i have described basics of Event receiver.This Event receiver must be attached to document Library The flow is as follows:
 


1. Document is added to Library

2.You can check to see if a folder within another document library exists based on metadata of document.

3. If the folder does not exist,Even receiver creates it.

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

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

            using (SPSite spSite = new SPSite(properties.SiteId))
            {
                SPWeb spWeb = spSite.OpenWeb(properties.RelativeWebUrl);

                SPFolder spFolder = spWeb.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(spWeb.Lists["My Document"], spFolder, destinationFolder);
                    //CopyListItems("http://dvnap-spdev08:2020","http://dvnap-spdev08:2020/nalco", "My Document");
                }

            }


        }

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

            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);
       }

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend