satish babu

satish babu

  • 1.1k
  • 633
  • 85.4k

moving the doucuments from one document library to another

May 14 2014 9:11 AM
Hi,
 
i want to move the documents from one document library to another document library along with folders and fields. I written some code for moving them but i only file is copied not the all the fields. Can any one help me.
I am writing the below code
 try
{
SPSite objactualsite = new SPSite("http://sitename");
SPWeb objactualweb = objactualsite.OpenWeb();
SPList objactuallist = objactualweb.Lists.TryGetList("listname");
if (objactuallist != null)
{
SPView objactualviewcoll = objactuallist.Views["All Documents"];
SPViewFieldCollection fldcoll = objactualviewcoll.ViewFields;
SPSite objdestsite = new SPSite("http://sitename/testsite");
SPWeb objdestweb = objdestsite.OpenWeb();
SPList objdestlist = objdestweb.Lists.TryGetList("listname");
if (objdestlist == null)
{
objdestweb.Lists.Add("listname", "same as previous list", SPListTemplateType.DocumentLibrary);
}
else
{
SPQuery query = new SPQuery();
query.Query = "<View/>";
SPListItemCollection items = objactuallist.GetItems(query);
SPFileCollection flcoll = null;
SPDocumentLibrary _mydoclib = null;
string ss = "http://sitename/testsite/listname/";
for (int i = 1; i < items.Count; i++)
{
if (items[i].Folder != null)
{

ss = ss + items[i].Folder.Name;
_mydoclib = (SPDocumentLibrary)objdestweb.Lists["listname"];
SPFolderCollection _myfolders = objdestweb.Folders;
_myfolders.Add("http://sitename/testsite/listname/" + items[i].Folder.Name + "/");
objdestlist.Update();
SPFolder usefolder = objactuallist.RootFolder.SubFolders[items[i].Folder.Name];
for (int k = 0; k < usefolder.Files.Count; k++)
{
SPFile item11 = usefolder.Files[k];
byte[] filebytes1 = item11.OpenBinary();
string desturl1 = ss + "/" + item11.Name;
SPFile destfile1 = objdestlist.RootFolder.Files.Add(desturl1, filebytes1, true);
SPListItem itemss = destfile1.Item;
itemss[SPBuiltInFieldId.Modified] = Convert.ToDateTime(usefolder.Files[k].Item[SPBuiltInFieldId.Modified]);
destfile1.Item.Update();
}
}
else
{


ss = "http://sitename/testsite/doctrack/";

SPListItem item111 = objactuallist.Items[i];
byte[] filebytes11 = item111.File.OpenBinary();
//string desturl1 = objdestlist.RootFolder.Url + "/" + item11.File.Name;
string desturl11 = ss + item111.File.Name;
SPFile destfile11 = objdestlist.RootFolder.Files.Add(desturl11, filebytes11,objactualsite.RootWeb.CurrentUser,objactualsite.RootWeb.CurrentUser,Convert.ToDateTime(item111[SPBuiltInFieldId.Created]),Convert.ToDateTime(item111[SPBuiltInFieldId.Modified]) );



SPListItem itemss1 = destfile11.Item;
object obj = items[i].Fields[SPBuiltInFieldId.Modified];
MessageBox .Show(item111["Reference"]+"") ;
itemss1["Reference"] = item111["Reference"] ;

}

}
}
}
}
catch (Exception ex)
{

 

Answers (1)