Twilio Sending SMS from a SharePoint List

The following example can be worked into event receivers, workflow, etc – for simplicity and due to time constraints I used a console application.

For this simple PoC I wanted to send text messages from a SharePoint list, I signed up for a Twilio ( trial, Twilio offer a very good api for dealing with SMS and voice calls.

I’ve created a new console application and installed two nuget packages:

Install-Package Microsoft.SharePointOnline.CSOM

Install-Package Twilio

The following code assumes you have created a Client Context…

The code below is standard SharePoint code, this deals with getting the data from the list, and populating a dictionary containing the recipients number, and the message to be sent.  I also update the list item so I know it has been processed.

using (var context = new ClientContext(contextWebUrl))
                context.Credentials = new SharePointOnlineCredentials(userName, password);
                Web web = context.Web;
                context.Load(context.Web, w => w.Title);

                List sms = web.Lists.GetByTitle("SMS");

                CamlQuery query = CamlQuery.CreateAllItemsQuery(100);
                ListItemCollection items = sms.GetItems(query);

                Dictionary<string, string> messageToSend = new Dictionary<string, string>();

                foreach (ListItem item in items)
                    if (string.Equals(item["Status"], "new"))
                        messageToSend.Add(item["Title"].ToString(), item["Body"].ToString());
                        item["Status"] = "Sent";

The next piece of code deals with sending the messages (this is rough and ready code – purely for a PoC)..

            string AccountSid = "xxxxxxxxxxxxxxxxxxxxxxxx";
            string AuthToken = "xxxxxxxxxxxxxxxxxxxxx";
            var twilio = new TwilioRestClient(AccountSid, AuthToken);

            foreach (var msg in messageToSend)
                var newMsg = twilio.SendMessage("My Twilio Number", msg.Key, msg.Value);
                Console.WriteLine(newMsg.Sid + " " + newMsg.Status);

Once you have a Twilio trial account, you’ll get a phone number, and credentials for interacting with the API.

The next article covers building a web API to receive text messages.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s