Rob Carroll
Rob Carroll

Reputation: 377

Upload file to Sharepoint from External MVC Web Application

How can I use Sharepoint's default CopyIntoItems method to upload a new file into sharepoint from an external web application by passing in a sharepoint username and password. I don't want to use the default credentials because I'm using SQL server based forms authentication on the MVC web application.

I've tried the following:

Where CopySoapClient is the webservice connected to this url.

http://sharepointaddress/_vti_bin/copy.asmx

Code Sample

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;
}

The problem is that I keep getting the following error:

The HTTP request is unauthorized with client authentication scheme 'Negotiate'. The authentication header received from the server was 'Negotiate,NTLM'.

When I tried putting this in the web.config in the web service bindings:

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

Then I got the following error:

The provided URI scheme 'http' is invalid; expected 'https'. Parameter name: via

Upvotes: 0

Views: 4032

Answers (1)

Rob Carroll
Rob Carroll

Reputation: 377

I figured it out. All I need to do was change the security mode in the web.config to

TransportCredentialOnly

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

Upvotes: 0

Related Questions