OutputSharePointDeviceProcessingScriptTemplate Field |
Default processing script template
Namespace: Seal.ModelAssembly: SealLibrary (in SealLibrary.dll) Version: 10.0.1.0+a44cd3a7f03c2bf3cdbd971a878c679e40bed08a
Syntaxpublic const string ProcessingScriptTemplate = "@using System.IO\n@using System.Net.Http\n@using System.Net.Http.Headers\n@using System.Text\n@using System.Text.Json\n@{\n //Upload the report result to a SharePoint Online document library using the Microsoft Graph API.\n //The authentication (client secret or certificate) is handled by the device with device.GetAccessToken().\n //The script can be modified for specific needs (e.g. setting metadata columns on the uploaded file).\n Report report = Model;\n ReportOutput output = report.OutputToExecute;\n var device = output.Device as OutputSharePointDevice;\n\n var resultFileName = (output.ZipResult ? Path.GetFileNameWithoutExtension(report.ResultFileName) + ".zip" : Path.GetFileNameWithoutExtension(report.ResultFileName) + Path.GetExtension(report.ResultFilePath));\n device.HandleZipOptions(report);\n\n //Destination path in the document library\n var remotePath = output.FileServerFolderWithSeparators + resultFileName;\n\n using (var client = new HttpClient())\n {\n //Authentication\n client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", device.GetAccessToken());\n\n //Resolve the drive of the document library\n var driveId = device.GetDriveId(client);\n\n //Create an upload session (works for any file size, conflict behavior taken from the device)\n var sessionUrl = string.Format("{0}/v1.0/drives/{1}/root:{2}:/createUploadSession", device.GraphUrlNormalized, driveId, device.EscapePath(remotePath));\n var sessionBody = "{\"item\":{\"@microsoft.graph.conflictBehavior\":\"" + device.ConflictBehavior.ToString().ToLower() + "\"}}";\n var sessionResponse = client.PostAsync(sessionUrl, new StringContent(sessionBody, Encoding.UTF8, "application/json")).Result;\n var sessionJson = sessionResponse.Content.ReadAsStringAsync().Result;\n if (!sessionResponse.IsSuccessStatusCode) throw new Exception("SharePoint upload session failed: " + sessionJson);\n var uploadUrl = JsonDocument.Parse(sessionJson).RootElement.GetProperty("uploadUrl").GetString();\n\n //Upload the file in chunks (the chunk size must be a multiple of 320 KB)\n var fileBytes = File.ReadAllBytes(report.ResultFilePath);\n const int chunkSize = 16 * 320 * 1024;\n for (int pos = 0; pos < fileBytes.Length; pos += chunkSize)\n {\n int size = Math.Min(chunkSize, fileBytes.Length - pos);\n var chunk = new ByteArrayContent(fileBytes, pos, size);\n chunk.Headers.ContentRange = new ContentRangeHeaderValue(pos, pos + size - 1, fileBytes.Length);\n var chunkResponse = client.PutAsync(uploadUrl, chunk).Result;\n if (!chunkResponse.IsSuccessStatusCode) throw new Exception("SharePoint upload failed: " + chunkResponse.Content.ReadAsStringAsync().Result);\n }\n }\n\n output.Information = report.Translate("Report result generated in '{0}'", remotePath);\n report.LogMessage("Report result generated in '{0}'", remotePath);\n}\n"Field Value
String
See Also