GeeCON Prague 2014*
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
Author

Consume Sharepoint Web Service from inside code

Dave Ross
Greenhorn

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.Log","org.apache.commons.logging.impl.SimpleLog");
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
System.out.println(strOutput);

//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");
auth.setPassword("passwd1");
auth.setDomain("DOMAIN");
auth.setHost("sharepointhost");

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

Dave Ross
Greenhorn

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
Greenhorn

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
Greenhorn

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: http://aspose.com/file-tools
 
subject: Consume Sharepoint Web Service from inside code