I've just written some test code to get a list of items in a SharePoint folder with Apache Axis2 and since this was "not so easy", I'll share my insights here.
First, you need Axis2. If you're using Maven2, put this in your pom.xml:
<dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2-kernel</artifactId> <version>1.4.1</version> </dependency> <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2-adb</artifactId> <version>1.4.1</version> </dependency>
Next stop: Setting up NTLM authorization.
import org.apache.axis2.transport.http.HttpTransportProperties; import org.apache.commons.httpclient.auth.AuthPolicy; HttpTransportProperties.Authenticator auth = new HttpTransportProperties.Authenticator(); auth.setUsername ("username"); auth.setPassword "password"); auth.setDomain ("ntdom"); auth.setHost ("host.domain.com"); ListauthPrefs = new ArrayList (1); authPrefs.add (AuthPolicy.NTLM); auth.setAuthSchemes (authPrefs);
This should be the username/password you're using to login to the NT domain "ntdom" on the NT domain server "host.domain.com". Often, this server is the same as the SharePoint server you want to connect to.
If the SharePoint server is somewhere outside your intranet, you may need to specify a proxy:
HttpTransportProperties.ProxyProperties proxyProperties = new HttpTransportProperties.ProxyProperties(); proxyProperties.setProxyName ("your.proxy.com"); proxyProperties.setProxyPort (8888);
You can get these values from your Internet browser.
If there are several SharePoint "sites" on the server, set site to the relative URL of the site you want to connect to. Otherwise, leave site empty. If you have no idea what I'm talking about, browse the SharePoint server in Internet Explorer. In the location bar, you'll see an URL like this: https://sp.company.com/projects/demo/Documents2/Forms/AllItems.aspx?RootFolder=%2fprojects%2fdemo%2fDocument2%2f&FolderCTID=&View=%7b18698D80%2dE081%2d4BBE%2d96EB%2d73BA839230B9%7d. Scary, huh? Let's take it apart:
https:// = the protocol,
sp.company.com = The server name (with domain),
projects/demo = The "site" name
Documents2 = A "list" stored on the site "projects/demo"
/Forms/AllItems.aspx?RootFolder=... is stuff to make IE happy. Ignore it.
So in out example, we have to set site to:
String site = "/projects/demo";
Mind the leading slash!
To verify that this is correct, replace "/Documents2/Forms/" and anything beyond with "/_vti_bin/Lists.asmx?WSDL". That should return the WSDL definition for this site. Save the result as "sharepoint.wsdl" (File menu, "Save as..."). Install Axis2, open a command prompt in the directory where you saved the WSDL file and run this command (don't forget to replace the Java package name):
%AXIS2_HOME%\bin\WSDL2Java -uri sharepoint.wsdl -p java.package.name -d adb -s
This will create a "src" directory with the Java package and a single file "ListsStub.java". Copy it into your Maven2 project.
Now, we can get a list of the lists on the site:
ListsStub lists = new ListsStub ("https://sp.company.com"+site+"/_vti_bin/Lists.asmx"); lists._getServiceClient ().getOptions () .setProperty (HTTPConstants.AUTHENTICATE, auth);
If you need a proxy, specify it here:
options.setProperty (HTTPConstants.HTTP_PROTOCOL_VERSION, HTTPConstants.HEADER_PROTOCOL_10); options.setProperty (HTTPConstants.PROXY, proxyProperties);
We need to reduce the HTTP protocol version to 1.0 because most proxies don't allow to send multiple requests over a single connection. If you want to speed things up, you can try to comment out this line but be prepared to see it fail afterwards.
Okay. The plumbing is in place. Now we query the server for the lists it has:
String liste = "Documents2"; String document2ID; { ListsStub.GetListCollection req = new ListsStub.GetListCollection(); ListsStub.GetListCollectionResponse res = lists.GetListCollection (req); displayResult (req, res); document2ID = getIDByTitle (res, liste); }
This downloads all lists defined on the server and searches for the one we need. If you're in doubt what the name of the list might be: Check the bread crumbs in the blue part in the intern explorer. The first two items are the title of the site and the list you're currently in.
displayResult() is the usual XML dump code:
private void displayResult (GetListCollection req, GetListCollectionResponse res) { System.out.println ("Result OK: " +res.localGetListCollectionResultTracker); OMElement root = res.getGetListCollectionResult () .getExtraElement (); dump (System.out, root, 0); } private void dump (PrintStream out, OMElement e, int indent) { indent(out, indent); out.print (e.getLocalName ()); for (Iterator iter = e.getAllAttributes (); iter.hasNext (); ) { OMAttribute attr = (OMAttribute)iter.next (); out.print (" "); out.print (attr.getLocalName ()); out.print ("=\""); out.print (attr.getAttributeValue ()); out.print ("\""); } out.println (); for (Iterator iter = e.getChildElements (); iter.hasNext (); ) { OMElement child = (OMElement)iter.next (); dump (out, child, indent+1); } } private void indent (PrintStream out, int indent) { for (int i=0; i<indent; i++) out.print (" "); }
We also need getIDByTitle() to search for the ID of a SparePoint list:
private String getIDByTitle (GetListCollectionResponse res, String title) { OMElement root = res.getGetListCollectionResult ().getExtraElement (); QName qnameTitle = new QName ("Title"); QName qnameID = new QName ("ID"); for (Iterator iter = root.getChildrenWithLocalName ("List"); iter.hasNext (); ) { OMElement list = (OMElement)iter.next (); if (title.equals (list.getAttributeValue (qnameTitle))) return list.getAttributeValue (qnameID); } return null; }
With that, we can finally list the items in a folder:
{ String dir = "folder/subfolder"; ListsStub.GetListItems req = new ListsStub.GetListItems (); req.setListName (document2ID); QueryOptions_type1 query = new QueryOptions_type1 (); OMFactory fac = OMAbstractFactory.getOMFactory(); OMElement root = fac.createOMElement ( new QName("", "QueryOptions")); query.setExtraElement (root); OMElement folder = fac.createOMElement ( new QName("", "Folder")); root.addChild (folder); folder.setText (liste+"/"+dir); // <--!! req.setQueryOptions (query); GetListItemsResponse res = lists.GetListItems (req); displayResult (req, res); }
The important bits here are: To list the items in a folder, you must include the name of the list in the "Folder" element! For reference, this is the XML which actually sent to the server:
<?xml version='1.0' encoding='UTF-8'?> <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"> <soapenv:Body> <ns1:GetListItems xmlns:ns1="http://schemas.microsoft.com/sharepoint/soap/"> <ns1:listName>{12AF2346-CCA1-486D-BE3C-82223DEC3F42}</ns1:listName> <ns1:queryOptions> <QueryOptions> <Folder>Documents2/folder/subfolder</Folder> </QueryOptions> </ns1:queryOptions> </ns1:GetListItems> </soapenv:Body> </soapenv:Envelope>
If the folder name is not correct, you'll get a list of all files and folders that the SharePoint server can find anywhere. The folder names can be found in the bread crumbs. The first two items are the site and the list name, respectively, followed by the folder names.
The last missing piece is displayResult() for the items:
private void displayResult (GetListItems req, GetListItemsResponse res) { System.out.println ("Result OK: " +res.localGetListItemsResultTracker); OMElement root = res.getGetListItemsResult () .getExtraElement (); dump (System.out, root, 0); }
If you run this code and you see the exception "unable to find valid certification path to requested target", this article will help.
If the SharePoint server returns an error, you'll see "detail unsupported element in SOAPFault element". I haven't found a way to work around this bug in Axis2. Try to set the log level of "org.apache.axis2" to "DEBUG" and you'll see what the SharePoint server sent back (not that it will help in most of the cases ...)
Links: GetListItems on MSDN, How to configure Axis2 to support Basic, NTLM and Proxy authentication?, Java to SharePoint Integration - Part I (old, for Java 1.4)
Good luck!
No comments:
Post a Comment