aspose file tools*
The moose likes Web Services and the fly likes Uploading a document to SharePoint using Java and Axis2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "Uploading a document to SharePoint using Java and Axis2" Watch "Uploading a document to SharePoint using Java and Axis2" New topic
Author

Uploading a document to SharePoint using Java and Axis2

Tom Carey
Greenhorn

Joined: Oct 29, 2009
Posts: 3
I'm encountering a problem uploading a modified document back into SharePoint 2007. I'm using Axis2 1.5 and Java.

1) I've successfully checked out the Word doc using the Lists webservice, which marks the doc in SharePoint as checked out by me.
2) I've successfully retrieved the actual document using the Copy webservice.
3) After making changes locally, I'm attempting to upload the document back into SharePoint using the copyIntoItems method in the Copy webservice. This is where the problem occurs using the following code:

CopyStub stub = new CopyStub();

Options options = stub._getServiceClient().getOptions();
options.setProperty(HTTPConstants.AUTHENTICATE, getAuthenticator());
// If this next statement is uncommented, the call throws an exception with HTTP/1.1 415 Unsupperted Media Type.
//options.setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);

CopyStub.CopyIntoItems copyIntoItemsRequest = (CopyStub.CopyIntoItems) getTestObject(CopyStub.CopyIntoItems.class);

DestinationUrlCollection destUrlCollection = new DestinationUrlCollection();
destUrlCollection.addString(docUriSharePoint);
copyIntoItemsRequest.setDestinationUrls(destUrlCollection);

try
{
File file = new File(docUriLocal);
FileDataSource fileDataSource = new FileDataSource(file);
DataHandler dataHandler = new DataHandler(fileDataSource);

copyIntoItemsRequest.setStream(dataHandler);

// If I call getItems(..) first (not shown) and get the FieldInformationCollection , referenced as "fic", for this document,
// and uncomment this code, a different error occurs (shown further down in this post)

//FieldInformationCollection fic = itemResponse.getFields();
//copyIntoItemsRequest.setFields(fic);

CopyStub.CopyIntoItemsResponse copyIntoItemsResponse = stub.copyIntoItems(copyIntoItemsRequest);

log.debug("Was getCopyIntoItems() call successful? : " + copyIntoItemsResponse.localResultsTracker);

// debug info suppressed
}
catch (Throwable t)
{
log.error("Exception occured", t);
}
finally
{
stub.cleanup();
}

With the code as is above , I get the following error listed within the xml response (shown in the log):

2009-10-29 14:52:18,298 DEBUG [httpclient.wire.content] << "<?xm"
2009-10-29 14:52:18,299 DEBUG [httpclient.wire.content] << "l version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><CopyIntoItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/"><CopyIntoItemsResult>0</CopyIntoItemsResult><Results><CopyResult ErrorCode="Unknown" ErrorMessage="Object reference not set to an instance of an object. " DestinationUrl="http://<server_name>/tv_intranet/Shared Documents/Projects/DocMgr - SharePoint Integration/TV_Scope DocMgr-SharePoint Integration.doc" /></Results></CopyIntoItemsResponse></soap:Body></soap:Envelope>"

If I uncomment the two FieldInformationCollection statements , I get a different error in the response:

2009-10-29 14:58:07,816 DEBUG [httpclient.wire.content] << "<?xm"
2009-10-29 14:58:07,817 DEBUG [httpclient.wire.content] << "l version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><CopyIntoItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/"><CopyIntoItemsResult>0</CopyIntoItemsResult><Results><CopyResult ErrorCode="Unknown" ErrorMessage="Value does not fall within the expected range. " DestinationUrl="http://pricli025/tv_intranet/Shared Documents/Projects/DocMgr - SharePoint Integration/TV_Scope DocMgr-SharePoint Integration.doc" /></Results></CopyIntoItemsResponse></soap:Body></soap:Envelope>"

Finally, if I uncomment the code that turns on MTOM , I get an exception:

[[17~2009-10-29 15:04:49,436 ERROR [com.tanval.extsvc.service.cms.sharepoint.lists.ListsTest] Exception occured
org.apache.axis2.AxisFault: Transport error: 415 Error: Unsupported Media Type
at org.apache.axis2.transport.http.HTTPSender.handleResponse(HTTPSender.java:295)
at org.apache.axis2.transport.http.HTTPSender.sendViaPost(HTTPSender.java:190)
at org.apache.axis2.transport.http.HTTPSender.send(HTTPSender.java:75)
at org.apache.axis2.transport.http.CommonsHTTPTransportSender.writeMessageWithCommons(CommonsHTTPTransportSender.java:389)
at org.apache.axis2.transport.http.CommonsHTTPTransportSender.invoke(CommonsHTTPTransportSender.java:222)
at org.apache.axis2.engine.AxisEngine.send(AxisEngine.java:435)
at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:402)
at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:229)
at org.apache.axis2.client.OperationClient.execute(OperationClient.java:165)
at com.tanval.extsvc.service.cms.sharepoint.copy.CopyStub.copyIntoItems(CopyStub.java:357)
at com.tanval.extsvc.service.cms.sharepoint.copy.CopyTest.testcopyIntoItems(CopyTest.java:177)
...

These generic error messages make it difficult to resolve. Hopefully someone has experienced this and has the answer.

Thanks,
Tom
Tom Carey
Greenhorn

Joined: Oct 29, 2009
Posts: 3
Updated 11/5 with my own discoveries....

I'm encountering a problem uploading a modified document back into SharePoint 2007. I'm using Axis2 1.5 and Java.

1) I've successfully checked out the Word doc using the Lists webservice, which marks the doc in SharePoint as checked out by me.
2) I've successfully retrieved the actual document using the Copy webservice.
3) After making changes locally, I'm attempting to upload the document back into SharePoint using the copyIntoItems method in the Copy webservice.

