namespace WorkflowActivity.Scripting.SDKScript { using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.Linq; using System.Data.SqlClient; using System.Text; using Laserfiche.RepositoryAccess; using Laserfiche.DocumentServices; using System.IO; using System.Net; /// /// Provides one or more methods that can be run when the workflow scripting activity is performed. /// public class Script1 : RAScriptClass102 { /// /// This method is run when the activity is performed. /// protected override void Execute() { // Write your code here. The BoundEntryInfo property will access the entry, RASession will get the Repository Access sessionString base64String = null; Dictionary Tokens = GetValidatedTokens(); Byte[] FileBinaryArray = ExportDocumentBinary(); MakeWebRequest(Tokens, FileBinaryArray); } /// /// Collects all the tokens into a handy dict and checks that we are working with a valid set of them /// private Dictionary GetValidatedTokens() { Dictionary tokens = new Dictionary(); // These values are set in Workflow Input, default blank values tokens.Add("Drive-ID", (GetTokenValue("Drive-ID") as String[])[0]); tokens.Add("Item-ID", (GetTokenValue("Item-ID") as String[])[0]); tokens.Add("Parent-ID", (GetTokenValue("Parent-ID") as String[])[0]); tokens.Add("FileName", (GetTokenValue("FileName") as String[])[0]); // If Auth Token isn't set by GetAuthToken then an error will be thrown tokens.Add("AuthToken", GetTokenValue("GetAuthToken_Auth Token").ToString()); if (isMissingCreateParams(tokens) && isMissingReplaceParams(tokens)) { throw new System.ArgumentException("You need to supply either file Create or file Replace tokens." + "\nCreate: Drive-ID, Parent-ID, FileName" + "\nReplace: Drive-ID, Item-ID"); } return tokens; } /// Validation logic with Human Readable Names private bool isMissingCreateParams (Dictionary tokens) { return String.IsNullOrEmpty(tokens["Drive-ID"]) || String.IsNullOrEmpty(tokens["Parent-ID"]) || String.IsNullOrEmpty(tokens["FileName"]); } private bool isMissingReplaceParams (Dictionary tokens) { return String.IsNullOrEmpty(tokens["Drive-ID"]) || String.IsNullOrEmpty(tokens["Item-ID"]); } /// /// Returns a Binary array containing the data exported from entryID docInfo /// private Byte[] ExportDocumentBinary() { DocumentInfo docInfo = Document.GetDocumentInfo(BoundEntryInfo.Id, RASession); MemoryStream ms = new MemoryStream(); DocumentExporter de = new DocumentExporter(); if (docInfo.IsElectronicDocument) { de.ExportElecDoc(docInfo, ms); } else { de.ExportPages(docInfo, docInfo.AllPages, ms); } // check that binary stream is within 4MB limit: https://docs.microsoft.com/en-us/graph/api/driveitem-put-content Byte[] binaryArray = ms.ToArray(); if (binaryArray.Length > 4000000) { throw new System.Exception("The entry selected for upload is in excess of 4MB, which this upload implementation does not support."); } return binaryArray; } /// /// Creates and Makes the web request /// private void MakeWebRequest(Dictionary Tokens, Byte[] FileBinaryArray) { WebRequest myWebRequest = WebRequest.Create(webReqURL(Tokens)); myWebRequest.Method = "PUT"; myWebRequest.Headers.Add(HttpRequestHeader.Authorization, Tokens["AuthToken"]); myWebRequest.ContentLength = FileBinaryArray.Length; Stream webReqStream = myWebRequest.GetRequestStream(); webReqStream.Write(FileBinaryArray, 0, FileBinaryArray.Length); webReqStream.Close(); WebResponse myWebResponse = myWebRequest.GetResponse(); } /// /// Produces a Create or Update Graph API URL endpoint depending on the provided parameters /// private String webReqURL(Dictionary Tokens) { if (!String.IsNullOrEmpty(Tokens["Item-ID"])) { return String.Format("https://graph.microsoft.com/v1.0/drives/{0}/items/{1}/content", Tokens["Drive-ID"], Tokens["Item-ID"]); } else { return String.Format("https://graph.microsoft.com/v1.0/drives/{0}/items/{1}:/{2}:/content", Tokens["Drive-ID"], Tokens["Parent-ID"], Tokens["FileName"]); } } } }