Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 25064

Unable to read the SharePoint List using java

$
0
0

Hi,
Here i am trying to read the list of sharepoint. For this i have done the following:
I have downloaded the WSDL file which i got from The URL like: sharepointsite.com/_vti_bin/Lists.asmx?WSDL.
and i have executed the following command to generate the classes in the directory 
 “C:\Program Files\Java\jdk1.6.0_12\bin\wsimport.exe” -p com.microsoft.schemas.sharepoint.soap -keep -extension Lists.wsdl. 
 Imported those classes in my eclipse IDE java application.


 and i have written the following code

/* Creates a port connected to the SharePoint Web Service given.

	 * Authentication is done here. It also prints the authentication details

	 * in the console.

	 * @param userName SharePoint username

	 * @param password SharePoint password

	 * @return port ListsSoap port, connected with SharePoint

	 * @throws Exception in case of invalid parameters or connection error.

	 */

	public static ListsSoap sharePointListsAuth(String userName, String password) throws Exception {

	    ListsSoap port = null;

	    if (userName != null && password != null) {

	        try {

	            Lists service = new Lists();

	            port = service.getListsSoap();

	            System.out.println("Web Service Auth Username: " + userName);

	            ((BindingProvider) port).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, userName);

	            ((BindingProvider) port).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password);

	        } catch (Exception e) {

	            throw new Exception("Error: " + e.toString());

	        }

	    } else {

	        throw new Exception("Couldn't authenticate: Invalid connection details given.");

	    }

	    return port;

	}
	/**

	 * Creates a string from an XML file with start and end indicators

	 * @param docToString document to convert

	 * @return string of the xml document

	 */

	public static String xmlToString(Document docToString) {

	    String returnString = "\n-------------- XML START --------------\n";

	    try {

	        //create string from xml tree

	        //Output the XML

	        //set up a transformer

	        TransformerFactory transfac = TransformerFactory.newInstance();

	        Transformer trans;

	        trans = transfac.newTransformer();

	        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

	        trans.setOutputProperty(OutputKeys.INDENT, "yes");

	        StringWriter sw = new StringWriter();

	        StreamResult streamResult = new StreamResult(sw);

	        DOMSource source = new DOMSource(docToString);

	        trans.transform(source, streamResult);

	        String xmlString = sw.toString();

	        //print the XML

	        returnString = returnString + xmlString;

	    } catch (TransformerException ex) {

	        Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, ex);

	    }

	    returnString = returnString + "-------------- XML END --------------";

	    return returnString;

	}
	/**
	02
	 * Connects to a SharePoint Lists Web Service through the given open port,
	03
	 * and reads all the elements of the given list. Only the ID and the given
	04
	 * attributes (column names) are displayed, as well as a dump of the SOAP
	05
	 * response from the Web Service (for debugging purposes).
	06
	 * @param port an already authentificated SharePoint Online SOAP port
	07
	 * @param listName original name of the Sharepoint list that is going to be read
	08
	 * @param listColumnNames arraylist containing the various names of the Columns
	09
	 * of the SharePoint list that are going to be read. If the column name isn't
	10
	 * found, then an exception will be thrown
	11
	 * @param rowLimit limits the number of rows (list items) that are going to
	12
	 * be returned
	13
	 * @throws Exception
	14
	 */

	public static void displaySharePointList(ListsSoap port, String listName, ArrayList<String> listColumnNames, String rowLimit) throws Exception {

	    if (port != null && listName != null && listColumnNames != null && rowLimit != null) {

	        try {

	 

	            //Here are additional parameters that may be set

	            String viewName = "";

	            GetListItems.ViewFields viewFields = null;

	            GetListItems.Query query = null;

	            GetListItems.QueryOptions queryOptions = null;

	            String webID = "";

	 

	            //Calling the List Web Service

	            GetListItemsResponse.GetListItemsResult result = port.getListItems(listName, viewName, query, viewFields, rowLimit, queryOptions, webID);

	            Object listResult = result.getContent().get(0);

	            if ((listResult != null) && (listResult instanceof ElementNSImpl)) {

	                ElementNSImpl node = (ElementNSImpl) listResult;

	 

	                //Dumps the retrieved info in the console

	                Document document = node.getOwnerDocument();

	                System.out.println("SharePoint Online Lists Web Service Response:" + Manager.xmlToString(document));

	 

	                //selects a list of nodes which have z:row elements

	                NodeList list = node.getElementsByTagName("z:row");

	                System.out.println("=> " + list.getLength() + " results from SharePoint Online");

	 

	                //Displaying every result received from SharePoint, with its ID

	                for (int i = 0; i < list.getLength(); i++) {


	                    //Gets the attributes of the current row/element

	                    NamedNodeMap attributes = list.item(i).getAttributes();

	                    System.out.println("******** Item ID: " + attributes.getNamedItem("ows_ID").getNodeValue()+" ********");

	 

	                    //Displays all the attributes of the list item that correspond to the column names given

	                    for (String columnName : listColumnNames) {

	                        String internalColumnName = "ows_" + columnName;

	                        if (attributes.getNamedItem(internalColumnName) != null) {

	                            System.out.println(columnName + ": " + attributes.getNamedItem(internalColumnName).getNodeValue());

	                        } else {

	                            throw new Exception("Couldn't find the '" + columnName + "' column in the '" + listName + "' list in SharePoint.\n");

	                        }

	                    }

	                }

	            } else {

	                throw new Exception(listName + " list response from SharePoint is either null or corrupt\n");

	            }

	        } catch (Exception ex) {

	            throw new Exception("Exception. See stacktrace." + ex.toString() + "\n");

	        }

	    }
	}
	public static void main(String[] args) {
		
		    try {
		
		 
		
		        //Authentication parameters
		
		        String userName = "domain\\username";
		        String password = "pass";
		        //Opening the SOAP port of the Lists Web Service
		        ListsSoap port = Manager.sharePointListsAuth(userName, password);
		        /*
		         * Lists Web service parameters
		         * The list names below must be the *original* names of the list.
		         * if a list or column was renamed from SharePoint afterwards,
		         * these parameters don't change.
		         */
		        String listName = "listname";
		        String rowLimit = "2";
		        ArrayList<String> listColumnNames = new ArrayList<String>();
		        listColumnNames.add("Name");
		        listColumnNames.add("ID");
		        //Displays the lists items in the console
		        Manager.displaySharePointList(port, listName, listColumnNames, rowLimit);
		    } catch (Exception ex) {
		        System.err.println(ex);
		    }
		}

I am getting following exception :
Web Service Auth Username: "domain\\username"
java.lang.Exception: Exception. See stacktrace.javax.xml.ws.soap.SOAPFaultException: Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown.
It would be helpfull if i get the specific exception from the server.With this expection i am unable to track my pgm. 
I dont know where did i go wrong?  am i missing anything ?  any configuration is required ?

Thanks in advance for your help


  

Viewing all articles
Browse latest Browse all 25064

Trending Articles