将文件从外部MVC Web应用程序上载到Sharepoint

本文关键字:上载 Sharepoint 应用程序 Web 文件 从外部 MVC | 更新日期: 2023-09-27 18:25:37

如何使用Sharepoint的默认CopyIntoItems方法,通过传入Sharepoint用户名和密码,将新文件从外部web应用程序上载到Sharepoint。我不想使用默认凭据,因为我在MVC web应用程序上使用基于SQL服务器的表单身份验证。

我尝试过以下操作:

其中CopySoapClient是连接到此url的Web服务。

http://sharepointaddress/_vti_bin/copy.asmx

代码示例

public static bool UploadSharePointFile(string file, string destination)
{
    bool success = false;
    CopySoapClient client = new CopySoapClient();
    client.ClientCredentials.Windows.ClientCredential = new NetworkCredential("username", "password", "domain");
    try
    {
        client.Open();
        string filename = Path.GetFileName(file);
        string destinationUrl = destination + filename;
        string[] destinationUrls = { destinationUrl };
        FieldInformation i1 = new FieldInformation { DisplayName = "Title", InternalName = "Title", Type = FieldType.Text, Value = filename };
        FieldInformation[] info = { i1 };
        CopyResult[] result;
        byte[] data = File.ReadAllBytes(file);
        uint ret = client.CopyIntoItems(filename, destinationUrls, info, data, out result);
        if (result != null && result.Length > 0 && result[0].ErrorCode == 0)
            success = true;
    }
    finally
    {
        if (client.State == System.ServiceModel.CommunicationState.Faulted)
            client.Abort();
        if (client.State != System.ServiceModel.CommunicationState.Closed)
            client.Close();
    }
    return success;
}

问题是我一直收到以下错误:

HTTP请求未经授权,客户端身份验证方案为"协商"。从服务器收到的身份验证标头为"协商,NTLM"。

当我尝试将其放入web服务绑定的web.config中时:

<security mode="Transport">
    <transport clientCredentialType="Ntlm" proxyCredentialType="None"
      realm="" />
    <message clientCredentialType="UserName" algorithmSuite="Default" />
</security>

然后我得到以下错误:

提供的URI方案"http"无效;应为"https"。参数名称:通过

将文件从外部MVC Web应用程序上载到Sharepoint

我想明白了。我所需要做的就是将web.config中的安全模式更改为

TransportCredentialOnly

<security mode="TransportCredentialOnly">
    <transport clientCredentialType="Ntlm" proxyCredentialType="None"
      realm="" />
    <message clientCredentialType="UserName" algorithmSuite="Default" />
</security>