3

New to SharePoint.

I'm trying to upload a document to SharePoint using it's CopyIntoItems web service method with Java but keep on getting 400 Bad Request. I've use the Java's wsimport to generate the class files from the .wsdl file. Here is my Java code with the generated classes.

public static void createDocument(CopySoap port) {
    String url = SoapPortProvider.spSiteUrl + "/Shared Documents/Temp Folder/test.txt";
    String sourceUrl = "http://null";

    byte[] content = IoUtil.getBytes(new File("C:/CopyFile/READ-ME.txt"));

    FieldInformation descInfo = new FieldInformation ();
    descInfo.setDisplayName("Test Doc");
    descInfo.setType(FieldType.TEXT);
    descInfo.setValue("Test uploaded file");        


    DestinationUrlCollection urls = new DestinationUrlCollection();
    urls.getString().add(url);

    FieldInformationCollection infos = new FieldInformationCollection ();
    infos.getFieldInformation().add(descInfo);  

    CopyResultCollection results = new CopyResultCollection ();
    Holder<CopyResultCollection> resultHolder = new Holder<CopyResultCollection>(results);

    Holder<Long> longHolder = new Holder<Long>(new Long(-1));

    port.copyIntoItems(sourceUrl, urls, infos, content, longHolder, resultHolder);

}

My SOAP Request looks like

<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <CopyIntoItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">    
         <SourceUrl>http://null</SourceUrl>
         <DestinationUrls>
            <string>https://www.mysite.com/sites/TestSite/Shared Documents/Temp Folder/test.txt</string>
         </DestinationUrls>
         <Fields>
            <FieldInformation Value="Test uploaded file" DisplayName="Test Doc" Type="Text"/>
         </Fields>
         <Stream>KioqTWFrZSBzdXJlIHRoZSBjb250ZW50IGlzIHVuZGVyIEM6L0NvcHlGaWxlLy4gICoqKg0KDQpUbyBydW46DQoNCjEuICBFZGl0IHRoZSBkZXBsb3kucHJvcHMgZmlsZS4gIFNwZWNpZnkgdGhlIHNvdXJjZSAoZm9sZGVyIHRoYXQgY29udGFpbiBpdGVtcyB0byBkZXBsb3kpLCBkZXN0aW5hdGlvbiAoZm9sZGVyIHRvIGRlcGxveSB0byksIGFuZCBmaWxlcyAodGhlIGl0ZW1zIHRvIGRlcGxveSkuDQoyLiAgRG91YmxlIGNsaWNrIG9uIGRlcGxveUN1c3RvbWl6YXRpb24uYmF0DQoNCk5vdGUgdGhhdCBhIGxvZyBvZiB0aGUgcHJvZ3Jlc3Mgd2lsbCBiZSBjcmVhdGVkIGluIEM6L0NvcHlGaWxlL2NvcHlGaWxlLmxvZyANCg0KTm90ZSB0aGF0LCBmb3IgcHJlY2F1dGlvbiwgZXhpc3RpbmcgZmlsZSB3aWxsIG5vdCBiZSBvdmVyd3JpdHRlbiwgaW5zdGVhZCB3aWxsIGJlIHNhdmVkIGFzIHlvdXJfY29kZV9maWxlLm9sZA==</Stream>
      </CopyIntoItems>
    </S:Body>
</S:Envelope>

And the response I get is

null: HTTP/1.1 400 Bad Request
Content-length: 0
X-powered-by: ASP.NET
Server: Microsoft-IIS/7.5
Date: Tue, 14 Feb 2012 16:29:51 GMT
Microsoftsharepointteamservices: 14.0.0.5138

which doesn't tell me much. What could be missing?

duvo
  • 1,634
  • 2
  • 18
  • 30
  • That sourceUrl of `http://null` looks suspicious. In this answer, it looks like setting the source to the same URL as the target works. http://stackoverflow.com/a/998565/1030409 – Patrick Feb 14 '12 at 21:41
  • I've tried that as well but still getting the same response. Thanks though. – duvo Feb 14 '12 at 22:32
  • I need the same solution. Trying the below answer and getting lots of compile errors. help please – topcat3 May 14 '13 at 15:12

2 Answers2

3

