понеділок, 16 листопада 2009 р.

Java to Sharepoint integration. (Copy files solution)

So, this is my first publication :)  Last week i had a task to implement sharepoint integration lib and it was not so trivial task as i thought. But.. task completed and after hours of investigation i`d like to share my knowledge about HOW TO COPY FILES AND CREATE FOLDER FROM JAVA APPLICATION TO SHAREPOINT.

1) Let`s create new Maven project and add dependencies:


2) For copying files we`ll use sharepoint`s web services :Copy and for creating new folders : Lists. This services situated on http://yoursharepointurl/_vti_bin/Copy.asmx and http://yoursharepointurl/_vti_bin/Lists.asmx
I found a maven plugin which generate all java classes from wsdl file.


3) Save your wsdl files to src/main/wsdl folder and run maven goal generate-sources. Plugin will generate all java classes. Add these files to build path to Eclipse.

4) Now time is to create SharepointService.java class. Add constructor

public SharePointService(String serviceUrl, String userPassword, String userName) {
this.servicesUrl = serviceUrl;
this.userName = userName;
this.userPassword = userPassword;
this.listsService = initListService();
this.copyService = initCopyService();
5) add private methods to initialise listsService and copyService. In case if you have NTLM authentication you should look here : http://derekjmiller.spaces.live.com/blog/cns!A77124B9D0EE317F!142.entry

public CopySoap12Stub initCopyService() {
CopySoap12Stub stub;
CopyLocator locator = new CopyLocator();
try {
URL url = new URL(this.servicesUrl + COPY_SERVICE);
stub = (CopySoap12Stub) locator.getCopySoap12(url);
return stub;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
} catch (ServiceException e) {
return null;

and for lists service :
private ListsSoap12Stub initListService() {
if (this.listsService != null)
return this.listsService;

ListsSoap12Stub stub;
ListsLocator locator = new ListsLocator();
try {
URL url = new URL(this.servicesUrl + LISTS_SERVICE);
stub = (ListsSoap12Stub) locator.getListsSoap12(url);
return stub;
} catch (MalformedURLException e) {
} catch (ServiceException e) {
} catch (Exception ex) {
return null;
6) I added rootPath property - this is the document library on  where folders and file will be uploaded

private String rootPath;

public void setRootPath(String path) {
this.rootPath = path;

public String getRootPath() {
if (this.rootPath == null) {
} else
return this.rootPath;

7) Interface of our upload-method will be :  uploadDocument(String path, File file) where path - path where to upload file, relative to the RootPath  and file - simple java.io.File.
Bad news is thst copy service CANT create folders, just copy file. Good news - we can to this with Lists service.
Here the implementation:

private void makeNewFolder(String relativePath) throws SOAPException,
RemoteException {
UpdateListItemsUpdates updates;
String[] folderNames = relativePath.split("/");
if (folderNames.length == 0) {
.info("Path should have delimiters: example: ParentFolder/ChildFolder");
} else {
String currentFolder = folderNames[0];
for (int i = 0; i < folderNames.length; i++) {

if (i > 0) {
currentFolder += "/" + folderNames[i];
updates = new UpdateListItemsUpdates(BatchCreator
listsService.updateListItems(DEFAULT_ROOT_FOLDER, updates);

The main thing here is - updateListItems method . We need to pass 2 parameters there :
DEFAULT_ROOT_FOLDER - name of the list (name of the shared library, which we will update)
updates - Wrapper of MessageElement[] . We create it in our utill class BatchCreator in createNewFolderBatch method:

8) Ant the last - put it all together implementing uploadDocument(String path, File file) method:

public void uploadDocument(String path, File file)
throws FileAlreadyExistsException, SOAPException {
try {
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file));
byte[] bytes = new byte[bis.available()];
FieldInformation fieldInformation = new FieldInformation();
FieldInformationCollection infos = new FieldInformationCollection(
new FieldInformation[] { fieldInformation });


String sourceUrl = getServiceUrl() + "/" + getRootPath() + "/"
+ path + "/" + file.getName();
DestinationUrlCollection destinationUrls = new DestinationUrlCollection(
new String[] { sourceUrl });

UnsignedIntHolder copyIntoItemsResult = new UnsignedIntHolder(
new UnsignedInt());
CopyResultCollectionHolder results = new CopyResultCollectionHolder(
new CopyResultCollection(
new CopyResult[] { new CopyResult() }));

copyService.copyIntoItems(sourceUrl, destinationUrls, infos, bytes,
copyIntoItemsResult, results);
// error message always exists
String errorMessage = results.value.getCopyResult()[0]
if (errorMessage != null) {
throw new FileAlreadyExistsException("File already exists in directory");
} else {
logger.info("file " + file.getName()
+ " has been successfully uploaded to server" + "\n "
+ getServiceUrl() + "/" + getRootPath() + "/" + path
+ "/" + file.getName());

} catch (FileNotFoundException e) {
} catch (IOException e) {

This works for me and i hope will be work for you. ;)

4 коментарі:

  1. This is very impressive. I'm having a few issues with compilation (such as this signature not being found:

    FieldInformationCollection infos = new FieldInformationCollection( new FieldInformation[] { fieldInformation });

    Do you have the ability to post a zip file of this solution?

    Thanks a lot.

  2. unfortunately i`ve lost all sources, but as i remember FieldInformationCollection class should be generated by wsdl2java from sharepoint COPY-webservice http://msdn.microsoft.com/en-us/library/dd920449.aspx

  3. Thanks for the post. Uploaded the document but unable to see the uploaded document in the list. Any idea?

  4. Hi, Thanks for such informative post. I am trying to create a folder in sahrepoint. while trying that I got stuck on this line.

    updates = new UpdateListItemsUpdates(BatchCreator

    I didnt understand where did you get UpdateListItemsUpdates class from. Your post doesnot tell about this class and neither I could find it in Lists.asmx

    Please let how to pass the MessageElement[] in UpdateListItems()

    Thanks again