void SaveDocument(IDocumentInfo documentInfo, bool checkModificationTime)
    {
        long itemId  = ParseDocumentIdFromEditor(documentInfo.DocumentId);
        Item docItem = DataService.FindItemById(itemId);

        if (docItem == null || checkModificationTime && documentInfo.LastModifyTime < docItem.LastWriteTime)
        {
            return;
        }

        byte[] content = null;
        if (IsRichEditDocument(docItem))
        {
            RichEditDocumentInfo richEditDocument = (RichEditDocumentInfo)documentInfo;
            content = richEditDocument.SaveCopy(RichEditDocumentManager.GetFormat(docItem));
        }
        else if (IsSpreadsheetDocument(docItem))
        {
            SpreadsheetDocumentInfo spreadsheetDocument = (SpreadsheetDocumentInfo)documentInfo;
            content = spreadsheetDocument.SaveCopy(SpreadsheetDocumentManager.GetFormat(docItem));
        }
        else
        {
            throw new Exception("Incorrect document format.");
        }

        docItem.UpdateContent(content);
        DataService.SaveChanges();
    }
    public DocumentCommandResult TrySaveAsNewDocument(string documentName, out string newDocumentUrl)
    {
        newDocumentUrl = null;
        Item newDocument = new Item()
        {
            Name     = documentName,
            IsFolder = false
        };

        if (!IsDocumentEditingAllowed(newDocument))
        {
            return(DocumentCommandResult.NotSupportedFormat);
        }

        Item document = GetDocumentInCurrentFolder(documentName);

        if (document != null)
        {
            return(DocumentCommandResult.DocumentAlreadyExists);
        }

        newDocument.Owner          = DocumentsApp.User.CurrentUser;
        newDocument.ParentItem     = CurrentDocument.ParentItem;
        newDocument.CreationTime   = DateTime.UtcNow;
        newDocument.LastAccessTime = DateTime.UtcNow;
        newDocument.LastWriteTime  = DateTime.UtcNow;

        byte[]        newDocumentData;
        string        sourceDocIdForEditor = GetDocumentIdForEditor(CurrentDocument);
        IDocumentInfo sourceDocInfo        = DocumentManager.FindDocument(sourceDocIdForEditor);

        if (IsRichEditDocument(CurrentDocument))
        {
            RichEditDocumentInfo richEditDocInfo = sourceDocInfo as RichEditDocumentInfo;
            DevExpress.XtraRichEdit.DocumentFormat richEditFormat = GetRichEditDocumentFormat(newDocument);
            newDocumentData = richEditDocInfo.SaveCopy(richEditFormat);
        }
        else if (IsSpreadsheetDocument(CurrentDocument))
        {
            SpreadsheetDocumentInfo spreadsheetDocInfo = sourceDocInfo as SpreadsheetDocumentInfo;
            DevExpress.Spreadsheet.DocumentFormat spreadsheetFormat = GetSpreadsheetDocumentFormat(newDocument);
            newDocumentData = spreadsheetDocInfo.SaveCopy(spreadsheetFormat);
        }
        else
        {
            throw new Exception("Incorrect document format.");
        }
        newDocument.Content = DataService.CreateBinaryContent(newDocumentData);

        DataService.AddItem(newDocument);
        DataService.SaveChanges();
        string newDocumentPath = GetPathForDocumentInCurrentFolder(documentName);

        newDocumentUrl = GetDocumentEditorRequestUrl(newDocumentPath);
        return(DocumentCommandResult.OK);
    }