CopyStub stub = new CopyStub();

Options options = stub._getServiceClient().getOptions();
options.setProperty(HTTPConstants.AUTHENTICATE, getAuthenticator());
// If this next statement is uncommented, the call throws an exception with HTTP/1.1 415 Unsupported Media Type.
//options.setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);

CopyStub.CopyIntoItems copyIntoItemsRequest = (CopyStub.CopyIntoItems) getTestObject(CopyStub.CopyIntoItems.class);

DestinationUrlCollection destUrlCollection = new DestinationUrlCollection();
// 11/5/2009 The following needs to be a relative URI. Don't include http://<domain>.
// This will eliminate the error:Object reference not set to an instance of an object.
destUrlCollection.addString(docUriSharePointRelative);
copyIntoItemsRequest.setDestinationUrls(destUrlCollection);

// 11/5/2009 Even though the sourceUrl is not used since we're passing in a datahandler, it still needs to be valued.
// I've added the below statement and this error goes away: Value does not fall within the expected range.
copyIntoItemsRequest.setSourceUrl(docUriSharePointRelative);

try
{
File file = new File(docUriLocal);
file.lastModified();
FileDataSource fileDataSource = new FileDataSource(file);
DataHandler dataHandler = new DataHandler(fileDataSource);

copyIntoItemsRequest.setStream(dataHandler);

CopyStub.CopyIntoItemsResponse copyIntoItemsResponse = stub.copyIntoItems(copyIntoItemsRequest);

log.debug("Was getCopyIntoItems() call successful? : " + copyIntoItemsResponse.localResultsTracker);

// debug info suppressed
}
catch (Throwable t)
{
log.error("Exception occured", t);
}
finally
{
stub.cleanup();
}

11/5 - With the code as is above , I no longer get any errors. Unfortunately, whether the document already exists or doesn't exist in SharePoint, SharePoint is not updated. Does anyone know what can cause this? If I can get this resolved, I'll be done with this task.

The xml response looks like:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body><CopyIntoItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<CopyIntoItemsResult>0</CopyIntoItemsResult>
<Results>
<CopyResult ErrorCode="Success" DestinationUrl="/tv_intranet/Shared Documents/Projects/DocMgr - SharePoint Integration/TV_Scope_doc_FieldInformation.txt" />
</Results>
</CopyIntoItemsResponse>
</soap:Body>
</soap:Envelope>"

For extra credit, as mentioned above, the statement: options.setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);
will cause the following error . I believe the data is sent binary and would expect SharePoint to have a problem since it supports Base64 encoding but this appears to be a HTTP error.

[[17~2009-10-29 15:04:49,436 ERROR [com.tanval.extsvc.service.cms.sharepoint.lists.ListsTest] Exception occured
org.apache.axis2.AxisFault: Transport error: 415 Error: Unsupported Media Type
at org.apache.axis2.transport.http.HTTPSender.handleResponse(HTTPSender.java:295)
at org.apache.axis2.transport.http.HTTPSender.sendViaPost(HTTPSender.java:190)
at org.apache.axis2.transport.http.HTTPSender.send(HTTPSender.java:75)
at org.apache.axis2.transport.http.CommonsHTTPTransportSender.writeMessageWithCommons(CommonsHTTPTransportSender.java:389)
at org.apache.axis2.transport.http.CommonsHTTPTransportSender.invoke(CommonsHTTPTransportSender.java:222)
at org.apache.axis2.engine.AxisEngine.send(AxisEngine.java:435)
at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:402)
at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:229)
at org.apache.axis2.client.OperationClient.execute(OperationClient.java:165)
at com.tanval.extsvc.service.cms.sharepoint.copy.CopyStub.copyIntoItems(CopyStub.java:357)
at com.tanval.extsvc.service.cms.sharepoint.copy.CopyTest.testcopyIntoItems(CopyTest.java:177)
...

Thanks,
Tom
Tom Carey
Greenhorn

Joined: Oct 29, 2009
Posts: 3
The part I mentioned previously about the destinationUrl being relative is incorrect. In reviewing the API documentation, it is supposed to be an absolute url, like http://domain/site/Share Documents/some.doc. When I used this I received an error:

ErrorCode='DestinationInvalid'
ErrorMessage='Cannot create an item at the requested destination. Verify that the folder exists and that you have permission to edit in it.'

But that was because the sourceURL was the same as the destinationURL. As long as the sourceURL differs, the document is updated in SharePoint.

Here is a final working copy of code.
Oleg Dm
Greenhorn

Joined: Nov 19, 2009
Posts: 2
Thanks for the topic, it helps me a lot.
Here is my solution: Copy files to sharepoint document library
blue fish
Greenhorn

Joined: Jul 21, 2011
Posts: 1
Hello Tom,
Came across your page on "sharepoint 2007 java axis2 upload" search and you said you were able to successfully resolved the issue and upload local file to sharepoint with java and axis2.
I'm trying to do the same thing, after resolving the error "Object reference not set to an instance of an object", credit to your post at javaranch, thank you very much!

Now my upload return code shows success,
[main 2011-07-21 04:58:56,516] INFO com.my.sharepoint.client.SharePointCopyClientAPI - desUrlocuments/testfolder1/test1.pdf,
errorCode:Success, errorMsg:null
[main 2011-07-21 04:58:56,516] INFO com.my.sharepoint.client.SharePointCopyClientAPITest - ======END testCopyIntoItems() =======

but when I logged into sharepoint, I don't see the file there; do you have any ideas?
Also back when you were doing your upload, did you logged into sharepoint to see if the file really exists?

Appreciate any feedback
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Uploading a document to SharePoint using Java and Axis2