locked
move documents from one web application to another SharePoint c# RRS feed

  • Question

  • Guys, i want to move SP 2010 Web Application document library files to SP 2013 web application document library on cross machines (on different servers not on same). i have written windows application as follow

    private void MoveDocuments(string sourceWebUrl, string sourceDocLib, string destWebUrl, string destDocLib)
            {
                try
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                        {
                            using (SPSite sourceSite = new SPSite(txtSp2010Url.Text))
                            {
                                using (SPWeb sourceWeb2010 = sourceSite.OpenWeb())
                                {
                                    sourceWeb2010.AllowUnsafeUpdates = true;
    
                                    SPDocumentLibrary sourceDocumentLibrary = (SPDocumentLibrary)sourceWeb2010.Lists[sourceDocLib];
                                    SPFileCollection sourceFiles = sourceDocumentLibrary.RootFolder.Files;
    
                                    using (SPSite destSite = new SPSite(txtSp2013Url.Text))
                                    {
                                        using (SPWeb destWeb2013 = destSite.OpenWeb())
                                        {
                                            destWeb2013.AllowUnsafeUpdates = true;
    
                                            SPDocumentLibrary destDocumentLibrary = (SPDocumentLibrary)destWeb2013.Lists[destDocLib];
    
                                            foreach (SPFile sourceFile in sourceFiles)
                                            {
                                                sourceFile.MoveTo(destDocumentLibrary.ParentWeb.Url + "/" + destDocumentLibrary + "/" + sourceFile.Name, true);
                                            }
                                            destWeb2013.AllowUnsafeUpdates = false;
                                            lblStatus.Text = "Move successfully.";
                                        }
                                    }
                                    sourceWeb2010.AllowUnsafeUpdates = false;
                                }
                            }
                        });
                }
                catch (Exception ex)
                {
                    lblStatus.Text = ex.ToString();
                }
    }

    but not working it gives an error "Value does not fall within the expected range."  on sourceFile.MoveTo(destDocumentLibrary.ParentWeb.Url + "/" + destDocumentLibrary + "/" + sourceFile.Name, true);

    how could i do this ?

    Thanks

    Nasir


    • Edited by Hamad Nasir Tuesday, October 8, 2013 7:11 AM
    Wednesday, October 2, 2013 1:08 PM

Answers

  • My aim was to move document library files to another on different machines (these two apps are on different servers not on same server). 

    according to my R&D, it is not possible with the Server Object Model we can move documents on different sharepoint server apps by using Client Object Model. 

    below code for server object Model (Copy documents from one SharePoint App to another on same server)

            private void MoveDocumentsServerObjectModel(string sourceWebUrl, string sourceDocLib, string destWebUrl, string destDocLib)
            {
                try
                {
                    using (SPSite sourceSite = new SPSite(txtSp2010Url.Text))
                    {
                        using (SPWeb sourceWeb2010 = sourceSite.OpenWeb())
                        {
                            sourceWeb2010.AllowUnsafeUpdates = true;
    
                            SPDocumentLibrary sourceDocumentLibrary = (SPDocumentLibrary)sourceWeb2010.Lists[sourceDocLib];
                            SPFileCollection sourceFiles = sourceDocumentLibrary.RootFolder.Files;
    
                            using (SPSite destSite = new SPSite(txtSp2013Url.Text))
                            {
                                using (SPWeb destWeb2013 = destSite.OpenWeb())
                                {
                                    destWeb2013.AllowUnsafeUpdates = true;
    
                                    SPDocumentLibrary destDocumentLibrary = (SPDocumentLibrary)destWeb2013.Lists[destDocLib];
    
                                    foreach (SPFile sourceFile in sourceFiles)
                                    {
                                        destDocumentLibrary.RootFolder.Files.Add(destWeb2013.Url + "/" + destDocumentLibrary + "/" + sourceFile.Name, (Stream)sourceFile.OpenBinaryStream(), true);
                                        //sourceFile.CopyTo(destWeb2013.Url + "/" + destDocumentLibrary + "/" + sourceFile.Name, true);
                                    }
                                    destWeb2013.AllowUnsafeUpdates = false;
                                    lblStatus.Text = "Move successfully.";
                                }
                            }
                            sourceWeb2010.AllowUnsafeUpdates = false;
                        }
                    }
                }
                catch (Exception ex)
                {
                    lblStatus.Text = ex.ToString();
                }
            }
    below code for Client object Model (Copy documents from one SharePoint App to another on different servers)

    private void CopyDocuments(string srcUrl, string srcLibrary, string destUrl, string destLibrary) { // set up the src client ClientContext srcContext = new ClientContext(srcUrl); // set up the destination context ClientContext destContext = new ClientContext(destUrl); // get the source list and items Web srcWeb = srcContext.Web; List srcList = srcWeb.Lists.GetByTitle(srcLibrary); ListItemCollection itemColl = srcList.GetItems(new CamlQuery()); srcContext.Load(itemColl); srcContext.ExecuteQuery(); // get the destination list Web destWeb = destContext.Web; destContext.Load(destWeb); destContext.ExecuteQuery(); foreach (var doc in itemColl) { try { //if (doc.FileSystemObjectType == FileSystemObjectType.File) //Field or Property "FileAttachement not found." //{ // get the file File file = doc.File; srcContext.Load(file); srcContext.ExecuteQuery(); // build destination url string nLocation = destWeb.ServerRelativeUrl.TrimEnd('/') + "/" + destLibrary.Replace(" ", "") + "/" + file.Name; // read the file, copy the content to new file at new location FileInformation fileInfo = File.OpenBinaryDirect(srcContext, file.ServerRelativeUrl); File.SaveBinaryDirect(destContext, nLocation, fileInfo.Stream, true); // } } catch (Exception ex) { } }

    }

    http://blog.blumshapiro.com/2012/08/sharepoint-2010-using-the-client-object-model-to-move-files-and-folders-across-site-collections-and-subsites/

    Regards,

    Nasir


    • Marked as answer by Hamad Nasir Tuesday, October 8, 2013 7:17 AM
    • Edited by Hamad Nasir Tuesday, October 8, 2013 7:18 AM
    Tuesday, October 8, 2013 7:17 AM

