Blog

SharePoint 2010 – REST Atom Service and JAVA, part 2

by | May 26, 2011 | Java, Process, SharePoint, Solutions | 4 comments

This is the second of two blogs where I demonstrate how to use the openCMIS library from a JAVA client to connect to a CMIS enabled SharePoint site through the REST Atom service of SharePoint.

My previous blog “SharePoint 2010 – REST Atom Service.” provided information on how to connect to a SharePoint site and examined some of the SharePoint site’s capabilities. In this blog I will show how to use the openCMIS library and REST Atom Service to: 1) Access and Create Folders; 2) Iterate over SharePoint folders; and 3) Add documents to a SharePoint folder. This blog builds on the previous one, so reference it if you aren’t able to follow the code in this one.

Accessing and Creating SharePoint Folders

The following exmple will show you how to get and create folder objects. An additional class library is imported for accessing folders and another for getting a CMIS object by path.

…
import org.apache.chemistry.opencmis.client.api.Folder;
import org.apache.chemistry.opencmis.client.api.CmisObject;
…
Folder folder = session.getRootFolder();
String folderprops=twolines + "ROOT FOLDER:" + newline +
tab + "ID: " + folder.getId().toString() + newline +
tab + "name: " + folder.getName() + newline +
tab + "path: " + folder.getPath();//create new folder in the root folder
Map properties = new HashMap();
properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:folder");
properties.put(PropertyIds.NAME, “mynewfolder”);
Folder newFolder = folder.createFolder(properties);//Check properties
String folderprops=twolines + "NEW FOLDER:" + newline +
tab + "ID: " + newFolder.getId().toString() + newline +
tab + "name: " + newFolder.getName() + newline +
tab + "path: " + newFolder.getPath();//Get another folder by path that already exists; CmisObject does not have a “getPath” method,
//so convert it to a CMIS folder
CmisObject cmisObj = session.getObjectByPath("/2011");
Folder fo = (Folder) cmisObj;//Check properties
folderprops=twolines + "2011 FOLDER:" + newline +
tab + "ID: " + fo.getId().toString() + newline +
tab + "name: " + fo.getName() + newline +
tab + "path: " + fo.getPath();

 

Results:

ROOT FOLDER:
ID: -1
name: Shared Documents
path: /NEW FOLDER:
ID: 87
name: mynewfolder
path: /mynewfolder2011 FOLDER:
ID: 83
name: 2011
path: /2011

 

Things to note:

1) The path is relative to the Repository, so the “Shared Documents” library could be accessed at its top level with the path “/”.

Iterating over SharePoint Folders

The following exmple will show you how to iterate over the folder objects. An additional class library is imported iterating over CMIS objects.
 

…
import org.apache.chemistry.opencmis.client.api.ItemIterable;
…
FileList.setText("FOLDER (Name; ID)" + newline + tab + "CHILDREN (Name; ID)");folder = session.getRootFolder();
FileList.append(twolines+folder.getName()+"; " + folder.getId());
tabs = tab;
this.getKids(folder);
}private void getKids(Folder folder) {
//recursive method to go through libraryItemIterable children = folder.getChildren();
for ( CmisObject obj : children )
{
if(obj.getBaseTypeId().toString()!="CMIS_FOLDER")
{
FileList.append(newline+tabs+obj.getName()+"; " + obj.getId());
}
}
children = folder.getChildren();
for ( CmisObject obj : children )
{
if(obj.getBaseTypeId().toString()=="CMIS_FOLDER")
{
folder = (Folder) obj;
FileList.append(newline+tabs+"/" + folder.getName()+"; " + folder.getId());
tabs = tabs + tab;
this.getKids(folder);
}
}
}

Results:

 FOLDER (Name; ID)
CHILDREN (Name; ID)Shared Documents; -1
/2011; 83
/05; 84
/03; 85

Things to note:

1) The root ID of SharePoint libraries is always “-1”
2) You can detect and display properties of files within the folders by using the base type “CMIS_DOCUMENT”.
3) The document ID produced from CMIS_DOCUMENT is not the one you see exposed in the SharePoint GUI, but it is the one you use to access a document in CMIS.

Adding a Document to a Folder

Putting documents into a SharePoint folder is typical operation you would need to do. I’ve shown how you get and create a folder, so now you have a place to put your file. A number of additional libraries are needed for this operation.
 

…
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.io.ByteArrayInputStream;
import org.apache.chemistry.opencmis.commons.data.ContentStream;
import org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl;
import org.apache.chemistry.opencmis.client.api.Document;
…
//details of the fileChooser not shown here. The goal is to get an InputStream
File file = fileChooser.getSelectedFile();
InputStream is = new FileInputStream(file);
long length = file.length();
BigInteger bi = BigInteger.valueOf(length);
ContentStream contentStream = new ContentStreamImpl(file.getName(), bi, "text/plain", is);
Map properties = new HashMap();
properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document");
properties.put(PropertyIds.NAME, file.getName());folder = session.getRootFolder();
Document doc = folder.createDocument(properties, contentStream, VersioningState.MINOR);

Things to note:

1) This code shows the essentials, but actual implementation should provide for the various exceptions that could arise, CmisConstraintException and IOException for example.
2) New ContentStreamImpl requires a BigInteger, hence the conversion of the file length from long.
3) VersioningState can be MAJOR, MINOR, NONE, or CHECKEDOUT. Whichever you use must match the versioning enabled in the SharePoint library.

I have shown how you can use the Chemistry openCMIS library and REST Atom Service to:
1) Access and Create Folders; 2) Iterate over SharePoint folders; and 3) Add documents to a SharePoint folder.

I hope this will “de-mystify” how to perform common DM operations in SharePoint using the CMIS standard!


Categories

4 Comments

  1. Sid Hazarika

    Thanks a lot for the postings.. I tried the create folder API. but am getting CmisUnauthorizedException back.

    I am able to successfully connect to Sharepoint, create a session, call the get API (like getRepositoryInfo, getRootFolderId etc).. but when I try to do any of the create API’s , I get the CmisUnauthorizedException. Is there a Sharepoint setting preventing me from creating anything.. the user I am passing is set in the Sharepoint Owners group with full control.

    Any help will be highly appreciated.

    thanks

    Reply
  2. Tim Lisko

    Sid: It has been awhile since your post. Have you solved the issue? I am not clear about what specifically you are trying to do. Are you trying to create a folder within the document library?

    Tim

    Reply
  3. Tim Lisko

    Sid:

    One thing that will also create the 404 error is if the spcmis.wsp has not been deployed to the site you are trying to access.

    Hope that helps!

    Tim

    Reply
  4. Ade

    Many thanks for the great explanation!
    I ran into an error while creating a folder in SharePoint, not sure if you’ve come across such issue before.
    “CmisRuntimeException: Internal Server Error”

    Any help will be greatly appreciated.

    Thanks!

    Reply

Submit a Comment

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