In my previous post I demonstrated how easy it was to have a document copied, on upload, to another document library with very few lines of code. This involved utilizing an Event Receiver which performed an override using the ItemUpdated method. This would also support document updates as the code utilized the ItemUpated method.
Since then I have been working on a feature receiver which that creates a SharePoint list that defines the source and target document libraries for the copy operation.
The ideal approach is to code this so that when the Document Library Replicator feature is activated (the event receiver), we would like the replication configuration list to be created at the same time in the current Web. This has been achieved by created a Feature Receiver within the project that does the following:
1 – Create the “Replicator Configuration” list.
2 – Sets the crawl attribute for the list to none.
3 – Create two text fields for the source and target path and name
4 – Adds the two new text fields to the default view.
The feature receiver only overrides the FeatureActivated method, and does not remove the list when the Document Library Replicator feature is de-activated.
The code for the feature receiver is below:
public class Document_Library_ReplicatorEventReceiver: SPFeatureReceiver
{
.public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
try
{
using (SPWeb spWeb = (SPWeb)properties.Feature.Parent)
{
// Create the replicator configuration list
SPSite spSite = spWeb.Site;
spWeb.Lists.Add(“Replicator Configuration”, “Document Library Replicator Configuration”, spWeb.ListTemplates[“Custom List”]);
SPList replicatorConfigList = spWeb.Lists[“Replicator Configuration”];
replicatorConfigList.NoCrawl = true;
replicatorConfigList.Fields.Add(“Source Library Path and Name”, SPFieldType.Text, true);
replicatorConfigList.Fields.Add(“Target Library Path and Name”, SPFieldType.Text, true);
replicatorConfigList.Update();
// Update the default view to show the new fields
SPView spView = replicatorConfigList.DefaultView;
spView.ViewFields.Add(“Source Library Path and Name”);
spView.ViewFields.Add(“Target Library Path and Name”);
spView.Update();
}
}
catch (SPException spEx)
{
}
finally
{
}
}
}