A better SharePoint Connector – part 4: why is she so hard to get?

The development of the SharePoint connector is coming along.

At the moment I’m working on adding files to a SharePoint document library using REST. SharePoint is a tough lady who’s playing hard to get!

*Did I hear: CHALLENGE?*

Adding a document usign the REST interface involves multiple REST calls. SharePoint does have to know all kinds of information, in particular the FormDigest –  a security hash needed to validate the request. Fortunatelly the FormDigest can be retrieved from SharePoint using a REST call (method POST):  http://[servername]/_api/contextinfo

The FormDigest value returned looks like this:

“0x7A999567B741EE8EA9E737BD9F64CB30EB471B2DEB15EE60EF7FC2203536724ADACF2C868C56713EDFE3D29C04E2E44952C666DCD64D0E7C8999A83590148780,09 Jan 2015 21:17:51 -0000″

At the moment, files can be added to SharePoint using the following lines of code (the AddFile call will be extended with an additional parameter to pass the meta data).

byte[] data = File.ReadAllBytes(@"C:\development\testsolution.zip");
// set the site Url
string siteUrl = "http://SharePoint:8080/sites/bas";
string docLib = "http://SharePoint:8080/sites/bas/shared documents";

// Connect to the local environment
SharePointConnector cls = new SharePointConnector(siteUrl);
Guid id = cls.AddFile("test.zip", docLib, data);

Once the docment is added to SharePoint, the SharePoint REST API returns a JSON blob containing the meta data of the document, from that meta data the document ID is retrieved querying the ContentTag. The document id will be needed later on to delete the document from SharePoint.

Next task is to send the meta data along with the document. The AddFile() method needs also to be extended with a check on duplicate documents. If a document name already exists in the library, then the document needs to be renamed.

A nice task for the weekend…