Hello I am trying to pull data from my cosmos database and then modify it and then send it out to a user through an email which has an attachment of a tab delimited text document of the data.
So far I have the logic app set up to recieve the data and the query to get the data and the method to change the data into the format I want. I need to figure out a way to create a tab delimited file and send that to my logic app. I am only able to get a string to get sent to my logic app of all the data. Here is the code I am working with:
string query = $"Select * from c";
var restrictions = await _cosmosService.QueryItemsAsync<Data>(Database, query);
if (restrictions == null)
return new NoContentResult();
string tabDelimitedText = ConvertToTabDelimited(restrictions);
byte[] tabDelimitedBytes = System.Text.Encoding.UTF8.GetBytes(tabDelimitedText);
MemoryStream stream = new MemoryStream(tabDelimitedBytes);
var rtn = new FileStreamResult(stream, "text/plain");
await _client.PostAsync(EmailTriggerUrl, new StringContent(rtn, Encoding.UTF8, "application/text"));
return rtn;
private string GetTabDelimitedText(List<Person> people)
{
StringWriter textWriter = new StringWriter();
textWriter.WriteLine("Name\tAge\tCity");
foreach (var person in people)
{
textWriter.WriteLine($"{person.Name}\t{person.Age}\t{person.City}");
}
return textWriter.ToString();
}
I know that I need to change the PostAsync from a StringContent but I am unsure what is needs to be
I am trying to pull data from my cosmos database and then modify it and then send it out to a user through an email.
cosmosClient
and stored it as a Delimited-Text
file in the Blob container using the below code. Then I used Logic Apps to send a mail.Below are the steps I followed:
cosmosDbEP
and cosmosDbKey
are created an instance called cosmosClient
which is used to interact with the Cosmos DB. query
is used to retrieve data from the db.
blobServClient
is created using blobConnStr
which is used to interact with the Azure Blob Storage account.
GetTabDelimitedText
method takes a Data
as parameter. It uses string interpolation to create a string where data is seperated by tabs \t
.
blobName
is used to generate a unique blob name for each and every item in cosmos db and tab-delimited
data is uploaded to Blob storage as a text file.
Below is the code I tried with:
CosmosClient cosmosClient = new CosmosClient(cosmosDbEP, cosmosDbKey);
var cont = cosmosClient.GetContainer(dbId, contId);
var query = new QueryDefinition("SELECT * FROM c");
var queryIterator = cont.GetItemQueryIterator<Data>(query);
BlobServiceClient blobServClient = new BlobServiceClient(blobConnStr);
BlobContainerClient contClient = blobServClient.GetBlobContainerClient(blobContName);
while (queryIterator.HasMoreResults)
{
var response = await queryIterator.ReadNextAsync();
foreach (var item in response)
{
string tabDelimitedContent = GetTabDelimitedText(item);
string blobName = Guid.NewGuid().ToString() + ".txt";
BlobClient blobClient = contClient.GetBlobClient(blobName);
using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(tabDelimitedContent)))
{
await blobClient.UploadAsync(stream, new BlobUploadOptions { HttpHeaders = new BlobHttpHeaders { ContentType = "text/plain" } });
}
Console.WriteLine($"Uploaded item to Blob Storage: {blobName}");
}
}
Console.WriteLine("Data transfer completed.");
Output:
Uploaded item to Blob Storage: 8ee63e0d-cc6d-47b6-ad69-af40097c9134.json
Uploaded item to Blob Storage: 5f24d20c-baff-4f43-a226-e784d333ac72.json
Uploaded item to Blob Storage: 52f0fc8c-6ab8-412b-a700-d36f7ba3aae8.json
Uploaded item to Blob Storage: ca9f2958-0a2c-4eae-9734-605490ccad3d.json
Uploaded item to Blob Storage: e97888ca-3ef2-4c8e-994c-369cd01ad24b.json
Data transfer completed.
Data stored in Blob:
1 Balaji 23 Male
2 Pavan 20 Male
3 Likitha 24 Female
4 Sai 24 Female
5 Venkatesh 25 Male
Design in Logic Apps:
/tbalaji
. Now, the data can be sent to mail in Logic Apps as you can see below.