Move Files of SharePoint Document Library using Object Model

Move Files of SharePoint Document Library using Object Model
2 votes, 5.00 avg. rating (96% score)


In this post we will see how to move files of SharePoint document library to one location to another using object model.
We will also see how to keep same file Version, Modified and ModifiedBy values after moving
The post includes

  • Generic method to move file to destination
  • Keeping the file version, Modified and Modified By without any change

Skill Level – Medium

Move file

	string siteUrl = "";
	string fileName = "ProjectPlan.docx";
	string sourceDirectory = "/DevSite/Shared Documents";
	string destinationDirectory  = "/DevSite/ProjectDocs/ProjTeam/DevTeam/TechLead";
	using (SPSite currSite = new SPSite(siteUrl))
		using (SPWeb currWeb = currSite.OpenWeb())
			MoveFile(currWeb, destinationDirectory, sourceDirectory, fileName);

	public void MoveFile(SPWeb sourceWeb, string sourceDirectory, string destinationDirectory, string fileName)
		SPFile sourcefile = sourceWeb.GetFile(sourceDirectory + "/" + fileName);
		if (sourcefile.Exists)
			object modifiedOn = sourcefile.Item["Modified"];
			object modifiedBy = sourcefile.Item["Modified By"];
			//true - replace if file exists
			sourcefile.MoveTo(destinationDirectory + "/" + fileName, true);   

			SPFile dstFile = sourceWeb.GetFile(destinationDirectory + "/" + fileName);
			SPListItem dstItem = (SPListItem)dstFile.Item;
			dstItem.ParentList.Fields["Modified"].ReadOnlyField = false;
			dstItem.ParentList.Fields["Modified By"].ReadOnlyField = false;
			dstItem["Modified"] = modifiedOn;
			dstItem["Modified By"] = modifiedBy;
			//updates the item without creating another version of the item
			dstItem.ParentList.Fields["Modified"].ReadOnlyField = true;
			dstItem.ParentList.Fields["Modified By"].ReadOnlyField = true;

MoveFile is the method which moves the file to destination without changing ‘Modified’ and ‘Modified By’ values.


Moving files from UI can be done through ‘Content and structure’ under SiteAdministration (siteurl/_Layouts/sitemanager.aspx will give us Content and structure).
The above code for Moving files will be working only if source and destination are in same site collection. Content and structure from UI also does not provide this option. If destination is located in other site collection or web application, there is no move option. The file should be created in the destination and delete from source to resemble move operation. Also version should be kept in mind.


If we have multiple files and multiple locations and we tend to write code rather than doing from Content and structure. Hope this code helps when required.

August 9, 2013 В· Adi В· No Comments
Tags: , , , , ,  В· Posted in: C#, Sharepoint 2007, Sharepoint 2010, SharePoint 2013

Leave a Reply

What is 12 + 9 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)