Sharepoint 내용 읽기 with Java : Tutorial
개발 / 2013. 10. 25. 11:18
이번 포스트는 Sharepoint 2007의 WSDL을 활용하여 내용을 읽고 쓰는 방법을 소개 해드리겠습니다.
Sharepoint 2010은 Rest API를 지원하기 때문에 wsdl보다는 쉽게 활용이 가능할 것 같습니다.
Tutorial : Sharepoint의 내용 읽기
1. WSDL file 다운로드
Web service WSDL 파일을 다운로드 받기 위해서 현재 사용중인 sharepoint site주소 뒤에 아래와 같이 입력합니다.
브라우저에 나온 WSDL을 PC에 파일로 저장하세요.
1 | http://sharepoint.com/_vti_bin/Lists.asmx?WSDL |
2. Stub Code Generate
Stub code생성을 위해 console창을 열어 JDK에서 제공하는 wsimport tool을 사용하여 하래와 같이 실행합니다.
1 | wsimport -p com.microsoft.sharepoint -keep -extension sharepoint.wsdl |
정상적으로 실행이 되었다면 실행한 folder에 stub code가 생성됩니다.
3. eclipse project 생성
eclipse 에서 새 프로젝트를 생성하시고, 생성된 stub code를 프로젝트에 import 합니다.
4. Authentication
Sharepoint web service에서 Authentication 방법입니다. 정상적으로 Auth가 되면 ListSoap Object가 리턴됩니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public static ListsSoap Authentication(String userName, String password) throws Exception { ListsSoap port = null; if (userName != null && password != null) { Lists service = new Lists(); port = service.getListsSoap(); ((BindingProvider) port).getRequestContext() .put(BindingProvider.USERNAME_PROPERTY, userName); ((BindingProvider) port).getRequestContext() .put(BindingProvider.PASSWORD_PROPERTY, password); } else { throw new Exception("Fail: Authentication."); } return port; } |
여기서 혹시 Authentication failure 등의 에러가 발생한다면, IIS서버 Authentication이 필요합니다.
IIS서버의 Authentication Type이 Basic과 NTLM 두 가지 모두를 지원한다고 하는데 자세한건 패스.
아래 방법은 NTLM Type으로 로그인 하는 방법입니다.
IIS서버의 Authentication Type이 Basic과 NTLM 두 가지 모두를 지원한다고 하는데 자세한건 패스.
아래 방법은 NTLM Type으로 로그인 하는 방법입니다.
1 2 3 4 5 6 7 8 9 10 | public static void NTLMAuth(final String userName, final String password) { if (userName != null && password != null) { Authenticator.setDefault(new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userName, password.toCharArray()); } }); } } |
5. Sharepoint list 읽기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public static void readSharePointList(ListsSoap port, String listName, ArrayList<string> listColumnNames, String rowLimit) throws Exception { if (port != null && listName != null && listColumnNames != null && rowLimit != null) { try { String viewName = ""; GetListItems.ViewFields viewFields = null; GetListItems.Query query = null; GetListItems.QueryOptions queryOptions = null; String webID = ""; GetListItemsResponse.GetListItemsResult result = port.getListItems(listName, viewName, query, viewFields, rowLimit, queryOptions, webID); Object listResult = result.getContent().get(0); System.out.print(listResult); } catch (Exception ex) { throw new Exception("Exception :" + ex.toString() + "\n"); } } } </string> |
6.Test Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public static void main(String[] args) { String domain = "Domain\\"; String userName = "userName"; String password = "Password"; httpService.NTLMAuth(domain+userName, password); try { ArrayList<string> listColumnNames = new ArrayList<string>(); listColumnNames.add("Title"); listColumnNames.add("Comments"); ListsSoap port = sharepointService.Authentication(domain+userName, password); sharepointService.readSharePointList(port, "Issue Tracking", listColumnNames, "100"); } catch (Exception e) { e.printStackTrace(); } } </string></string> |
여기까지 문제 없이 오셨다면 나머지는 혼자서 충분히 하실수 있으리라 생각합니다.
출처: http://xtech.tistory.com/entry/Sharepoint-%EB%82%B4%EC%9A%A9-%EC%9D%BD%EA%B8%B0-with-Java-Tutorial
