/// <summary> /// Provides REST access and results in Google OneBox Result format. /// </summary> /// <param name="mailBoxName">The name of the mailbox to query.</param> /// <param name="searchTerm">The term to search for.</param> /// <returns></returns> public Stream SearchForMail(string searchTerm, string userName) { var result = new OneBoxResults { Provider = PROVIDER_NAME, ResultCodeSpecified = true }; try { var mailBoxCredentials = GetMailboxCredntials(userName); var sharedMailBoxes = GetSharedMailBoxes(); var personalMailFolders = GetPersonalMailFolders(); var matches =connector.FindItems(mailBoxCredentials, sharedMailBoxes, personalMailFolders, searchTerm); if (matches != null) { result.ModuleResult = new MODULE_RESULT[matches.Count()]; var index = 0; foreach (var match in matches) { var resultFields = new field[8] { new field{Name="MailBoxName",Value=match.MailBoxName}, new field{Name="FolderName",Value=match.FolderName}, new field{Name="FromAddress",Value=match.FromAddress}, new field{Name="ToAddresses",Value=String.Join(", ",match.ToAddresses.ToArray())}, new field{Name="CCAddresses",Value=String.Join(", ",match.CCAddresses.ToArray())}, new field{Name="AttachmentTitles",Value=String.Join(", ",match.AttachmentTitles.ToArray())}, new field{Name="Subject",Value=match.Subject}, new field{Name="Body",Value=match.Body} }; result.ModuleResult[index] = new MODULE_RESULT { Fields = resultFields, Title = String.Format("Mail item result {0}", index) }; index++; } result.Title = new OneBoxResultsTitle { URLText = String.Format("{0} results ", matches.Count()) }; } else { result.Title = new OneBoxResultsTitle { URLText = "0 results " }; } result.ResultCode = OneBoxResultsResultCode.success; } catch (UnauthorizedAccessException authEx) { if (!EventLog.SourceExists(LOG_SOURCE_NAME)) { EventLog.CreateEventSource(LOG_SOURCE_NAME, "Application"); } EventLog.WriteEntry(LOG_SOURCE_NAME, authEx.ToString(), EventLogEntryType.FailureAudit); result.Title = new OneBoxResultsTitle { URLText = "Access denied to mailbox." }; result.ResultCode = OneBoxResultsResultCode.securityFailure; if (exceptionsMustBeThrown) { throw authEx; } } catch (Exception ex) { EventLog.WriteEntry(LOG_SOURCE_NAME, ex.ToString() + "\n" + ex.StackTrace.ToString(), EventLogEntryType.Error); result.Title = new OneBoxResultsTitle { URLText = "Error prevented search of mailbox." }; result.ResultCode = OneBoxResultsResultCode.lookupFailure; if (exceptionsMustBeThrown) { throw ex; } } return StreamOut(result); }
private Stream StreamOut(OneBoxResults result) { MemoryStream stream = new MemoryStream(); StreamWriter writer = new StreamWriter(stream, Encoding.UTF8); var xmls = new XmlSerializer(typeof(OneBoxResults)); xmls.Serialize(writer, result); if ( WebOperationContext.Current != null) { WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml"; } writer.Flush(); stream.Position = 0; return stream; }