Integration Of Amazon Polly With SharePoint Using Client Side Object Model

Amazon Polly is a service that turns text into lifelike speech, allowing you to create applications that talk and build entirely new categories of speech-enabled products.

In today’s article we will see how we can integrate Amazon Polly service to get the required information from SharePoint online site and convert the information into an mp3 file (basically audio information).

Prerequisites for the integration

  • Free tier subscription for Amazon web services, which requires a debit card/ credit card for authentication purposes.

  • Creating a user with IAM Role for accessing the Amazon Polly services. Refer to this link for more details.

  • Configuring the AWS CLI for your environment and generating credentials file in .AWS Folder. Refer to this guide. (This will ensure that credentials are loaded once you start using aws SDK)

  • Console application using Visual Studio.

  • Amazon SDK for Visual Studio from NuGet Packages.

Steps for Integration

  • Create a console application in Visual Studio 2017.

  • Add Amazon SDK’s (AWSSDK.Core &AWSSDK.Polly) from NuGet Package manager, and install it.

  • Add Sharepoint client dll’s (Microsoft.SharePoint.Client.dll & Microsoft.Sharepoint.Client.RunTime.dll).

  • Add Username and password for your Sharepoint account by Providing values in the settings section from the project (right click on the project and click on settings=> provide a username, password and site URL and set the scope to “user”).

  • Run the application.

Walkthrough of the code

  • The main method initializes the Amazon Polly client and creates an instance of synthesizing speech request class.

  • Create a method to get the client context for the given site collection, after which we will get the required List instance, and get some of the properties related to the list.

  • List-related information set to the “text” property of the synthesize speech instance, and output format is set to “Mp3” and Voice ID is set (voice id is mapped within built voice samples from aws you can choose any of them).

  • Create an instance of the SynthesizeSpeechResponse by passing the request object we obtained in the previous steps.

  • Create a file with the extension with .mp3 extension and convert the audio stream from the response to an mp3 File.
    1. static void Main(string[] args)  
    2.         {  
    3.             AmazonPollyClient pc = new AmazonPollyClient();  
    4.             SynthesizeSpeechRequest sreq = new SynthesizeSpeechRequest();  
    5.             ClientContext ctx = connectSPOnline();  
    6.             List Lexlist = ctx.Web.Lists.GetByTitle("LEX Testing");  
    7.             ctx.Load(Lexlist);  
    8.             ctx.ExecuteQuery();  
    9.             Console.WriteLine(Lexlist.LastItemModifiedDate.ToString("mm-dd-yyyy"));  
    10.             Console.WriteLine(Lexlist.Created.ToShortDateString());  
    11.             Console.ReadLine();  
    12.             sreq.Text = "The List Name" + Lexlist.Title + "is having List Item Count"+"  "+ Lexlist.ItemCount.ToString()+"  "+"  "+"Created date of List is"+"  "+Lexlist.Created.ToShortDateString();  
    13.             sreq.OutputFormat = OutputFormat.Mp3;  
    14.             sreq.VoiceId = VoiceId.Aditi;  
    15.             SynthesizeSpeechResponse sres = pc.SynthesizeSpeech(sreq);  
    16.               
    17.             using (var fileStream = System.IO.File.Create(@""))  
    18.             {  
    19.                 sres.AudioStream.CopyTo(fileStream);  
    20.                 fileStream.Flush();  
    21.                 fileStream.Close();  
    22.             }  
    23.         }  
    24.   
    25.         static ClientContext connectSPOnline()  
    26.         {  
    27.   
    28.             string username = Properties.Settings.Default.SPUserName;  
    29.             string password = Properties.Settings.Default.SPPassWord;  
    30.             SecureString securepass = new SecureString();  
    31.   
    32.             foreach (char c in password)  
    33.             {  
    34.                 securepass.AppendChar(c);  
    35.             }  
    36.             ClientContext context = new ClientContext(Properties.Settings.Default.SPSiteUrl);  
    37.             context.Credentials = new SharePointOnlineCredentials(username, securepass);  
    38.             context.RequestTimeout = 180000;  
    39.             context.Load(context.Web);  
    40.             context.ExecuteQuery();  
    41.             return context;  
    42.         }  
  • Play the .Mp3 file and the audio contains the information from the SharePoint List.

To run this sample follow the below steps,

  • Provision the credentials file from AWS CLI with proper IAM Roles.
  • Set the Site URL, Username, Password in the settings of the project as the code is taking these parameters from the configuration settings.
  • Provide the file location for the .mp3 file.


Similar Articles