3

我有一个客户正在 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 进行身份验证?

4

2 回答 2

1

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

如何在 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);
 }

参考

于 2015-02-18T10:39:05.097 回答
0

在我的情况下(前提)我有这个错误。当我在 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/ 扩展您的混合身份验证网络应用程序,并为 Windows 身份验证创建一个区域,然后更改 Web 服务属性中的 Web 引用 URL,以使用该扩展 URL 和端口。您应该不再有此类问题。

于 2015-03-20T10:06:14.177 回答