Share via


Use Visual Studio to move SharePoint files/folder from one library to another library

Here is the C# code to get this job done:

private static  void Main(string[] args)
 {
     SPSite siteColl = new  SPSite("http://riponkundu/abc/ ; ");
     SPWeb site = siteColl.OpenWeb();
     SPSecurity.RunWithElevatedPrivileges(delegate()
     {
         using (SPSite ElevatedsiteColl = new SPSite(siteColl.ID))
         {
             using (SPWeb web = ElevatedsiteColl.OpenWeb(site.ID))
             {
                 web.AllowUnsafeUpdates = true;
                 string listSrc = "My Documents";
                 string listDest = "My Documents Arc";
                 SPList sourceList = web.Lists.TryGetList(listSrc);
                 SPList targetList = web.Lists.TryGetList(listDest);
                 if (sourceList != null)
                 {
                     SPQuery query = new  SPQuery();
                     query.Query = @"<Where><And><IsNotNull><FieldRef Name='ID' /></IsNotNull><Neq><FieldRef Name='ContentType' /><Value Type='Computed'>Folder</Value>
  </Neq></And></Where><QueryOptions><ViewAttributes Scope='RecursiveAll' /></QueryOptions>";
                     query.ViewAttributes = "Scope='RecursiveAll'";
                     SPListItemCollection coll = sourceList.GetItems(query);
                     for (int i = 0; i < coll.Count; i++)
                     {
                         SPListItem sourceItem = sourceList.GetItemById(Convert.ToInt32(coll[i]["ID"]));
                           
                         string modTime = sourceItem["Modified"].ToString();
                         string folderName = sourceItem.Web.GetFile(sourceItem.Url).ParentFolder.Name;//folder name
                         string folder = sourceItem.Web.GetFile(sourceItem.Url).ParentFolder.ServerRelativeUrl;//folder URL
                         SPFolder sourceFolder = sourceItem.Folder;
                         string targetPath = targetList.RootFolder.ServerRelativeUrl + "/" + folderName;
                         SPFolder targetFolder = web.GetFolder(targetPath);
                         SPListItem targetItem = targetFolder.Item;
  
                         if (Convert.ToDateTime(modTime) <= System.DateTime.Now.AddHours(-4))
                         {
                             #region check for check out item and make in CheckedIn
                             if (sourceItem.FileSystemObjectType == SPFileSystemObjectType.File)
                             {
                                 SPFile sourceFile = sourceItem.File;
                                 if (sourceFile.CheckOutStatus != SPFile.SPCheckOutStatus.None)
                                 {
                                     Console.WriteLine(sourceItem.Name);
                                     sourceFile.UndoCheckOut();
                                 }
                             }
                             #endregion
                             if (web.GetFolder(targetPath).Exists==false)
                             {
                                 SPFolderCollection targetFolderColl = web.Folders;
                                 targetFolderColl.Add(web.Url+"/"+listDest+"/"+folderName);
                                   
                             }
                               
                             SPFolder oFolder = web.GetFolder(listDest);
                             SPFile file = web.Folders[listSrc].SubFolders[folderName].Files[sourceItem.Name.ToString()];
                             file.MoveTo(oFolder.Url + "/"+ folderName +"/" + file.Name, true);
                         }
                     }
                      
                 }
                 web.AllowUnsafeUpdates = false;
             }
         }
     });
 }

See Also