1

I am trying to make a program that copies all of a Sharepoint folder's contents (all subfolders and files) into another Sharepoint folder. Both of these folders will be on the same Sharepoint site.

However, I am trying to do this remotely - if possible*. Therefore, I have tried using the Copy web service without success. The Copy web service appears to only work with copying files, not folders. In addition, I cannot determine a way to iterate through the folder's contents to copy everything - it will only copy one item.

Thank you for any insights or tips,
Scott

*From a custom CRM workflow activity

~~Edited for clarification~~

4

2 回答 2

1

最后,我决定在 Sharepoint 中创建我自己的自定义 Web 服务,我能够从 Microsoft CRM 成功访问该服务。如果有人感兴趣,我已经粘贴了我用来复制文件夹结构的 C# 代码:

public String CopyFolderContents(String sourceURL, String folderURL, String destinationURL)
{
    try
    {
        #region Copying Code
        //Get the SPSite and SPWeb from the sourceURL
        using (SPWeb oWebsite = new SPSite(sourceURL).OpenWeb())
        {
            //Get the parent folder from the folderURL
            SPFolder oFolder = oWebsite.GetFolder(folderURL);

            //Create a list of all files (not folders) on the current level
            SPFileCollection collFile = oFolder.Files;

            //Copy all files on the current level to the target URL
            foreach (SPFile oFile in collFile)
            {
                oFile.CopyTo(destinationURL + "/" + oFile.Name, true);
            }
            //Create a list of all folders on the current level
            SPFolderCollection collFolder = oFolder.SubFolders;

            //Copy each of the folders and all of their contents
            String[] folderURLs = new String[collFolder.Count];
            int i = 0;
            foreach (SPFolder subFolder in collFolder)
            {
                folderURLs[i++] = subFolder.Url;
            }
            for (i = 0; i < folderURLs.Length; i++)
            {
                SPFolder folder = collFolder[folderURLs[i]];
                folder.CopyTo(destinationURL + "/" + folder.Name);
            }
        }
        #endregion Copying Code
    }
    catch (Exception e)
    {
        #region Exception Handling
        String Message;
        if (e.InnerException != null)
            Message =  "MESSAGE: " + e.Message + "\n\n" +
                   "INNER EXCEPTION: " + e.InnerException.Message + "\n\n" +
                   "STACK TRACE: " + e.StackTrace + "\n\n" +
                   "Source: " + sourceURL + "\n" + 
                   "Folder: " + folderURL + "\n" +
                   "Destination: " + destinationURL; 
        else
            Message =  "MESSAGE: " + e.Message + "\n\n" +
                   "STACK TRACE: " + e.StackTrace + "\n\n" +
                   "Source: " + sourceURL + "\n" +
                   "Folder: " + folderURL + "\n" +
                   "Destination: " + destinationURL;
        throw new Exception(Message);
        #endregion Exception Handling
    }
    return "Operation Successful!";
}

我所做的只是将此方法添加到 Sharepoint Web 服务中,然后从 CRM 中调用它,它就可以工作了。

感谢所有提供其他答案的人,
斯科特

于 2013-08-06T21:09:21.003 回答
-1

这个问题有一个简单的解决方案,因为它只是复制和粘贴使用简单的XCOPY命令

XCOPY将文件和/或目录树复制到另一个文件夹。XCOPY 类似于​​ COPY 命令,只是它具有额外的开关来详细指定源和目标。

复制包含所有子文件夹的文件夹

 XCOPY C:\utils\* D:\Backup\utils /s /i

这里 /i 将目标定义为文件夹

有关更多详细信息,请参阅此链接

于 2013-07-23T13:58:17.953 回答