Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Use an item event receiver and override the <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spitemeventreceiver.itemadded.aspx" rel="noreferrer">ItemAdded</a> event. <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spitemeventproperties_members.aspx" rel="noreferrer">SPItemEventProperties</a> will give you a reference to the list item via the ListItem property.</p> <p>There are two methods to do this (thanks to your discovery of CopyTo).</p> <p><strong>Method 1: Use <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitem.copyto.aspx" rel="noreferrer">CopyTo</a></strong></p> <p>This method copies any list item with its associated file and properties to any location in the same site collection (possibly other web applications as well but I haven't tested). SharePoint automatically maintains the link to the source item as well if you view the item's properties or use its drop-down menu. This link can be removed with <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitem.unlinkfromcopysource.aspx" rel="noreferrer">UnlinkFromCopySource</a>.</p> <p>The only trick to CopyTo is that a full URL is required for the destination location.</p> <pre><code>public class EventReceiverTest : SPItemEventReceiver { public override void ItemAdded(SPItemEventProperties properties) { properties.ListItem.CopyTo( properties.WebUrl + "/Destination/" + properties.ListItem.File.Name); } } </code></pre> <p><strong>Method 2: Stream copy, manually set properties</strong></p> <p>This method would only be necessary if you need more control over which item properties are copied or if the file's contents need to be altered.</p> <pre><code>public class EventReceiverTest : SPItemEventReceiver { public override void ItemAdded(SPItemEventProperties properties) { SPFile sourceFile = properties.ListItem.File; SPFile destFile; // Copy file from source library to destination using (Stream stream = sourceFile.OpenBinaryStream()) { SPDocumentLibrary destLib = (SPDocumentLibrary) properties.ListItem.Web.Lists["Destination"]; destFile = destLib.RootFolder.Files.Add(sourceFile.Name, stream); stream.Close(); } // Update item properties SPListItem destItem = destFile.Item; SPListItem sourceItem = sourceFile.Item; destItem["Title"] = sourceItem["Title"]; //... //... destItem["FieldX"] = sourceItem["FieldX"]; //... destItem.UpdateOverwriteVersion(); } } </code></pre> <p><strong>Deployment</strong></p> <p>You have various options for deployment as well. You can associate event receivers with a feature connected to a content type or list, and programmatically add them. See <a href="http://www.sharepointdevwiki.com/display/public/Event+Receivers" rel="noreferrer">this article at SharePointDevWiki</a> for more details.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload