使用 Web 服务将文档上载到 SharePoint 2013 Online

本文关键字:SharePoint 2013 Online 上载 文档 Web 服务 使用 | 更新日期: 2023-09-27 18:31:27

我有一个客户正在 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,如本文中所述。然而,我的问题是还涉及ADFS。我怎样才能针对 ADFS 进行身份验证?

使用 Web 服务将文档上载到 SharePoint 2013 Online

此错误很可能是由于不正确的身份验证模式而发生的。
由于 SharePoint Online (SPO) 使用基于声明的身份验证,因此不能在 SPO 中将 NetworkCredential 类用于身份验证。为了在 SPO 中对 ADFS 执行身份验证,您可以使用 SharePoint Online Client Components SDK 中的 SharePoint OnlineCredentials 类。

如何在 SharePoint Online 中对 SharePoint Web Services 进行身份验证 (SPO)

以下示例演示如何检索身份验证 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);
 }

引用

  • 使用基于声明的 SharePoint Online 中的远程身份验证认证
  • SharePoint Online Client Components SDK

就我而言(在本地)我有这个错误。 当我在 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 和端口。您应该不再有此类问题。