Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Programmatically copy files from a doclib to another doclib

0 views
Skip to first unread message

Ole Kristian

unread,
Mar 3, 2005, 3:03:05 AM3/3/05
to
I have a problem with copying (programatically) all the files from a site's
DocumentLibrary to another DocLib on another Site, including files in
subfolders. I also want to keep version history and metadata. The part I am
struggling with is the part that copy the subfolders. The part where I try
to copy the metadata is also not working. My code for copying the
subfolders has a problem when trying to get the current SPFolder from the
SPFolderCollection of destination folders (I'm trying to index this). The
code below is pasted from VS.NET, and probably looks better if pasted back
there. If anyone has experience with this type of problem, please let me know.
/// <summary>
/// Metod for copying the documents in a source-site to a destination-site
/// </summary>
/// <param name="srcSite">The source-site</param>
/// <param name="destSite">The destination-site</param>
/// <param name="DocumentsLib">The documentlibrary-urlpath</param>
private void copyDocuments( SPSite srcSite, SPSite destSite, string
DocumentsLib )
{
const string STR_CHECKIN = "CheckIN";
const string STR_CHECKIN_PURPOSE = "File was checked out, had to check
file in and out again for upgrading purposes ";


//a collection of the destination sub-folders
SPFolderCollection destSubFolderColl =
destSite.RootWeb.Folders[DocumentsLib].SubFolders;

//looping trough all the folders
foreach( SPFolder srcSubFolder in
srcSite.RootWeb.Folders[DocumentsLib].SubFolders )
{
if(!srcSubFolder.Name.Equals("Forms"))
{
//Getting the destinating folder for the current srcfolder. This
generates an exception
SPFolder destSubFolder = destSubFolderColl[srcSubFolder.Url];

//collection of files in the overlying folder
SPFileCollection srcSubFileColl = srcSubFolder.Files;
SPFileCollection destSubFileColl = destSubFolder.Files;

//loop trough all the files in the current filecollection
foreach( SPFile srcSubFile in srcSubFileColl)
{
//destination url
string destSubURL = srcSubFolder.Url + "/" + srcSubFile.Name;

//getting all the versions of the files
SPFileVersionCollection srcSubVersions = srcSubFile.Versions;

//looping trough all the versions of the file and adding versions to
filecollection
foreach(SPFileVersion srcSubVersion in srcSubVersions)
{
byte[] srcSubBinVersion = srcSubVersion.OpenBinary();
destSubFileColl.Add(destSubURL, srcSubBinVersion,
srcSubVersion.CreatedBy, srcSubVersion.CreatedBy, srcSubVersion.Created,
srcSubVersion.Created );
}
//open binary stream
byte[] srcSubBin = srcSubFile.OpenBinary();

//adding file to filecollection
SPFile destSubFile = destSubFileColl.Add(destSubURL, srcSubBin,
srcSubFile.ModifiedBy, srcSubFile.ModifiedBy, srcSubFile.TimeCreated,
srcSubFile.TimeLastModified);

//copy metadata by checking out and copying attributes and then
checking in the file.

destSubFile.CheckOut();
//Getting a hashtable with the metadata from the source
Hashtable hash = srcSubFile.Properties;
ICollection keys = hash.Keys;

//removing all possible metadata from the destination-file
destSubFile.Properties.Clear();
//looping through all the keys in the hashtable
foreach(object key in keys)
{
//copying the metadata
destSubFile.Properties.Add(key, hash[key].ToString());
}

destSubFile.CheckIn(STR_CHECKIN);
//deleting the initial file that was created without attributes
//destSubFile.Versions.Delete(destSubFile.Versions.Count -1);

}//foreach

}
}//foreach
}//if
}//metod-end

0 new messages