SharePoint 2010 Document Library Replicator: Part 4

Its been a while since my last post on this solution. The code is almost complete and this is the final update to the current solution. The solution can be downloaded from CodePlex here.

In this post, the code update covers the following areas:

1 – A new method has been added to read the configuration list where we specify source and target document libraries. A new class named SPListOperations was created with a method named queryConfigList to query the replicator configuration list for source and target libraries.

2 – The itemupdated and itemdeleted methods in the solution have been updated to call a method that performs the add and delete operation.

Changes to the Event Receiver methods

///

<summary>

///

 An item was updated.

 ///

 </summary>

 public override void ItemUpdated(SPItemEventProperties properties)

{

 base.ItemUpdated(properties);

replicatorOperation(properties,“Added/Updated”);

}

<summary>

///

An item was deleted

///

</summary>

public override void ItemDeleted(SPItemEventProperties properties)

{

 base.ItemDeleted(properties);

replicatorOperation(properties,“Deleted”);

}

New method to perform the copy/update and delete operations

public void replicatorOperation(SPItemEventProperties properties, string operation)

{

 using (SPSite spsite = newSPSite(properties.WebUrl))

 try

 {

EventFiringEnabled = false;

 if (properties.ListItem.ParentList.Title != “Replicator Logger”)

{

 SPWeb siteForLogger = spsite.OpenWeb();

 // determine target filename – the same as the source

 string targetfilename = properties.ListItem.Name.ToString();

 string SourceLibPath = properties.WebUrl + “/” + properties.ListItem.ParentList.ToString();

 string sourceLibrary = properties.ListItem.ParentList.ToString();

 string targetLibraryFromConfigList = SPListOperations.queryConfigList(siteForLogger, sourceLibrary).ToString();

 // Query the Configuration List

 if (targetLibraryFromConfigList != “NotListed”)

{

 // Copy/Delete the file to the destination library (targetlibrary)

 if (operation == “Added/Updated”)

{

 properties.ListItem.File.CopyTo(targetLibraryFromConfigList + “/” + targetfilename, true

);

}

if (operation == “Deleted”)

{

 SPFile spFileTarget = siteForLogger.GetFile(targetLibraryFromConfigList + “/” + targetfilename);

spFileTarget.Delete();

 }

 // Write to the Replicator Log – unfinished…

 ReplicatorLogger replogger = newReplicatorLogger();

 // Get the current Date and Time

 DateTime dtime = DateTime.Now;

 // Create the title for the logger list item

 string title = (dtime.Date.ToString() + “_” + dtime.Hour.ToString() + “_” + dtime.Minute.ToString() + “_” + dtime.Second.ToString() + “_” + targetfilename.ToString());

// Write to the log list

EventFiringEnabled = false;

replogger.writeToLog(title, dtime.ToShortDateString(), properties.UserDisplayName.ToString(), operation, SourceLibPath, targetfilename, siteForLogger);

}

}

}

catch (SPException spEx) { }

 finally

{

 // Enable Event Firing

 EventFiringEnabled = true;

}

 }

 SPListOperations Class and a new queryConfigList method

using Microsoft.SharePoint;

 namespace DocLibraryReplicator

{

 internal class SPListOperations 

{

publicstaticstring queryConfigList(SPWeb spWeb, string query)

{

 // Build a query.

 string result = “NotListed”;

 SPQuery spQuery = newSPQuery();

spQuery.Query =  string.Concat(“<Where><Eq>”, “<FieldRef Name=’Source Library Path and Name’/>”,“<Value Type=’Text’>” + query + “</Value>”, “</Eq></Where>”);

spQuery.ViewFields = string.Concat(“<FieldRef Name=’Source Library Path and Name’ />”);

spQuery.ViewFieldsOnly = true;

 // Fetch only the data that we need.

 // Get data from a list.

 string listUrl = spWeb.ServerRelativeUrl + “lists/Replicator Configuration”;

 SPList spList = spWeb.GetList(listUrl);

 // result = list.GetItems(spQuery);

 string sourceList = spWeb.Url + “/” + query;

 foreach (SPListItem spItem in spList.Items)

{

 string a = spItem.GetFormattedValue(“Source Library Path and Name”).ToString();

 if (spItem.GetFormattedValue(“Source Library Path and Name”).ToString() == sourceList)

result = spItem[“Target Library Path and Name”].ToString();

}

 }

 return result;

}

 }

 }

 There you have the final pieces which make up the replicator. This solution does not take checking SPFolders into consideraton, but could easily be modified to accomodate this.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: