1

I am new to SharePoint.

I have given the flow below.

• I have a button in visual web part called Archive.

• On Click of the button I like to move the folder inside the site(A) to another site(B).

• I used this code to move a folder from a site to another site.

I am getting an error:

"Folder 'Archive' not exist".

What is the reason for this issue?

protected void btnArchive_Click(object sender, EventArgs e)
    {
        try
        {
            SPSecurity.RunWithElevatedPrivileges(delegate
            {

                SPWeb spWeb = SPContext.Current.Web;
                spWeb.AllowUnsafeUpdates = true;


                string strURl = @"http://server/site/Shared%20Documents/Copies";

                SPFolder spFolder = spWeb.GetFolder(strURl);


                string sDestURL = @"http://server/site/archive/Shared%20Documents/";
                    using (SPSite site = new SPSite(sDestURL))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            web.AllowUnsafeUpdates = true;
                            string sSiteName = spWeb.Name;

                            SPFolder subSiteFolder = web.GetFolder(sDestURL + sSiteName);

                            if (!subSiteFolder.Exists)
                            {

                                subSiteFolder = web.Folders.Add(sSiteName);
                                web.Update();

                            }


                            spFolder.CopyTo(sDestURL);

                        }

                    }

            });
        }
        catch (Exception Ex)
        {
            lblMesg.Text = "Error:" + Ex.Message;
        }
    }
  • 6
    So? What is your question? – Akhoy Oct 7 '15 at 4:52
  • Hi Akhoy, Apologise for not detailing the issue. I posted the issue which i am getting. Please let me know if you want any input. Thanks in advance. – Ganee Oct 7 '15 at 7:39
  • Like the error message says: There is no Folder 'Archive' in the Shared Documents Lib. Are you sure, you need web.GetFolder(sDestURL + sSiteName)? I think "+ sSiteName" is to much – ReTech Oct 7 '15 at 8:18
0

Side note: You need to initialize also the source web inside Elevated Privileges, otherwise it'll use current user privileges.

SPSecurity.RunWithElevatedPrivileges(delegate
{
    using (SPSite site = new SPSite(strURl))
    {
        using (SPWeb web = site.OpenWeb())
        {         

However, this should cover it pretty much.

private static void RecursiveCopy(SPList objSourceList, SPFolder objSourceFolder, SPFolder objDestinationFolder)
    {
        SPListItemCollection objItems = ((SPDocumentLibrary)objSourceList).GetItemsInFolder(objSourceList.DefaultView, objSourceFolder);

        foreach (SPListItem objItem in objItems)
        {
            //If it's a file copy it.
            if (objItem.FileSystemObjectType == SPFileSystemObjectType.File)
            {

                byte[] fileBytes = objItem.File.OpenBinary();
                string DestinationURL = string.Format(@"{0}/{1}", objDestinationFolder.Url, objItem.File.Name);

                //Copy the file.
                SPFile objDestinationFile = objDestinationFolder.Files.Add(DestinationURL, fileBytes, true);
                objDestinationFile.Update();
            }
            else
            {
                string dirURL = string.Format(@"{0}/{1}", objDestinationFolder.Url, objItem.Folder.Name);
                SPFolder objNewFolder = objDestinationFolder.SubFolders.Add(dirURL);
                objNewFolder.Update();

                //Copy all the files in the sub folder
                RecursiveCopy(objSourceList, objItem.Folder, objNewFolder);
            }
        }
    }

public static void CopyListItems(string SourceSiteURL, string DestinationSiteURL, string ListName)
    {
        string DestinationURL = string.Empty;

        using (SPSite SourceSite = new SPSite(SourceSiteURL))
        {
            using (SPWeb SourceWeb = SourceSite.OpenWeb())
            {
                using (SPSite DestinationSite = new SPSite(DestinationSiteURL))
                {
                    using (SPWeb DestinationWeb = DestinationSite.OpenWeb())
                    {
                        DestinationWeb.AllowUnsafeUpdates = true;

                        //Get the QA Forms Document libarary from the source web
                        SPList objSourceList = SourceWeb.Lists[ListName];

                        SPList objDestinationList = null;

                        try
                        {
                            objDestinationList = DestinationWeb.Lists[ListName];
                        }
                        catch
                        {
                            //Create a list in the destination web
                            DestinationWeb.Lists.Add(ListName, string.Empty, SPListTemplateType.DocumentLibrary);
                        }

                        objDestinationList = DestinationWeb.Lists[ListName];

                        //Recursively copy all the files and folders
                        RecursiveCopy(objSourceList, objSourceList.RootFolder, objDestinationList.RootFolder);



                        DestinationWeb.Update();
                        DestinationWeb.AllowUnsafeUpdates = false;
                    }
                }
            }
        }
    }

Source

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.