Sharepoint 2010 客户端对象模型 - 上传文档(409 冲突)

发布于 2024-10-06 10:50:01 字数 669 浏览 0 评论 0原文

我正在使用 SP2010 客户端对象模型上传到文档库,遵循 Microsoft 的指导:http://msdn.microsoft.com/en-us/library/ee956524.aspx#SP2010ClientOMOpenXml_Uploading

我在执行以下代码时遇到 HTTP 409(冲突)状态代码。

var clientContext = new ClientContext("http://myservername/sites/subsitename") { Credentials = LogonCredentials };
using (var fileStream = new FileStream(@"C:\Temp\Test.txt", FileMode.Open))
{
    Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, "/MyDocLibraryName/Test_FromClientOM.txt", fileStream, true);
} 

我做错了什么?

I am using the SP2010 Client Object Model to upload to a document library, following the lead from Microsoft here: http://msdn.microsoft.com/en-us/library/ee956524.aspx#SP2010ClientOMOpenXml_Uploading

I am facing an HTTP 409 (Conflict) status code when executing the following code.

var clientContext = new ClientContext("http://myservername/sites/subsitename") { Credentials = LogonCredentials };
using (var fileStream = new FileStream(@"C:\Temp\Test.txt", FileMode.Open))
{
    Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, "/MyDocLibraryName/Test_FromClientOM.txt", fileStream, true);
} 

What am I doing wrong?

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

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

发布评论

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

评论(4

淡笑忘祈一世凡恋 2024-10-13 10:50:01

这里的问题是我上传到的网站是一个子网站,而不是 sharepoint 的根网站。我不知道这是否是一个“设计”选择,但似乎您必须使用 ClientContext 的共享点的根,至少在这种特殊情况下是这样。

工作代码:

var clientContext = new ClientContext("http://myservername") { Credentials = LogonCredentials };
using (var fileStream = new FileStream(@"C:\Temp\Test.txt", FileMode.Open))
{
       Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, "/sites/subsitename/MyDocLibraryName/Test_FromClientOM.txt", fileStream, true);
} 

The issue here was that the site I am uploading to is a subsite, not the root of sharepoint. I don't know if this was a "design" choice or not, but it seems you have to use the root of sharepoint for the ClientContext, at least in this particular case.

Working code:

var clientContext = new ClientContext("http://myservername") { Credentials = LogonCredentials };
using (var fileStream = new FileStream(@"C:\Temp\Test.txt", FileMode.Open))
{
       Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, "/sites/subsitename/MyDocLibraryName/Test_FromClientOM.txt", fileStream, true);
} 
jJeQQOZ5 2024-10-13 10:50:01

我在尝试通过 SharePoint 2010 客户端对象模型上传文件时也遇到了 409 错误。绝对确保您将文件上传到的路径完全存在。该调用不会创建任何(子)文件夹。如果您将 ClientContext 连接到根子网站或直接连接到您所说的子网站,这并不重要。只需确保您始终向 SaveBinaryDirect 方法提供要上传到的位置的 SPSite 相对 URL。

例如,如果您要将 ClientContext 连接到 http://somesite/sites/subsitename,请确保您还向 SaveBinaryDirect 传递字符串 /sites/subsitename/documents/filename.txt,因此相对于 SPSite,而不是您使用 ClientContext 连接到的子站点。

I was also facing a 409 error while trying to upload a file via the SharePoint 2010 client object model. Make absolutely sure the path you are uploading the file to completely exists. The call will not create any (sub)folders. It does not matter if you're connecting your ClientContext to the root subweb or directly to the subsite as you say. Just make sure you're always feeding the SaveBinaryDirect method the SPSite relative URL of the place to upload to that exists.

For example if you're connecting your ClientContext to http://somesite/sites/subsitename, make sure you're passing to SaveBinaryDirect also the string /sites/subsitename/documents/filename.txt, so relative to the SPSite and not the subsite you're connecting to using your ClientContext.

拔了角的鹿 2024-10-13 10:50:01

Soledad Pano 有一个博客条目 Sharepoint 上传文件错误:“远程服务器返回错误:(409) 冲突”,这帮助了我:

我发现问题出在库名称上。它包含一个破折号,例如“My-LibraryName”。当我重命名它而不带破折号时,它开始工作

Soledad Pano has a blog-entry Sharepoint Upload File Error: ‘The remote server returned an error: (409) Conflict’, which helped me:

I figured out that the problem was the library name. It contained a dash on it, like “My-LibraryName”. When I renamed it without the dash it started working

夜灵血窟げ 2024-10-13 10:50:01

在我的例子中,文件是使用 SaveBinaryDirect 上传到打开版本控制的库中的。如果未签入该文件,则任何后续尝试上传更新版本的操作都将导致 409 错误。打开版本控制后,请务必在上传后签入。

    var clientContext = (ClientContext)file.Context;
    destinationWebContext.Load(destinationList, d => d.ParentWebUrl);
    destinationWebContext.Load(destinationList, d => d.RootFolder.ServerRelativeUrl);
    clientContext.Load(file, f => f.ServerRelativeUrl);
    clientContext.Load(file, f => f.Name);

    if (clientContext.HasPendingRequest)
       clientContext.ExecuteQueryRetry();

    if (destinationWebContext.HasPendingRequest)
        destinationWebContext.ExecuteQueryRetry();

    var location = string.Format("{1}/{2}", destinationList.ParentWebUrl, destinationList.RootFolder.ServerRelativeUrl, file.Name);
    var fileInfo = File.OpenBinaryDirect(clientContext, file.ServerRelativeUrl);
    File.SaveBinaryDirect(destinationWebContext, location, fileInfo.Stream, overwrite);

    File newFile = destinationWebContext.Web.GetFileByServerRelativeUrl(location);
    newFile.CheckIn("Checked in by provisioning service", Microsoft.SharePoint.Client.CheckinType.MajorCheckIn);
    destinationWebContext.ExecuteQuery();

In my case the file was uploaded using SaveBinaryDirect into a library with versioning turned on. If the file is not checked in, any subsequent attempts to upload a newer version will result in the 409 error. Make sure to check-in after upload in when versioning is turned on.

    var clientContext = (ClientContext)file.Context;
    destinationWebContext.Load(destinationList, d => d.ParentWebUrl);
    destinationWebContext.Load(destinationList, d => d.RootFolder.ServerRelativeUrl);
    clientContext.Load(file, f => f.ServerRelativeUrl);
    clientContext.Load(file, f => f.Name);

    if (clientContext.HasPendingRequest)
       clientContext.ExecuteQueryRetry();

    if (destinationWebContext.HasPendingRequest)
        destinationWebContext.ExecuteQueryRetry();

    var location = string.Format("{1}/{2}", destinationList.ParentWebUrl, destinationList.RootFolder.ServerRelativeUrl, file.Name);
    var fileInfo = File.OpenBinaryDirect(clientContext, file.ServerRelativeUrl);
    File.SaveBinaryDirect(destinationWebContext, location, fileInfo.Stream, overwrite);

    File newFile = destinationWebContext.Web.GetFileByServerRelativeUrl(location);
    newFile.CheckIn("Checked in by provisioning service", Microsoft.SharePoint.Client.CheckinType.MajorCheckIn);
    destinationWebContext.ExecuteQuery();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文