使用 WebRequest PUT 将文件上传到 SharePoint WSS 3.0
嘿,我有这段不错的小代码,很像使用 WSS WebServices 的这种上传方法的所有其他版本。 不过,我遇到了一个主要问题 - 一旦我将文件上传到我的文档列表中,并更新列表项以写入评论/描述,该文件就会卡在那里。 我的意思是,一旦我上传文件,此方法就不会覆盖该文件。 似乎还没有其他人发布过这个问题,所以..有人吗?
我有另一个版本的方法,它使用 byte[] 而不是 Stream ..但有同样的问题。
注意:我已经关闭了库的“要求文档在编辑之前先检出”选项。 不过,不幸的是,文档库确实打开了版本控制,为每次更新创建了一个主要版本。
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
编辑 -- 主要发现..当我从我的 nUnit 测试项目中调用它时,它工作正常。 似乎只有当我从 WCF 应用程序调用它时才会失败(nUnit 在登录的用户帐户下运行,WCF 应用程序的应用程序池在同一用户下运行 - 我的帐户,该帐户在 SharePoint 中也具有有效权限)。
坚果。 “现在从哪里开始呢?!”我沉思着。
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?
I have another version of the method which uses a byte[] instead of a Stream .. same issue though.
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();
}
Original credits to: http://geek.hubkey.com/2007/10/upload-file-to-sharepoint-document.html
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
已解决 - 我发现了一个小错误 - 文件是在正确的位置创建的,但更新路径是错误的。我最终找到了一个充满文件的文件夹,其中包含许多新版本。啊!
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!
为什么不使用现成的 SharePoint Web 服务 Lists.asmx? 您可以在
http://SITEURL/___vti_bin/Lists.asmx
编辑< /strong>,我查看了链接,看来您正在调用开箱即用的网络服务。 这必须与版本控制相关。 您可以查看特定文件的文档库中存在的不同版本吗? 看看它是否可能通过服务添加为次要版本?
Why not use the out-of-the-box SharePoint webservice, Lists.asmx? You'll find it in
http://SITEURL/___vti_bin/Lists.asmx
Edit, I checked out the link and it seems you are calling the out of the box web service. This has got be versioning related then. Can you check out the different versions that exist in the doc lib of the specific file? see if it perhaps gets added as a minor version through the service?
您尝试过使用大写 T 吗? SharePoint 的 webdav 标头处理不太可能区分大小写,但协议确实指定了大写 T。哦,响应是什么? 412 错误代码还是完全不同的代码?
Have you tried using a capital T? SharePoint's webdav header processing is not very likely to be case-sensitive, but the protocol does specify a capital T. Oh, and what is the response? A 412 error code or something altogether different?