Blog

CMIS, SharePoint 2010, and File Checkins

by | Jun 3, 2011 | ECM Industry, Enterprise Content Management, SharePoint | 0 comments

A while back I wrote about checking out a SharePoint 2010 file using CMIS.  Getting checkout working excited me so much I forgot to try checking the file back in!

Turns out there are a few tricks.

We use Apache Chemistry as our CMIS client.  Chemistry’s checkin method allows you to provide a file content stream, such that the act of checking the file in also updates the file contents:

checkin(boolean major, Map<String,?> properties, ContentStream contentStream, String checkinComment)

However, sending a non-null contentStream to this method makes it fail!  You will get the famous “object reference not set to an instance of an object” message.  You must send null for this parameter.  So how do you update the file contents?  Right before the checkin, you can call the setContentStream method, as illustrated in the below code snippet.

Also, in the properties parameter, you must provide a property for the object name.  This is also illustrated in the below snippet.  You may provide other properties if you want, but if you don’t provide the object name, the checkin will fail.

The JUnit test case below illustrates how to connect to SP2010 via WSDL, find an object by path, check out the object, update its content stream, and check it back in.

import java.io.InputStream;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.chemistry.opencmis.client.api.Document;
import org.apache.chemistry.opencmis.client.api.ObjectId;
import org.apache.chemistry.opencmis.client.api.Repository;
import org.apache.chemistry.opencmis.client.api.Session;
import org.apache.chemistry.opencmis.client.api.SessionFactory;
import org.apache.chemistry.opencmis.client.bindings.CmisBindingFactory;
import org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl;
import org.apache.chemistry.opencmis.commons.PropertyIds;
import org.apache.chemistry.opencmis.commons.SessionParameter;
import org.apache.chemistry.opencmis.commons.data.ContentStream;
import org.apache.chemistry.opencmis.commons.enums.BindingType;
import org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException;
import org.apache.chemistry.opencmis.commons.impl.dataobjects.BindingsObjectFactoryImpl;
import org.apache.chemistry.opencmis.commons.spi.BindingsObjectFactory;
import org.junit.Test;

/**
*
* @author millerd
*/
public class SharePointWSDLTest
{

@Test
public void connectViaWsdl() throws Exception
{
String cwd = System.getProperty(“user.dir”);
System.out.println(“Working dir: ” + cwd);
// Default factory implementation of client runtime.
SessionFactory sessionFactory = SessionFactoryImpl.newInstance();
Map parameter = new HashMap();

// User credentials.
parameter.put(SessionParameter.USER, “some_user”);
parameter.put(SessionParameter.PASSWORD, “some_password”);

// relative file-based URL works fine so long as you give it the
// right relative path… ie. you have to know what the current
// working directory is.
String wsdl = “file:target/classes/acmv2-sharepoint.wsdl”;
// Connection settings.
parameter.put(SessionParameter.BINDING_TYPE, BindingType.WEBSERVICES.value());
parameter.put(SessionParameter.WEBSERVICES_ACL_SERVICE, wsdl);
parameter.put(SessionParameter.WEBSERVICES_DISCOVERY_SERVICE, wsdl);
parameter.put(SessionParameter.WEBSERVICES_MULTIFILING_SERVICE, wsdl);
parameter.put(SessionParameter.WEBSERVICES_NAVIGATION_SERVICE, wsdl);
parameter.put(SessionParameter.WEBSERVICES_OBJECT_SERVICE, wsdl);
parameter.put(SessionParameter.WEBSERVICES_POLICY_SERVICE, wsdl);
parameter.put(SessionParameter.WEBSERVICES_RELATIONSHIP_SERVICE, wsdl);
parameter.put(SessionParameter.WEBSERVICES_REPOSITORY_SERVICE, wsdl);
parameter.put(SessionParameter.WEBSERVICES_VERSIONING_SERVICE, wsdl);

parameter.put(SessionParameter.AUTHENTICATION_PROVIDER_CLASS,
CmisBindingFactory.STANDARD_AUTHENTICATION_PROVIDER);

// Create session.
Session session = null;

List repositories = sessionFactory.getRepositories(parameter);
for ( Repository rep : repositories )
{
// My test file is in the “Unapproved Files” repository
if ( “Unapproved Files”.equals(rep.getName()) )
{
session = rep.createSession();
Document doc = (Document)
session.getObjectByPath(“/2011/05/03/10/31/19/dave-sharepoint.wsdl”);
System.out.println(“Doc ID: ” + ( doc == null ? ” *NOT FOUND* ” : doc.getId()));
if ( doc != null )
{
try
{
ObjectId co = doc.checkOut();
System.out.println(“Checked out doc ID: ” + co.getId());
}
catch (CmisBaseException cbe)
{
System.out.println(“Exception checking out; must be ” +
“checked out already: ” + cbe.getMessage());
}

// create a new content stream to update the file contents
InputStream is = Thread.currentThread().
getContextClassLoader().getResourceAsStream(
“dave-sharepoint.wsdl”);

boolean majorVersion = true;
Map props = new HashMap();
// Object name MUST b eprovided
props.put(PropertyIds.NAME, “dave-sharepoint”);

BindingsObjectFactory bof = new BindingsObjectFactoryImpl();

ContentStream csNew = bof.createContentStream(
“dave-sharepoint.wsdl”,
BigInteger.valueOf(is.available()),
“text/plain”,
is);

// OPTIONAL: update the file content stream
doc.setContentStream(csNew, true);
// NOTE: DO NOT send a content stream in the checkIn call!
// the call will fail
ObjectId ci = doc.checkIn(
majorVersion,
props,
null,
“test checkin”);
System.out.println(“Object ID after checkin: ” + ci.getId());
}
}
}

}
}

Categories

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *