【问题标题】:Upload document to Sharepoint 2013 Online using webservices使用 Web 服务将文档上传到 Sharepoint 2013 Online
【发布时间】:2013-09-21 02:12:48
【问题描述】:

我有一个客户正在 Sharepoint 2013 Online 中实施客户门户。当前程序通过邮件向客户分发文件。现在我们必须将文件上传到客户门户。

我尝试在 sharepoint 中使用复制 web 服务。我创建了一个测试项目并将 web 服务添加为 Web 参考并编写了以下测试代码:

static void Main(string[] args)
{
    string baseUrl = "https://mycustomer.sharepoint.com/sites/";
    string customer = "customerportalname";
    string serviceUrl = "/_vti_bin/copy.asmx";
    string destinationDirectory = "/folder/";
    string fileName = "uploaded.xml";

    string username = "username@outlook.com";
    string password = "password";

    XmlDocument xmlDocument = new XmlDocument();
    xmlDocument.LoadXml("<fiets><onderdeel>voorwiel</onderdeel><onderdeel>achterwiel</onderdeel><onderdeel>trappers</onderdeel><onderdeel>stuur</onderdeel><onderdeel>frame</onderdeel></fiets>");

    byte[] xmlByteArray;
    using (MemoryStream memoryStream = new MemoryStream())
    {
        xmlDocument.Save(memoryStream);
        xmlByteArray = memoryStream.ToArray();
    }

    string destinationUrl = string.Format("{0}{1}{2}{3}", baseUrl, customer, destinationDirectory, fileName);
    string[] destinationUrlArray = new string[] { destinationUrl };

    FieldInformation fieldInfo = new FieldInformation();
    FieldInformation[] fields = { fieldInfo };


    CopyResult[] resultsArray;

    using (Copy copyService = new Copy())
    {
        copyService.PreAuthenticate = true;
        copyService.Credentials = new NetworkCredential(username, password);
        copyService.Url = string.Format("{0}{1}", baseUrl, serviceUrl);

        copyService.Timeout = 600000;

        uint documentId = copyService.CopyIntoItems(destinationUrl , destinationUrlArray, fields, xmlByteArray, out resultsArray);
    }
} 

当我执行代码时,我收到以下错误:

The request failed with the error message:
--
<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="/_forms/default.aspx?ReturnUrl=%2f_vti_bin%2fcopy.asmx">here</a>.</h2>
</body></html>
--

我好像没有通过身份验证并被重定向。然而,凭据是正确的。 有人有想法吗?提前致谢!

更新

为了能够连接到 SharePoint 2013 Online,您必须附加 Office 365 身份验证 cookie,如this 帖子中所述。 然而,我的问题是还涉及到 ADFS。如何针对 ADFS 进行身份验证?

【问题讨论】:

  • 您来对地方了。但请允许我推荐一个 Stack Overflow 的姊妹站点,因为它更面向 Sharepoint:sharepoint.stackexchange.com
  • 你让它工作了吗?我很想看到你的解决方案。我也有同样的困境。

标签: c# web-services sharepoint-2013


【解决方案1】:

这个错误很可能是由于不正确的身份验证模式造成的。
由于 SharePoint Online (SPO) 使用基于声明的身份验证NetworkCredential Class 不能用于 SPO 中的身份验证。 为了在 SPO 中针对 ADFS 执行身份验证,您可以使用来自 SharePoint Online Client Components SDKSharePointOnlineCredentials class

如何在 SharePoint Online (SPO) 中验证 SharePoint Web 服务

以下示例演示如何检索身份验证 cookie:

private static CookieContainer GetAuthCookies(Uri webUri, string userName, string password)
{
    var securePassword = new SecureString();
    foreach (var c in password) { securePassword.AppendChar(c); }
    var credentials = new SharePointOnlineCredentials(userName, securePassword);
    var authCookie = credentials.GetAuthenticationCookie(webUri);
    var cookieContainer = new CookieContainer();
    cookieContainer.SetCookies(webUri, authCookie);
    return cookieContainer;
}

示例

string sourceUrl = "https://contoso.sharepoint.com/Documents/SharePoint User Guide.docx";
string destinationUrl = "https://contoso.sharepoint.com/Documents/SharePoint User Guide 2013.docx";
FieldInformation[] fieldInfos;
CopyResult[] result;
byte[] fileContent;
using(var proxyCopy = new Copy())
{
     proxyCopy.Url = webUri + "/_vti_bin/Copy.asmx";
     proxyCopy.CookieContainer = GetAuthCookies(webUri, userName, password);

     proxyCopy.GetItem(sourceUrl,out fieldInfos,out fileContent);
     proxyCopy.CopyIntoItems(sourceUrl,new []{ destinationUrl}, fieldInfos, fileContent, out result);
 }

参考文献

【讨论】:

  • 嗨瓦迪姆。我正在成功上传文件,但在上传大文件时出现操作超时错误。你知道如何增加 Web 服务的超时时间吗?
【解决方案2】:

在我的情况下(前提)我有这个错误。当我在 iis SharePoint 身份验证更改为 Web 应用程序并禁用“表单身份验证”时。现在,我无法通过 UI 进入 SharePoint,但 Web 服务可以工作......所以我已经恢复,我一直在寻找......

[Paul stork] 此站点的 Web 应用程序以经典模式而不是声明模式运行。如果您使用 Powershell 创建 Web 应用程序或从 2010 升级,则可能会发生这种情况。您可以使用 PowerShell 进行更改。

http://technet.microsoft.com/en-us/library/gg251985.aspx

我已经在管理中心(在同一个场)中由 UI 创建的另一个新应用程序中尝试了 Web 服务,它已经工作了。问题出在 Web 应用程序上。

尝试: http://sharepointyankee.com/2011/01/04/the-request-failed-with-the-error-message-object-moved-sharepoint-2010-web-services-fba/ 扩展您的混合身份验证 Web 应用程序,并为 Windows 身份验证创建一个区域,然后更改 Web 服务属性中的 Web 引用 URL,以使用该扩展 URL 和端口。您应该不会再遇到此类问题了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-06
    • 1970-01-01
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    • 2019-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多