I have use the Following code its work perfectly for me:

    try {
        //Copy WebService Settings 
        string webUrl           = "http://sharepointportal.ABC.com/";
        WSCopy.Copy copyService = new WSCopy.Copy();
        copyService.Url         = webUrl + "/_vti_bin/copy.asmx";
        copyService.Credentials = new NetworkCredential("username", "****", "Domain");

        //Declare and initiates the Copy WebService members for uploading 

        string sourceUrl        = "C:\\Work\\Ticket.Doc";   

        //Change file name if not exist then create new one     
        string[] destinationUrl    = { "http://sharepointportal.ABC.com/personal/username/Document Upload/Testing Document/newUpload.Doc" };

        WSCopy.CopyResult cResult1 = new WSCopy.CopyResult();

        WSCopy.CopyResult cResult2 = new WSCopy.CopyResult();

        WSCopy.CopyResult[] cResultArray = { cResult1, cResult2 };

        WSCopy.FieldInformation fFiledInfo = new WSCopy.FieldInformation();

        fFiledInfo.DisplayName = "Description";

        fFiledInfo.Type        = WSCopy.FieldType.Text;

        fFiledInfo.Value       = "Ticket";

        WSCopy.FieldInformation[] fFiledInfoArray = { fFiledInfo }; 

        FileStream strm = new FileStream(sourceUrl, FileMode.Open, FileAccess.Read); 

        byte[] fileContents = new Byte[strm.Length]; 

        byte[] r = new Byte[strm.Length];

        int ia = strm.Read(fileContents, 0, Convert.ToInt32(strm.Length));
        strm.Close();
        //Copy the document from Local to SharePoint 

        uint copyresult = copyService.CopyIntoItems(sourceUrl, destinationUrl, fFiledInfoArray, fileContents, out cResultArray); 

        MessageBox.Show("Suceess");  

     }
     catch (Exception ex) { 
        MessageBox.Show(ex.Message);

     }
thornjad
  • 155
  • 1
  • 13
Rony SP
  • 2,618
  • 15
  • 16
  • Hi Ravi, can you post your SOAP request. I believe that there is something wrong with my SOAP message, maybe I can identify it after comparing it with your. – duvo Feb 15 '12 at 15:08
  • Nevermind, there is a bug in my code, I'm passing extra string when initializing the Copy service. Thanks Ravi. – duvo Feb 15 '12 at 15:21
2

Got it! There is just a bug in my code in initialization. Here is the working code to anyone out there looking to work with SharePoint and Java. I've use JAX-WS wsimport tool to generate the class file from the .wsdl file. You can point the tool straight to the url of the WSDL, for example, https://my.site.come/sites/mysite/_vti_bin/copy.asmx?wsdl

public static CopySoap getPort(String username, String password)  {

    Copy service = new Copy();
    CopySoap port = service.getCopySoap();

    BindingProvider bp = (BindingProvider) port;

    bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, username);
    bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password);
    bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, 
            "https://my.site.com/sites/mysite/_vti_bin/copy.asmx");


    return port;
}   

public static void createDocument(CopySoap port) {
    String url = "https://my.site.com/sites/mysite/Shared Documents/Temp Folder/test.txt";
    String sourceUrl = "C:\\CopyFile\\READ-ME.txt";     

    DestinationUrlCollection urls = new DestinationUrlCollection();
    urls.getString().add(url);

    byte[] content = IoUtil.getBytes(new File(sourceUrl));

    FieldInformation titleInfo = new FieldInformation ();
    titleInfo.setDisplayName("Title");
    titleInfo.setType(FieldType.TEXT);
    titleInfo.setValue("Test Doc");

    FieldInformationCollection infos = new FieldInformationCollection ();
    infos.getFieldInformation().add(titleInfo);

    CopyResultCollection results = new CopyResultCollection ();

    Holder<CopyResultCollection> resultHolder = new Holder<CopyResultCollection>(results);      

    Holder<Long> longHolder = new Holder<Long>(new Long(-1));       

    port.copyIntoItems(sourceUrl, urls, infos, content, longHolder, resultHolder);

}
duvo
  • 1,634
  • 2
  • 18
  • 30
  • I just want to say that this post helped me SOOOOOOOOOOOOOOOOOOOOO much! I have been trying to get this to work for about a day now. – Reid Mac Apr 05 '12 at 17:01
  • Glad it helps. Just a warning though. I ran into issue with uploading large file (>10MB) to SharePoint, it's extremely slow. I ended up using WebDav with the web service. Basically map a drive to SharePoint, then use Java IO to copy file to that drive. The upload speed is tremendously faster. – duvo Apr 06 '12 at 19:18
  • I didn't run into that issue, I uploaded a 28MB file, and it uploaded in just about the same amount of time as a smaller 4MB file did. Granted, our SharePoint is located on site which might be the reason. We are switching to a SharePoint which will be located in a different state, so I will keep this in mind. Thanks again. =) – Reid Mac Apr 06 '12 at 19:59
  • 1
    can I get your full java file with imports? – topcat3 May 14 '13 at 15:13