string
siteUrl =
"http://sharepoint-devsite.com/DevSiteA"
;
string
sourceDirectory =
"/DevSiteA/Shared Documents/DevTeam"
;
string
destinationDirectory =
"/DevSiteA/ProjectDocs/ProjTeam"
;
using
(SPSite currSite =
new
SPSite(siteUrl))
{
using
(SPWeb currWeb = currSite.OpenWeb())
{
MoveFolder(currWeb, sourceDirectory, destinationDirectory);
}
}
public
void
MoveFolder(SPWeb sourceWeb,
string
sourceDirectory,
string
destinationDirectory)
{
SPFolder srcFolder = sourceWeb.GetFolder(sourceDirectory);
if
(srcFolder.Exists)
{
SPDocumentLibrary docLib = srcFolder.DocumentLibrary;
SPFolderCollection oFolder = srcFolder.SubFolders;
SPQuery oQuery =
new
SPQuery();
oQuery.Query =
"<Where><Eq><FieldRef Name='FSObjType'/><Value Type='Lookup'>1</Value></Eq></Where>"
;
oQuery.Folder = oFolder.Folder;
oQuery.ViewAttributes =
"Scope='RecursiveAll'"
;
SPListItemCollection collListItems = docLib.GetItems(oQuery);
string
newdestinationFolderPath = destinationDirectory;
MoveSingleFolder(sourceWeb, sourceDirectory, newdestinationFolderPath);
newdestinationFolderPath = destinationDirectory +
"/"
+ srcFolder.Name;
foreach
(SPListItem item
in
collListItems)
{
MoveSingleFolder(sourceWeb, item.Url, newdestinationFolderPath);
}
DeleteSourceFolder(sourceWeb, sourceDirectory);
}
}
private
void
MoveSingleFolder(SPWeb srcWeb,
string
sourceFolderPath,
string
destinationFolderPath)
{
SPFolder srcFolder = srcWeb.GetFolder(sourceFolderPath);
if
(srcFolder.Exists)
{
string
srcFolderName = srcFolder.Name;
SPFolder dstFolder = srcWeb.GetFolder(destinationFolderPath);
if
(dstFolder.Exists)
{
SPFolder newFolder = dstFolder.SubFolders.Add(srcFolderName);
SPListItem newFolderItem = (SPListItem)newFolder.Item;
newFolderItem.ParentList.Fields[SPBuiltInFieldId.Modified].ReadOnlyField =
false
;
newFolderItem.ParentList.Fields[SPBuiltInFieldId.Modified_x0020_By].ReadOnlyField =
false
;
newFolderItem[SPBuiltInFieldId.Modified] = srcFolder.Item[
"Modified"
];
newFolderItem[
"Modified By"
] = srcFolder.Item[
"Modified By"
];
newFolderItem.UpdateOverwriteVersion();
newFolderItem.ParentList.Fields[SPBuiltInFieldId.Modified].ReadOnlyField =
true
;
newFolderItem.ParentList.Fields[SPBuiltInFieldId.Modified_x0020_By].ReadOnlyField =
true
;
}
MoveFolderItems(srcWeb, sourceFolderPath, destinationFolderPath +
"/"
+ srcFolderName);
}
}
private
void
MoveFolderItems(SPWeb sourceWeb,
string
sourceFolderPath,
string
destinationFolderPath)
{
SPFolder srcFolder = sourceWeb.GetFolder(sourceFolderPath);
int
fileCount = srcFolder.Files.Count;
while
(fileCount > 0)
{
SPFile sourceFile = sourceWeb.GetFile(srcFolder.Files[0].Url);
object
modifiedOn = sourceFile.Item[
"Modified"
];
object
modifiedBy = sourceFile.Item[
"Modified By"
];
sourceFile.MoveTo(destinationFolderPath +
"/"
+ sourceFile.Name,
true
);
fileCount--;
SPListItem dstItem = (SPListItem)sourceFile.Item;
dstItem.ParentList.Fields[
"Modified"
].ReadOnlyField =
false
;
dstItem.ParentList.Fields[
"Modified By"
].ReadOnlyField =
false
;
dstItem[
"Modified"
] = modifiedOn;
dstItem[
"Modified By"
] = modifiedBy;
dstItem.UpdateOverwriteVersion();
dstItem.ParentList.Fields[
"Modified"
].ReadOnlyField =
true
;
dstItem.ParentList.Fields[
"Modified By"
].ReadOnlyField =
true
;
}
}
private
void
DeleteSourceFolder(SPWeb srcWeb,
string
sourceFolderPath)
{
SPFolder srcFolder = srcWeb.GetFolder(sourceFolderPath);
if
(srcFolder.Exists)
{
if
(srcFolder.Files.Count == 0)
{
SPFolder srcRootFolder = srcFolder.ParentFolder;
srcRootFolder.SubFolders.Delete(srcFolder.Url);
}
}
}