如何将文件从一个 SP 2010 根站点复制到其子站点 (C#)

发布于 2024-11-05 05:51:53 字数 1740 浏览 2 评论 0原文

我使用 SP 设计器在“http://test1/SitePages/”和“http://test1/sideA/SitePages/”下手动创建了“testFolder”。文件夹“http://test1/SitePages/testFolder/”中有一个txt文件,我想做的是将txt文件复制到子站点文件夹“http://test1/sideA/SitePages/testFolder”

protected void copyFile() 
            {
                SPSite mySite = new SPSite("http://test1/");
                SPWeb myWeb = mySite.OpenWeb();
                SPFolder collFolder = myWeb.GetFolder("SitePages/testFolder");
                SPFileCollection collFiles = null;
                if (collFolder.Exists)
                {
                    collFiles = collFolder.Files;
                }
                else
                {
                    Response.Write("Source folder dosenot exists");
                }

                myWeb.Dispose();
                mySite.Dispose();

                SPSite destSite = new SPSite("http://test1/sideA/");
                SPWeb destWeb = destSite.OpenWeb();
                SPFolder destFolder = destWeb.GetFolder("SitePages/testFolder");

                if (destFolder.Exists)
                {
                    foreach (SPFile oFile in collFiles)
                    {
                        oFile.CopyTo(destFolder + "/" + oFile.Name, true);
                    }
                }
                else 
                {
                    Response.Write("Target folder dosenot exists");
                }

                destWeb.Dispose();
                destSite.Dispose();

            }

0x81070925您无法复制“/SitePages/testFolder/something.txt”本身。

描述:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

异常详细信息:System.Runtime.InteropServices.COMException:0x81070925您无法将“/SitePages/testFolder/something.txt”复制到自身。

I manually created both "testFolder" under "http://test1/SitePages/" and "http://test1/sideA/SitePages/" using SP designer. There is a txt file in the folder "http://test1/SitePages/testFolder/", what Im trying to do is copy the txt file to subsite folder "http://test1/sideA/SitePages/testFolder"

protected void copyFile() 
            {
                SPSite mySite = new SPSite("http://test1/");
                SPWeb myWeb = mySite.OpenWeb();
                SPFolder collFolder = myWeb.GetFolder("SitePages/testFolder");
                SPFileCollection collFiles = null;
                if (collFolder.Exists)
                {
                    collFiles = collFolder.Files;
                }
                else
                {
                    Response.Write("Source folder dosenot exists");
                }

                myWeb.Dispose();
                mySite.Dispose();

                SPSite destSite = new SPSite("http://test1/sideA/");
                SPWeb destWeb = destSite.OpenWeb();
                SPFolder destFolder = destWeb.GetFolder("SitePages/testFolder");

                if (destFolder.Exists)
                {
                    foreach (SPFile oFile in collFiles)
                    {
                        oFile.CopyTo(destFolder + "/" + oFile.Name, true);
                    }
                }
                else 
                {
                    Response.Write("Target folder dosenot exists");
                }

                destWeb.Dispose();
                destSite.Dispose();

            }

0x81070925You cannot copy "/SitePages/testFolder/something.txt" to itself.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Runtime.InteropServices.COMException: 0x81070925You cannot copy "/SitePages/testFolder/something.txt" to itself.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终难遇 2024-11-12 05:51:54

您是否已验证该文件夹是否存在并且拼写是否正确?我注意到您的代码和代码描述中存在一些拼写错误(Fodler、newFoler 等)。有这么简单吗?

执行此操作时需要注意以下一件事:
http://sharepointlink.blogspot.com/2011/02/beware-of -spwebgetfolderurl.html

看来 GetFolder 方法不一定会立即告诉您存在问题。在尝试复制之前,您应该检查以确保文件夹/文件存在。

如果这不能解决问题,请提供完整的错误消息以及错误发生的位置。谢谢。

Have you verified that the folder exists and that it is spelled correctly? I've noticed a few spelling errors in your code and description of your code (Fodler, newFoler, etc.) Could it be that simple?

Here is one thing you need to look out for when you are doing this:
http://sharepointlink.blogspot.com/2011/02/beware-of-spwebgetfolderurl.html

It seems that the GetFolder method won't necessarily tell you there is a problem right away. You should check to be sure the folder/file exists before trying to copy it.

If this doesn't solve the issue, please include the full error message and where it occurs. Thanks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文