0
votes

I'm developing a custom component in Java (1.7). This component needs to be able to Push and Pull a file to a SharePoint 2010 Server.

I've successfully Pushed a file along with metadata to the Server. I've also successfully Pulled a file from the Server.

Well I need to be able to search (query) the Server depending on different search criteria according to what the User feeds my component.

I've been unable to successfully connect to the SPSearch Web Service.

I get this error:

"Error caught in main: javax.xml.ws.WebServiceException: {http://microsoft.com/webservices/SharePoint/QueryService}QueryServiceSoap is not a valid service. Valid services are: {http://schemas.microsoft.com/sharepoint/soap/}Lists "

I've searched on Google endlessly and found some C# code that I was able to teach myself how I need to use the service, but it doesn't help me utilize the service successfully. I cannot find a good Java reference to utilize this web service.

Any help or suggestions would be greatly appreciated. Thank you.

(wsdl = SPURL/_vti_bin/spsearch.asmx?wsdl; endpoint = SPURL/_vti_bin/spsearch.asmx)

Here's the Code:

protected ListsSoap getListsSoap(String username, String password, String wsdl, String endpoint) throws Exception {
    System.out.println("Creating a ListsSoap instance...");
    Lists service = new Lists(new URL(wsdl), new QName("http://schemas.microsoft.com/sharepoint/soap/", "Lists"));
    ListsSoap port = service.getListsSoap();
    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, endpoint);
    return port;
}

protected CopySoap getCopySoap(String username, String password, String wsdl, String endpoint) throws Exception {
    System.out.println("Creating a CopySoap instance...");
    Copy service = new Copy(new URL(wsdl), new QName("http://schemas.microsoft.com/sharepoint/soap/", "Copy"));
    CopySoap copySoap = service.getCopySoap();
    BindingProvider bp = (BindingProvider) copySoap;
    bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, username);
    bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password);
    bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpoint);
    return copySoap;
}

protected QueryServiceSoap getQuerySoap(String username, String password, String wsdl, String endpoint) throws Exception {
    System.out.println("Creating a QuerySoap instance...");
    QueryService service = new QueryService(new URL(wsdl), new QName("http://microsoft.com/webservices/SharePoint/QueryService", "QueryServiceSoap"));
    QueryServiceSoap querySoap = service.getQueryServiceSoap();
    BindingProvider bp = (BindingProvider) querySoap;
    bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, username);
    bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password);
    bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpoint);
    return querySoap;
}
1

1 Answers

0
votes

Educating myself further on SharePoint Web Services and looking at examples provided via the web I came up with the solution:

First to retrieve a file knowing the specific file name, one would use the Copy service.

I no longer needed to query the SP site as it wasn't defined in the scope of the project.

Still doesn't answer how to query the SP successfully, but it's the answer to my project.

Here's the code that retrieve the file:

private Document downloadSpFile(CopySoap p, String url) throws IOException {
    // Define the variables that will store the output from the web service
    // call
    System.out.println("Holders...");
    Holder<Long> l = new Holder<Long>();
    Holder<FieldInformationCollection> myFieldInfoArray = new Holder<FieldInformationCollection>();
    Holder<byte[]> myByteArray = new Holder<byte[]>();

    System.out.println("Call Web Service...");
    // Call the web service
    p.getItem(url, l, myFieldInfoArray, myByteArray);

    System.out.println("TEST - Write to Disk...");
    // Write to file system - TESTING
    //Files.write(Paths.get("target-file.pdf"), myByteArray.value);

    System.out.println("Convert to Adobe Document");
    try {
        return new Document(myByteArray.value);
    } catch (Exception e) {
        System.out.println(e);
        return null;
    }
}