You are here

You are here

Versioning an Existing Document

Creating a version of an existing document in the API is similar to uploading a new document.  The document must be chunked to SpringCM for a version in the same way that a new upload is done.  However, with an existing document, the document is checked out first and the DocumentCreateCheckin method is called to complete the transaction instead of DocumentCreateCommitDocumentCreateCheckin give the option to keep the document checked out after creating a new version.

Vertical Tabs

c#
string localPathAndFileName = "<Local file system path and file name for downloaded file>";
string documentToVersionId = "<Document Id retrieved by other method calls>";
DocumentCreateBeginResult dcbr = null;
 
try
{
	using (Stream localFileToBeUploaded = new MemoryStream(File.ReadAllBytes(localPathAndFileName)))
	{
		//Get the SCMDocument of the document we want to version and check it out if its not already
		SCMDocument document = springCMService.DocumentGetById(token, documentToVersionId, false);
		if(String.IsNullOrEmpty(document.CheckedOutUserId))
		{
			springCMService.DocumentCheckout(token,documentToVersionId);
		}
 
		//Here we upload, in most cases, this code should always be used exactly as shown
		dcbr = springCMService.DocumentCreateBeginWithFolder(token, document, null);
 
		//Always use the chunk size from the API, do not set your own
		int chunkSize = dcbr.ChunkSize;
		int index = 1;
 
		// Upload a chunk of the file at a time, looping until we are done
		while (true)
		{
			//Calculate the next chunk to upload
			//We cast to a long to accomodate for large files
			long offset = (long)(index - 1) * chunkSize;
			long remaining = localFileToBeUploaded.Length - offset;
 
			//Stop processing, at the end of the file 
			if (remaining < 1)
			{
				break;
			}
 
			//If the remaining is less then the chunksize, send it, otherwise send a full chunk
			int length = (int)(remaining >= chunkSize ? chunkSize : remaining);
			byte[] buffer = new byte[length];
 
			//Ok, read the next chunk and send it
			//The FileIdentifier tells the API what file we are uploading
			localFileToBeUploaded.Read(buffer, 0, length);
			springCMService.DocumentCreateUploadChunk(token, dcbr.FileIdentifier, index, length, buffer, null);
 
			//Increment chunk index 
			index++;
		}
 
		//All chunks have been uploaded, time to make the final commit call
		//A new SCMDocument is returned that is updated
		document = springCMService.DocumentCreateCheckin(token, dcbr.FileIdentifier,documentToVersionId, false);
 
		Console.WriteLine("Document versioned successfully.");
	}
}
catch (Exception ex)
{
	Console.WriteLine(ex.Message);
 
	//Cancel the upload if we hit an exception
	if (dcbr != null)
		springCMService.DocumentCreateCancel(token, dcbr.FileIdentifier);
}
java
String localPathAndFileName = "<Local file system path and file name for downloaded file>";
String documentToVersionId = "<Document Id retrieved by other method calls>";
DocumentCreateBeginResult dcbr=null;
FileInputStream localFileToBeUploaded=null;
 
try
{
	File file = new File(localPathAndFileName); 
	localFileToBeUploaded = new FileInputStream(file);
 
	//Get the SCMDocument of the document we want to version and check it out if its not already
	SCMDocument document = springCMService.documentGetById(token, documentToVersionId, false);
	if(StringUtils.isEmpty(document.getCheckedOutUserId()))
	{
		springCMService.documentCheckout(token,documentToVersionId);
	}
 
	//Here we upload, in most cases, this code should always be used exactly as shown
	dcbr = springCMService.documentCreateBeginWithFolder(token, document, null); 
 
	//Always use the chunk size from the API, do not set your own
	int chunkSize = dcbr.getChunkSize(); 
	int index = 1; 
 
	// Upload a chunk of the file at a time, looping until we are done
	while (true) 
	{ 
		//Calculate the next chunk to upload
		//We cast to a long to accommodate for large files
		long offset = (long)(index-1)*chunkSize; 
		long remaining = file.length() - offset; 
 
		//Stop processing, at the end of the file 
		if (remaining < 1)
		{
			break;
		}
 
		//If the remaining is less then the chunksize, send it, otherwise send a full chunk
		int length = (int) (remaining >= chunkSize ? chunkSize : remaining); 
		byte[] buffer = new byte[length]; 
 
		//Ok, read the next chunk and send it
		//The FileIdentifier tells the API what file we are uploading
		localFileToBeUploaded.read(buffer, 0, length); 
		springCMService.documentCreateUploadChunk(token, dcbr.getFileIdentifier(), index, length, buffer, null);
 
		//Increment chunk index 
		index++; 
	} 
 
	//All chunks have been uploaded, time to make the final commit call
	//A new SCMDocument is returned that is updated with the Id
	//Here additional properties can be added as well before commit such as Description, Metadata, etc.
	document = springCMService.documentCreateCheckin(token, dcbr.getFileIdentifier(), documentToVersionId,false); 
 
	System.out.println("Document versioned successfully.");
}
catch(Exception ex)
{
	ex.printStackTrace(); 
	springCMService.documentCreateCancel(token, dcbr.getFileIdentifier());
}
finally
{
	if(localFileToBeUploaded!=null) 
	{
		localFileToBeUploaded.close();
	}
}