.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Code Snippets >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Move file from one document Library to another

Posted By:volvo       Posted Date: November 28, 2013    Points: 25    Category: SharePoint    URL: http://www.dotnetspark.com  

Code snnipets explain that how to move file from one document Library to another in share server object model

string siteUrl = "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;


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend