How To Bulk Move Documents from DocsOnline (SharePoint 2003) to Exchange 2.0 (SharePoint 2007)
Overview
- Create an Excel worksheet with the following columns and populate:
- Path (where coming from)
- File (document name)
- Library (where going to)
- Tags (Meta-data about the document)
- Set up your new Document Libraries to mirror the new structure you intend.
- Ensure that you have disabled Check-Out for each document library.
- Create a Console app.
- Set a Web Reference to SharePoint Copy Web Service (http://intranet/{new site}/_vti_bin/copy.asmx)
- Read the documents via System.IO.File.
- Write the documents via Copy Web Service.
Code Listings
Read Excel
static DataTable ReadExcel()
{
string fileName = @"D:\Downloads\DocsOnlineContents.xlsx";
StringBuilder connectionString = new StringBuilder();
string quote = @"""";
connectionString.Append("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=");
connectionString.Append(fileName);
connectionString.Append(";Extended Properties=");
connectionString.Append(quote);
connectionString.Append("Excel 8.0;HDR=YES");
connectionString.Append(quote);
OleDbDataAdapter da;
DataSet ds = new DataSet();
da = new OleDbDataAdapter("SELECT * FROM [RawData$]", connectionString.ToString());
da.Fill(ds);
return ds.Tables[0];
}
Main Program
static void Main(string[] args)
{
string source = @"file://docsonline6/sites/{old site}/";
string destin = @"http://intranet/{new site}/";
bool retVal = true;
DataTable dt = ReadExcel();
foreach (DataRow dr in dt.Rows)
{
string path = dr["Path"].ToString();
string file = dr["File"].ToString();
string library = dr["Library"].ToString();
string docType = dr["Document Type"].ToString();
string subType = dr["Sub-Type"].ToString();
string appInst = dr["App Instance"].ToString();
string created = dr["Created"].ToString();
DateTime dtCreated = DateTime.Parse(created);
string author = dr["Author"].ToString();
string modifiedBy = dr["Modified By"].ToString();
string modified = dr["Modified"].ToString();
DateTime dtModified = DateTime.Parse(modified);
path = path.Replace("\\", "/");
if(library != "DELETE")
{
Console.Write("Copying file: {0}...", file);
retVal = ExchangeService.CopyFile(
source + path + file,
destin + library + @"/" + file,
file,
docType,
subType,
appInst,
dtCreated,
author,
modifiedBy,
dtModified);
}
if (retVal)
Console.WriteLine("success!");
else
{
Console.WriteLine("failed!");
break;
}
}
Console.WriteLine("Press any key to continue.");
Console.ReadLine();
}
Copy File
public static bool CopyFile(
string copyUrl,
string copyDest,
string file,
string docType,
string subType,
string appInst,
DateTime created,
string author,
string modifiedBy,
DateTime modified
)
{
//remove after debug
Log.WriteEntry("COPYURLFINAL: " + copyUrl + "/n" + "COPYDEST: " + copyDest);
bool IsCopied = false;
//copyUrl = Uri.EscapeUriString(copyUrl);
copyDest = Uri.EscapeUriString(copyDest);
try
{
Log.WriteEntry("INSIDE TRY");
//instantiating copy web service
SharepointCopyWebService.Copy copyService = new SharepointCopyWebService.Copy();
copyService.Credentials = CredentialCache.DefaultNetworkCredentials;
copyService.Url = "http://intranet/{new site}/_vti_bin/copy.asmx";
Log.WriteEntry("BEFORE CREDENTIAL");
Log.WriteEntry("AFTER CREDENTIAL USER: " + CredentialCache.DefaultNetworkCredentials.UserName);
//string array
string[] dest = { copyDest };
//gets the copysource in bytes
byte[] fileBytesArray;
//to receive the result xml
SharepointCopyWebService.CopyResult[] copyResultArray;
uint copyUnit;
Log.WriteEntry("IN if (copyUrl.Contains...");
//copy file
SharepointCopyWebService.FieldInformation[] info = GetFieldInfo(file, docType, subType, appInst, created, author, modifiedBy, modified);
Log.WriteEntry("AFTER FieldInfo.");
Log.WriteEntry("Identity: " + WindowsIdentity.GetCurrent().Name);
fileBytesArray = File.ReadAllBytes(new Uri(copyUrl).LocalPath);
Log.WriteEntry("AFTER fileBystesArray.Length: " + fileBytesArray.Length);
if (fileBytesArray == null)
{
Log.WriteEntry("THROWING Exception");
throw new Exception("Cannot read file.");
}
Log.WriteEntry("PRINT OUT BEFORE EXCEPTION");
copyUnit = copyService.CopyIntoItems(NULL_URI, dest, info, fileBytesArray, out copyResultArray);
Log.WriteEntry("AFTER COPYINTOITEMS for FILE. dest: " + dest + ", count of fileBytesArray: " + fileBytesArray.Length +
", count of copyResultArray: " + copyResultArray.Length);
if (copyUnit == 0)
{
Log.WriteEntry("IN copyUnit == 0 condition.");
int idx = 0;
foreach (SharepointCopyWebService.CopyResult myCopyResult in copyResultArray)
{
if (copyResultArray[idx].ErrorMessage == null)
{
IsCopied = true;
}
else
{
try
{
throw new Exception("Copy Operation failed.\r\n" + copyResultArray[idx].ErrorMessage);
}
catch (Exception ex)
{
Log.WriteEntry(ex.ToString());
Log.WriteEntry("INTO COPYFILE. EXCEPTION FOUND: " + ex.Message);
}
}
idx++;
}
}
}
catch (System.Web.Services.Protocols.SoapException ex)
{
Log.WriteEntry("INTO COPYFILE System.Web.Services.Protocols. SoapException FOUND: " + ex.Message);
}
catch (Exception ex)
{
Log.WriteEntry("INTO COPYFILE. Exception FOUND: " + ex.Message);
}
return IsCopied;
}
Constructor
static ExchangeService()
{
if (!System.Diagnostics.EventLog.SourceExists("MoveDocsOnline"))
System.Diagnostics.EventLog.CreateEventSource("MoveDocsOnline", "Application");
Log.Source = "MoveDocsOnline";
Log.WriteEntry("This is a simple event log entry");
}
Set up Meta-Data
private static SharepointCopyWebService.FieldInformation[] GetFieldInfo(
string file,
string docType,
string subType,
string appInst,
DateTime created,
string author,
string modifiedBy,
DateTime modified)
{
List<SharepointCopyWebService.FieldInformation> info = new List<FieldInformation>();
info.Add(new SharepointCopyWebService.FieldInformation
{
DisplayName = "Name",
Type = SharepointCopyWebService.FieldType.Text,
Value = file
});
info.Add(new SharepointCopyWebService.FieldInformation
{
DisplayName = "Title",
Type = SharepointCopyWebService.FieldType.Text,
Value = file
});
info.Add(new SharepointCopyWebService.FieldInformation
{
DisplayName = "Content Type",
Type = SharepointCopyWebService.FieldType.Text,
Value = "Default Document"
});
info.Add(new SharepointCopyWebService.FieldInformation
{
DisplayName = "Security Classification",
Type = SharepointCopyWebService.FieldType.Choice,
Value = "Internal"
});
info.Add(new SharepointCopyWebService.FieldInformation
{
DisplayName = "Document Type",
Type = SharepointCopyWebService.FieldType.Choice,
Value = docType
});
if (subType != "")
info.Add(new SharepointCopyWebService.FieldInformation
{
DisplayName = "Sub-Type",
Type = SharepointCopyWebService.FieldType.Choice,
Value = subType
});
if (appInst != "")
{
appInst = appInst.Replace(", ", ";#");
info.Add(new SharepointCopyWebService.FieldInformation
{
DisplayName = "App Instance",
Type = SharepointCopyWebService.FieldType.MultiChoice,
Value = appInst
});
}
info.Add(new SharepointCopyWebService.FieldInformation
{
DisplayName = "Created",
Type = SharepointCopyWebService.FieldType.DateTime,
Value = created.ToString("yyyy-MM-ddThh:mm:ssZ")
});
if (author != "")
info.Add(new SharepointCopyWebService.FieldInformation
{
DisplayName = "Content Owner",
Type = SharepointCopyWebService.FieldType.Text,
Value = author
});
info.Add(new SharepointCopyWebService.FieldInformation
{
DisplayName = "Modified By",
Type = SharepointCopyWebService.FieldType.Text,
Value = modifiedBy
});
info.Add(new SharepointCopyWebService.FieldInformation
{
DisplayName = "Modified",
Type = SharepointCopyWebService.FieldType.DateTime,
Value = modified.ToString("yyyy-MM-ddThh:mm:ssZ")
});
return info.ToArray();
}