4

在这里,我正在尝试阅读共享点列表。为此,我做了以下事情:我已经下载了从 URL 获得的 WSDL 文件,例如:sharepointsite.com/_vti_bin/Lists.asmx?WSDL。我已经执行了以下命令在目录“C:\Program Files\Java\jdk1.6.0_12\bin\wsimport.exe”-p com.microsoft.schemas.sharepoint.soap -keep -extension Lists 中生成类.wsdl。在我的 eclipse IDE java 应用程序中导入了这些类。

我写了下面的代码

/* 创建一个连接到给定的 SharePoint Web 服务的端口。

 * 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);

        }

    }

我收到以下异常:

Web 服务身份验证用户名:“域\用户名”java.lang.Exception:异常。请参阅 stacktrace.javax.xml.ws.soap.SOAPFaultException:引发了“Microsoft.SharePoint.SoapServer.SoapServerException”类型的异常。

    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
4

1 回答 1

1

我有同样的问题,尝试使用 Authenticator :

new BasicAuthentication(userName,password).authenticate();

我的 BasicAuthentication 类:

import java.net.Authenticator;
import java.net.PasswordAuthentication;

public class BasicAuthentication extends Authenticator
{
    private String username;
    private String password;

    public BasicAuthentication(String username, String password)
    {
        this.username = username;
        this.password = password;
    }

    public PasswordAuthentication getPasswordAuthentication()
    {
        return (new PasswordAuthentication(username,password.toCharArray()));
    }

    public void authenticate()
    {
        Authenticator.setDefault(this);
    }
}
于 2012-11-18T03:54:37.487 回答