0
votes

This is my first time ever with Sharepoint. Here is the scenario

  1. I have a stand alone web application
  2. I also have a stand alone sharepoint server.
  3. Both are on different servers.
  4. I need to upload a file from web application to sharepoint

I found 2 methods online,

  1. Using the webservice provided by Sharepoint (CopyIntoItems)
  2. Using jQuery library of Sharepoint webservice

After searching the web, I think the jQuery part will not work (you can correct me).

I am looking for a method that takes username/password and uploads a pdf file to Sharepoint server. The following is my C# code that tries to upload but ends up in error

public bool UploadFile(string file, string destination)
    {
        bool success = false;
        CopySoapClient client = new CopySoapClient();

        if (client.ClientCredentials != null)
            client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
        
        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);
            uint ret = client.CopyIntoItems(file, 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;
    }

I am calling the above function like this

UploadFile(@"C:\temp\uploadFile.txt", "http://spf-03:300/demo/Dokumente").ToString();

Error that i get:

Error Code: Destination Invalid

Error Message: The service method 'Copy' must be called on the same domain that contains the target URL.

2

2 Answers

0
votes

There is the 3rd option with SharePoint 2010 and that is to use the Client Side object model. The client side object model a a sub set of the larger Sharepoint API, but it does cover uploading documents. Below is blog post with an example of uploading.

Upload document through client object model

As with most things in SharePoint you will need to authenticate against it the site, so find out if your site collection is forms based or claims based and then you should be able to find sample code for your situation.

0
votes

Solution to the problem:

The problem was that the "security token webservice" was not working and it was giving some error when we manually ran the webservice.

The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs.

The above exception is a generic one. To view the exact exception we enabled remote error viewing from the web.config file of the webservice(link) and saw the exact exception. We found the solution for the exception and the service started. After that everything was working fine.