File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Services and the fly likes Consume Sharepoint Web Service from inside code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "Consume Sharepoint Web Service from inside code" Watch "Consume Sharepoint Web Service from inside code" New topic

Consume Sharepoint Web Service from inside code

Dave Ross

Joined: Nov 30, 2010
Posts: 2
Hi All,

Trying to get a sharepoint webservice talking to code has left me clueless after a few days of searching on the web and trying many examples from blogs.
I'm using wsdl2java to create the code from the locally copied WSDL but I'm running into the dreaded 401 error after trying numerous approaches.

The code below is where I advanced the most, I can successfully get the WSDL which also requires NTLM etc, but what do to do afterwards is unclear to me.

This is the code I have so far:

// Enable logging
System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire.header", "debug");
//The above outputs all the happy talk between code & sharepoint server

// Update this to the actual URL
String url = "http://sharepointhost/WebForms/_vti_bin/lists.asmx?wsdl"; //I use this in a method to get the response back
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(url);
HostConfiguration hostConfig= client.getHostConfiguration();

// Authenticate using NTLM
client.getState().setCredentials("domainname", "sharepointhost", new NTCredentials("user1","passwd1","","DOMAIN"));
int statusCode = client.executeMethod(method);

System.out.println("Stat=" +HttpStatus.getStatusText(statusCode)); //This returns OK to systemout
String strOutput = method.getResponseBodyAsString(); //This returns the WSDL

//The output so far shows that NTLM auth has been done, and the wsdl output is returned
//Next lines are where it's becoming blurry for me on how to continue using that webservice.

HttpTransportProperties.Authenticator auth = new HttpTransportProperties.Authenticator();
auth.setUsername ("user1");

List authPrefs = new ArrayList (1);
authPrefs.add (AuthPolicy.NTLM);
auth.setAuthSchemes (authPrefs);

ListsSoapStub lists = new ListsSoapStub();
lists._setProperty(org.apache.axis2.transport.http.HTTPConstants.AUTHENTICATE, auth); //This doesn't seem the right way to do it?
GetListCollectionResponseGetListCollectionResult res = lists.getListCollection(); //and here it's not authenticated

In this example I get a no endpoint set error, but in some other tries/approaches I managed to get the endpoint set only to have it complain with the 401 unauthorized error.

Any help is more then appreciated, this problem is doing in my head, I thought consuming a sharepoint webservice would be a little more straightforward


Dave Ross

Joined: Nov 30, 2010
Posts: 2
I got it working, with only the help from God-of-Google and way too much persistence, pure endorphins overflow now. damn, I'm happy!
For those stuck with the same problem, code below works.
I generated the java code with axis2 1.5.1 & options "wsdl2java -uri wsdl.wsdl -s -ssi -s -f

Please give some feedback on how to improve this.
Kavi tha

Joined: Jan 10, 2011
Posts: 1
Hi Daven,

I am facing the same Unauthorisation problem. I tried your coding but still facing the same issue.

Can you please help me to solve this.

Thanks in advance.
Sairajesh Parameswar

Joined: Aug 21, 2013
Posts: 2
Guys Please Check My Full Blown Working Solution in the Post "Sharepoint Working Solutions with Axis 2" . You will get going in under a minute I promise!!
I agree. Here's the link:
subject: Consume Sharepoint Web Service from inside code
It's not a secret anymore!