public async Task <ActionResult> SyncDocumentLibrary(string documentlibraryid)
        {
            try
            {
                if (documentlibraryid != null)
                {
                    Models.DocumentLibrary docLib = Repository.DocumentLibrary.Get(Convert.ToInt64(documentlibraryid));

                    // delete files from Azure
                    AzureBlobStorage azureStorage = new AzureBlobStorage();
                    string           error        = await azureStorage.DeleteDocumentLibraryDocumentsAsync(documentlibraryid, false);

                    // delete documents for this document library from db
                    List <Models.Document> documents = Repository.Documents.GetAllFromDocumentLibrary(Convert.ToInt64(documentlibraryid));

                    foreach (Document d in documents)
                    {
                        var retVal = Repository.Documents.Delete(d);
                    }

                    // download documents from SharePoint to Azure Storage... this might take a while if files are large in size or number
                    SharePoint      sp = new SharePoint();
                    List <Document> syncedDocuments = await sp.DownloadFilesToAzure(docLib);

                    // Save documents that were downloaded from SharePoint and saved to Azure Blob Storage to SQL db
                    foreach (Document d in syncedDocuments)
                    {
                        long docId = Repository.Documents.Save(d);
                    }

                    // return successful response
                    return(Json(new
                    {
                        Success = true,
                        DocumentLibraryId = Convert.ToInt64(documentlibraryid)
                    }, JsonRequestBehavior.AllowGet));
                }
            }
            catch (Exception ex)
            {
                return(Json(new
                {
                    Success = false,
                    message = "An internal error occurred - please try again.  If this error persists, please contact your system administrator."
                }, JsonRequestBehavior.AllowGet));
            }

            return(Json(new
            {
                Success = false,
                message = "documentlibraryid is required."
            }, JsonRequestBehavior.AllowGet));
        }
        public async Task <ActionResult> SaveDocumentLibrary(Models.DocumentsViewModel input)
        {
            try
            {
                if ((input != null) && (input.NewSPDocumentLibrary != null) && (TryValidateModel(input)) && (ModelState.IsValid))
                {
                    var docLib = input.NewSPDocumentLibrary;

                    // Save document library to db
                    docLib.DocumentLibraryId = Repository.DocumentLibrary.Save(docLib);

                    // Copy all documents from document library to Azure storage.. this may take a while if the files are large or there are many files...
                    SharePoint      sp        = new SharePoint();
                    List <Document> documents = await sp.DownloadFilesToAzure(docLib);

                    // Save documents that were downloaded from SharePoint and saved to Azure Blob Storage to SQL db
                    if (documents != null)
                    {
                        foreach (Document d in documents)
                        {
                            long docId = Repository.Documents.Save(d);
                        }
                    }
                    // Return successful response
                    return(Json(new
                    {
                        Success = true,
                        DocumentLibraryId = docLib.DocumentLibraryId
                    }, JsonRequestBehavior.AllowGet));
                }
            }
            catch (Exception ex)
            {
                return(Json(new
                {
                    Success = false,
                    message = "An internal error occurred - please try again.  If this error persists, please contact your system administrator",
                }, JsonRequestBehavior.AllowGet));
            }

            return(Json(new
            {
                Success = false,
                message = "Doc lib was not saved - enter required values.",
            }, JsonRequestBehavior.AllowGet));
        }