public static async Task <TextAnalyzeOCRResult> GetAzureTextAnalyzeOCR(byte[] myFileBytes) { TextAnalyzeOCRResult resultReturn = new TextAnalyzeOCRResult(); HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", ConfigurationManager.AppSettings["azVisionApiServiceKey"]); string requestParameters = "language=unk&detectOrientation=true"; string uri = ConfigurationManager.AppSettings["azVisionApiOcrEndpoint"] + "?" + requestParameters; string contentString = string.Empty; HttpResponseMessage response; using (ByteArrayContent content = new ByteArrayContent(myFileBytes)) { content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); response = await client.PostAsync(uri, content); contentString = await response.Content.ReadAsStringAsync(); resultReturn = JsonConvert.DeserializeObject <TextAnalyzeOCRResult>(contentString); return(resultReturn); } }
static void GetChanges(ClientContext SPClientContext, string ListId, ILogger log) { Web spWeb = SPClientContext.Web; List myList = spWeb.Lists.GetByTitle(ConfigurationManager.AppSettings["whListName"]); SPClientContext.Load(myList); SPClientContext.ExecuteQuery(); ChangeQuery myChangeQuery = GetChangeQueryNew(ListId); var allChanges = myList.GetChanges(myChangeQuery); SPClientContext.Load(allChanges); SPClientContext.ExecuteQuery(); foreach (Change oneChange in allChanges) { if (oneChange is ChangeItem) { int myItemId = (oneChange as ChangeItem).ItemId; log.LogInformation($"---- Changed ItemId : " + myItemId); ListItem myItem = myList.GetItemById(myItemId); Microsoft.SharePoint.Client.File myFile = myItem.File; ClientResult <System.IO.Stream> myFileStream = myFile.OpenBinaryStream(); SPClientContext.Load(myFile); SPClientContext.ExecuteQuery(); byte[] myFileBytes = ConvertStreamToByteArray(myFileStream); TextAnalyzeOCRResult myResult = GetAzureTextAnalyzeOCR(myFileBytes).Result; log.LogInformation($"---- Text Analyze OCR Result : " + JsonConvert.SerializeObject(myResult)); myItem["Language"] = myResult.language; string myText = string.Empty; for (int oneLine = 0; oneLine < myResult.regions[0].lines.Count(); oneLine++) { for (int oneWord = 0; oneWord < myResult.regions[0].lines[oneLine].words.Count(); oneWord++) { myText += myResult.regions[0].lines[oneLine].words[oneWord].text + " "; } } myItem["OCRText"] = myText; myItem.Update(); SPClientContext.ExecuteQuery(); log.LogInformation($"---- Text Analyze OCR added to SharePoint Item"); } } }