DocMaster - SharePoint Document Library Sync Tool

DocMaster is a handy tool to sync SharePoint Document Libraries Between Sites/Servers. I have used SharePoint Copy Web Service to perform this task. So, it will work on any PC with .NET Framework 2.0 installed. But, you may have to configure the config file according to your SharePoint resources.
 
Config File Settings:
 
<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<configSections>

<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >

<section name="DocMaster.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

</sectionGroup>

</configSections>

<appSettings>

<add key="SourceURL" value="http://dcftsd1" />

<add key="SourceLibrary" value="Shared Documents" />

<add key="DestinationURL" value="http://dcftsd1" />

<add key="DestinationLibrary" value="Policy" />

</appSettings>

<applicationSettings>

<DocMaster.Properties.Settings>

<setting name="DocMaster_SourceList_Lists" serializeAs="String">

<value>http://dcftsd1/_vti_bin/lists.asmx</value>

</setting>

<setting name="DocMaster_DestList_Lists" serializeAs="String">

<value>http://dcftsd1/_vti_bin/lists.asmx</value>

</setting>

<setting name="DocMaster_CopyService_Copy" serializeAs="String">

<value>http://dcftsd1/_vti_bin/copy.asmx</value>

</setting>

</DocMaster.Properties.Settings>

</applicationSettings>

</configuration>

There are basically 2 Main Functions:

private void uploadFiles()

{

try

{

this.listBox1.Items.Add("===============================================");

this.listBox1.Items.Add("Uploading Document Library: " + sourceList);

this.listBox1.Items.Add("===============================================");

this.listBox1.Refresh();

using (SourceList.Lists sourceListService = new SourceList.Lists())

{

using (DestList.Lists destListService = new DestList.Lists())

{

sourceListService.PreAuthenticate = false;

sourceListService.Credentials = System.Net.CredentialCache.DefaultCredentials;

destListService.PreAuthenticate = false;

destListService.Credentials = System.Net.CredentialCache.DefaultCredentials;

XmlDocument xmlDoc = new System.Xml.XmlDocument();

XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");

XmlNode ndViewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");

XmlNode ndQueryOptions = xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "");

try

{

ndQueryOptions.InnerXml = "<IncludeAttachmentUrls>TRUE</IncludeAttachmentUrls>";

ndViewFields.InnerXml = "";

ndQuery.InnerXml = "";

XmlNode sourceItemData = sourceListService.GetListItems(sourceList, null, ndQuery, ndViewFields, null, ndQueryOptions, null);

XmlNodeList oNodes = sourceItemData.ChildNodes;

foreach (XmlNode node in oNodes)

{

XmlNodeReader objReader = new XmlNodeReader(node);

while (objReader.Read())

{

if (objReader["ows_EncodedAbsUrl"] != null && objReader["ows_LinkFilename"] != null)

{

string strURL = objReader["ows_EncodedAbsUrl"].ToString();

string strFileName = objReader["ows_LinkFilename"].ToString();

this.listBox1.Items.Add("Uploading File: " + strFileName);

this.listBox1.Refresh();

string destFilePath = destURL + "/" + destList + "/" + strFileName;

UploadAttachment(strURL, destFilePath);

}

}

}

}

catch (SoapException ex)

{

this.listBox1.Items.Add("Message:\n" + ex.Message + "\nDetail:\n" + ex.Detail.InnerText + "\nStackTrace:\n" + ex.StackTrace);

this.listBox1.Refresh();

}

}

}

}

catch (Exception ex)

{

using (System.IO.StreamWriter str = new System.IO.StreamWriter("ErrorLog_" + System.DateTime.Now.ToString().Replace(":", "_").Replace(@"/", "_") + ".log"))

{

str.WriteLine("Message:->\n" + ex.Message + "\nStackTrace:->\n" + ex.StackTrace, "CruiseCradle");

}

this.listBox1.Items.Add("Message:->\n" + ex.Message + "\nStackTrace:->\n" + ex.StackTrace);

this.listBox1.Refresh();

}

}

private void UploadAttachment(string copySource, string destFilePath)

{

HttpWebRequest request;

HttpWebResponse response = null;

try

{

using (CopyService.Copy myCopyService = new CopyService.Copy())

{

string[] copyDest = { destFilePath };

myCopyService.Credentials = System.Net.CredentialCache.DefaultCredentials;

CopyService.FieldInformation myFieldInfo = new

CopyService.FieldInformation();

CopyService.FieldInformation[] myFieldInfoArray = { myFieldInfo };

byte[] myByteArray;

uint myGetUint = myCopyService.GetItem(copySource, out myFieldInfoArray, out myByteArray);

CopyService.CopyResult myCopyResult1 = new CopyService.CopyResult();

CopyService.CopyResult myCopyResult2 = new CopyService.CopyResult();

CopyService.CopyResult[] myCopyResultArray = { myCopyResult1, myCopyResult2 };

try

{

uint myCopyUint = myCopyService.CopyIntoItems(copySource, copyDest, myFieldInfoArray, myByteArray, out myCopyResultArray);

if (myCopyUint == 0)

{

int idx = 0;

foreach (CopyService.CopyResult myCopyResult in myCopyResultArray)

{

string opString = (idx + 1).ToString();

if (myCopyResultArray[idx].ErrorMessage == null)

{

this.listBox1.Items.Add("Copy operation completed.\r\n" + "Destination: " +

myCopyResultArray[idx].DestinationUrl);

this.listBox1.Refresh();

}

else

{

this.listBox1.Items.Add("Copy operation failed.\r\n" + "Error: " +

myCopyResultArray[idx].ErrorMessage + "\r\n" +

"Code: " + myCopyResultArray[idx].ErrorCode);

this.listBox1.Refresh();

}

idx++;

}

}

}

catch (Exception exc)

{

int idx = 0;

foreach (CopyService.CopyResult myCopyResult in myCopyResultArray)

{

idx++;

if (myCopyResult.DestinationUrl == null)

{

string idxString = idx.ToString();

this.listBox1.Items.Add("Copy operation failed.\r\n" + "Description: " + exc.Message);

this.listBox1.Refresh();

}

}

}

}

}

catch (Exception ex)

{

this.listBox1.Items.Add(ex.Message);

}

}

Download Link: http://docmaster.codeplex.com/

Comments

Post a Comment

Popular posts from this blog

How to Create SSRS Report on SharePoint List

Database Testing With DBUnit

SAP RFC Integration through BizTalk