Close a Document
The Spreadsheet does not close an open document when you open another document or when a user leaves a page that includes the control/document. The server’s memory stores each open document until the DocumentManager hibernates the document. When hibernation is disabled, all documents remain in the storage until the web server restarts.
Call the CloseAllDocuments() method to close all open documents. The CloseDocument(String) method allows you to close a specific document. The example below illustrates how to close all documents whose identifiers contain yesterday’s date:
<div id="button-close"></div>
<script type="text/javascript">
$("#button-close").dxButton({
text: "Close the oldest document",
onClick: function (e) {
var spreadsheetState = spreadsheet.getSpreadsheetState();
$.ajax({
type: 'POST',
url: '@Url.Action("CloseYesterdayDocuments")',
});
}
});
</script>
@(Html.DevExpress()
.Spreadsheet("spreadsheet")
// ...
)
public void CloseYesterdayDocuments() {
var documentInfoCollection = DocumentManager.GetAllDocuments();
string date = DateTime.Today.AddDays(-1).ToString("dd-MM-yyyy");
foreach (SpreadsheetDocumentInfo documentInfo in documentInfoCollection)
if (documentInfo.DocumentId.Contains(date))
DocumentManager.CloseDocument(documentInfo.DocumentId);
}
When you close a document, the DocumentManager applies the change for all users. This can generate a NullReferenceException
and “Your session has expired” errors. To avoid these errors/exceptions, you should only close unshared documents.