使用 WebRequest PUT 将文件上传到 SharePoint WSS 3.0 [英] Upload file to SharePoint WSS 3.0 with WebRequest PUT

查看:53
本文介绍了使用 WebRequest PUT 将文件上传到 SharePoint WSS 3.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我有这么好的一小段代码,很像这种使用 WSS WebServices 上传方法的所有其他版本.不过,我遇到了一个主要问题 - 一旦我将文件上传到我的文档列表中,并更新了列表项以编写评论/描述,该文件就会卡在那里.我的意思是,一旦我上传了文件,这种方法就不会覆盖它.似乎没有其他人发布过这个问题,所以..有人吗?

Hey, I've got this nice little piece of code, much like all the other versions of this method of upload using WSS WebServices. I've got one major problem though - once I have uploaded a file into my doc list, and updated the list item to write a comment/description, the file is stuck there. What I mean is that this method will not overwrite the file once I've uploaded it. Nobody else out there seems to have posted this issue yet, so .. anyone?

我有另一个版本的方法,它使用 byte[] 而不是 Stream .. 同样的问题.

I have another version of the method which uses a byte[] instead of a Stream .. same issue though.

注意:我已经关闭了库的在编辑文档之前需要签出文档"选项.不幸的是 .. doc 库确实开启了版本控制,每次更新都会创建一个主要版本.

Note: I have switched off the 'require documents to be checked out before they can be edited' option for the library. No luck tho .. The doc library does have versioning turned on though, with a major version being created for each update.

    private void UploadStream(string fullPath, Stream uploadStream)
    {
        WebRequest request = WebRequest.Create(fullPath);
        request.Credentials = CredentialCache.DefaultCredentials; // User must have 'Contributor' access to the document library
        request.Method = "PUT";
        request.Headers.Add("Overwrite", "t");

        byte[] buffer = new byte[4096];
        using (Stream stream = request.GetRequestStream())
        {
            for (int i = uploadStream.Read(buffer, 0, buffer.Length); i > 0; i = uploadStream.Read(buffer, 0, buffer.Length))
            {
                stream.Write(buffer, 0, i);
            }
        }
        WebResponse response = request.GetResponse(); // Upload the file
        response.Close();
    }

原始来源:http://geek.hubkey.com/2007/10/upload-file-to-sharepoint-document.html

EDIT - 主要发现......当我从我的 nUnit 测试项目中调用它时,它工作正常.似乎只有当我从我的 WCF 应用程序调用它时才会失败(nUnit 在登录用户帐户下运行,WCF 应用程序在同一用户下运行应用程序池 - 我的帐户,它在 SharePoint 中也具有有效权限).

EDIT -- major finding .. when I call it from my nUnit test project it works fine. It seems it only fails when I call it from my WCF application (nUnit running under logged on user account, WCF app has app pool running under that same user -- my account, which also has valid permissions in SharePoint).

坚果.现在从哪里开始?!",我暗自思索.

Nuts. "Now where to start?!", I muses to myself.

推荐答案

SOLVED -- 我发现了一个小错误——文件​​被创建在正确的位置,但更新路径错误.. 我最终找到了一个文件夹,里面装满了许多新版本的文件.. 哦!

SOLVED -- I found a little bug - the file was being created in the right place, but the update path was wrong.. I ended up finding a folder full of files with many, many new versions.. doh!

这篇关于使用 WebRequest PUT 将文件上传到 SharePoint WSS 3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