All replies

  • Hi Hamad Nasir,

    From your description, it seems that both SharePoint 2010 and 2013 are in the same server. I don’t think SPFile.MoveTo works in this situation.

    It works well in the same site, as to your scenario, please try to add the file to the destination library, if you want to delete the file in source library, just call the delete method after that.

    Thanks,


    Qiao Wei
    TechNet Community Support

    • Proposed as answer by Tarek Yehia Thursday, October 3, 2013 11:13 AM
    Thursday, October 3, 2013 9:14 AM
  • My aim was to move document library files to another on different machines (these two apps are on different servers not on same server). 

    according to my R&D, it is not possible with the Server Object Model we can move documents on different sharepoint server apps by using Client Object Model. 

    below code for server object Model (Copy documents from one SharePoint App to another on same server)

            private void MoveDocumentsServerObjectModel(string sourceWebUrl, string sourceDocLib, string destWebUrl, string destDocLib)
            {
                try
                {
                    using (SPSite sourceSite = new SPSite(txtSp2010Url.Text))
                    {
                        using (SPWeb sourceWeb2010 = sourceSite.OpenWeb())
                        {
                            sourceWeb2010.AllowUnsafeUpdates = true;
    
                            SPDocumentLibrary sourceDocumentLibrary = (SPDocumentLibrary)sourceWeb2010.Lists[sourceDocLib];
                            SPFileCollection sourceFiles = sourceDocumentLibrary.RootFolder.Files;
    
                            using (SPSite destSite = new SPSite(txtSp2013Url.Text))
                            {
                                using (SPWeb destWeb2013 = destSite.OpenWeb())
                                {
                                    destWeb2013.AllowUnsafeUpdates = true;
    
                                    SPDocumentLibrary destDocumentLibrary = (SPDocumentLibrary)destWeb2013.Lists[destDocLib];
    
                                    foreach (SPFile sourceFile in sourceFiles)
                                    {
                                        destDocumentLibrary.RootFolder.Files.Add(destWeb2013.Url + "/" + destDocumentLibrary + "/" + sourceFile.Name, (Stream)sourceFile.OpenBinaryStream(), true);
                                        //sourceFile.CopyTo(destWeb2013.Url + "/" + destDocumentLibrary + "/" + sourceFile.Name, true);
                                    }
                                    destWeb2013.AllowUnsafeUpdates = false;
                                    lblStatus.Text = "Move successfully.";
                                }
                            }
                            sourceWeb2010.AllowUnsafeUpdates = false;
                        }
                    }
                }
                catch (Exception ex)
                {
                    lblStatus.Text = ex.ToString();
                }
            }
    below code for Client object Model (Copy documents from one SharePoint App to another on different servers)

    private void CopyDocuments(string srcUrl, string srcLibrary, string destUrl, string destLibrary) { // set up the src client ClientContext srcContext = new ClientContext(srcUrl); // set up the destination context ClientContext destContext = new ClientContext(destUrl); // get the source list and items Web srcWeb = srcContext.Web; List srcList = srcWeb.Lists.GetByTitle(srcLibrary); ListItemCollection itemColl = srcList.GetItems(new CamlQuery()); srcContext.Load(itemColl); srcContext.ExecuteQuery(); // get the destination list Web destWeb = destContext.Web; destContext.Load(destWeb); destContext.ExecuteQuery(); foreach (var doc in itemColl) { try { //if (doc.FileSystemObjectType == FileSystemObjectType.File) //Field or Property "FileAttachement not found." //{ // get the file File file = doc.File; srcContext.Load(file); srcContext.ExecuteQuery(); // build destination url string nLocation = destWeb.ServerRelativeUrl.TrimEnd('/') + "/" + destLibrary.Replace(" ", "") + "/" + file.Name; // read the file, copy the content to new file at new location FileInformation fileInfo = File.OpenBinaryDirect(srcContext, file.ServerRelativeUrl); File.SaveBinaryDirect(destContext, nLocation, fileInfo.Stream, true); // } } catch (Exception ex) { } }

    }

    http://blog.blumshapiro.com/2012/08/sharepoint-2010-using-the-client-object-model-to-move-files-and-folders-across-site-collections-and-subsites/

    Regards,

    Nasir


    • Marked as answer by Hamad Nasir Tuesday, October 8, 2013 7:17 AM
    • Edited by Hamad Nasir Tuesday, October 8, 2013 7:18 AM
    Tuesday, October 8, 2013 7:17 AM