使用Sharepoint Copy Web服务复制文件夹和内容

时间:2013-07-23 13:52:19

标签: c# web-services sharepoint-2010

我正在尝试制作一个程序,将所有Sharepoint文件夹的内容(所有子文件夹和文件)复制到另一个Sharepoint文件夹中。这两个文件夹都位于同一个Sharepoint站点上。

但是,我试图远程执行此操作 - 如果可能*。因此,我尝试使用复制Web服务但没有成功。复制Web服务似乎仅适用于复制文件,而不适用于复制文件夹。另外,我无法确定迭代文件夹内容的方法来复制所有内容 - 它只会复制一个项目。

感谢您提供任何见解或提示,
  斯科特

*来自自定义CRM工作流程活动

~~编辑澄清~~

2 个答案:

答案 0 :(得分: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调用它并且它可以正常工作。

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

答案 1 :(得分:-1)

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

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

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

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

这里/我将目的地定义为文件夹

有关详细信息,请参阅this link