Skip to main content

Event handler to copy items from one site to another

The code can be used to copy items from one site to another ,but source and destination sites must be in same server.
If the item was added newly in a Document Library , Item Checked In Event Handler must be used :
  
public override void ItemCheckedIn(SPItemEventProperties properties)
       {
           base.ItemCheckedIn(properties);
           try
           {
               SPSite sourceSite = new SPSite("Source Site URL");
               SPWeb sourceweb = sourceSite.OpenWeb();
               SPList sourcelist = sourceweb.Lists["Source List"];
               if (properties.ListTitle == sourcelist.Title)
               {
                   SPListItem sourceitem = properties.ListItem;
                   string title = sourceitem["Title"].ToString();
                   string description = sourceitem["ToolName"].ToString()";
                   SPSite _destSite = new SPSite("Destination Site URL");
                   SPWeb _destweb = _destSite.OpenWeb();
                   SPList _destList = _destweb.Lists["DestinationList"];
                   SPListItem _destitem = _destList.Items.Add();
                   destweb.AllowUnsafeUpdates = true;
                   destitem["Title"] = title;
                   destitem["Description"] = description;
                   destitem.SystemUpdate();
                   destList.Update();

                   destweb.AllowUnsafeUpdates = false;
               }
           }
           catch (Exception ex)
           {
               properties.ErrorMessage = ex.Message;
           }
       }

Comments

Popular posts from this blog

How to link to Download a copy option in Office 365/SharePoint 2013

At times it is required to have the option to save a copy of a document uploaded in SharePoint document library, in a page or a webpart. The url to get the save option for a doc is - http:// site /_layouts/download.aspx?SourceUrl= url-of-document-in-library Change the bold text with the actual url, i.e., with the site url and the url to the doc in the doc library. For example, if the site is www.contso.com and the doc "Test" is uploaded in SiteAssets library, the url to save a copy would be - http://contso.com/_layouts/download.aspx?SourceUrl=http://contso.com/SiteAssets/Test.docx

Cannot open SharePoint 2010 Approval task form from Outlook

When trying to click on Open this task from Outlook 2010, it brings an error - " Outlook cannot open a new form. The form contains schema validation errors" Element '{ http://www.w3.org/1999/xhtml}div ' is unexpected according to the content model of parent element '{ http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields}Body '. As given in the error message, the issue is with the Body column of the workflow tasks list. For the approval workflow, there would be a tasks list. In the workflow task list, the Body column is Enhance Rich multi-line column. Change the setting to Plain text, instead of enhanced rich text. After that do an iisreset.

Display a Thank You message at the end of survey form in Sharepoint 2010

A quick and easy way to display a Thank you message at the end of survey for submitting the form. At the end of last question, insert a page breaker. After the page breaker, add the Thank you message as a question, and select the type Single line of text. Enter the default value as "Internal". Go to the survey form and click on Respond to Survey. Click Next and in the Editform url in the browser, append &ToolPaneView=2 to the end of URL. Add a CEWP to the below of the page and insert the below code, in HTML source or else paste the code in a text file uploaded to sharepoint library and refer to the text file. (referring the text file is preferred) <script> // hide the input textbox for the "thank you " message  var x = document.getElementsByTagName("INPUT");  var i=0;  for (i=0; i<x.length; i++)  {   if (x[i].value=="Internal")   {     x[i].style.display="none";   }  }  </script> Save the page. Respo...