4

ここで、共有ポイントのリストを読み取ろうとしています。このために、私は次のことを行いました: sharepointsite.com/_vti_bin/Lists.asmx?WSDL のような URL から取得した 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

同じ問題が発生しました。オーセンティケーターを試してみてください。

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 に答える