Jonas Stawski

Everything .NET and More

Create an SMS Task Using the Contact’s Name in Windows Phone

You can use the SmsComposeTask to create an SMS with Windows Phone. As the code below shows, it is very easy to do.

   1: <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
   2:     <Button Content="Number Only" Height="72" HorizontalAlignment="Left" Margin="12,6,0,0" Name="btnNumberOnly" VerticalAlignment="Top" Click="btnNumberOnly_Click" />
   3:     <Button Content="Name Only" Height="72" HorizontalAlignment="Left" Margin="12,84,0,0" Name="btnNameOnly" VerticalAlignment="Top" Click="btnNameOnly_Click" />
   4:     <Button Content="Name and Number - Contact" Height="72" HorizontalAlignment="Left" Margin="12,162,0,0" Name="btnNameAndNumber" VerticalAlignment="Top" Click="btnNameAndNumber_Click" />
   5:     <Button Content="Name and Number - No Contact" Height="72" HorizontalAlignment="Left" Margin="12,240,0,0" Name="btnNameAndNumberNoContact" VerticalAlignment="Top" Click="btnNameAndNumberNoContact_Click" />
   6: </Grid>
   1: private void btnNumberOnly_Click(object sender, RoutedEventArgs e)
   2: {
   3:     SmsComposeTask task = new SmsComposeTask();
   4:     task.Body = "Number Only";
   5:     task.To = "305-555-1212";
   6:     task.Show();
   7: }
   8: private void btnNameOnly_Click(object sender, RoutedEventArgs e)
   9: {
  10:     SmsComposeTask task = new SmsComposeTask();
  11:     task.Body = "Name Only";
  12:     task.To = "Chris Sells";
  13:     task.Show();
  14: }
  16: private void btnNameAndNumber_Click(object sender, RoutedEventArgs e)
  17: {
  18:     SmsComposeTask task = new SmsComposeTask();
  19:     task.Body = "Name and Number - Contact";
  20:     task.To = "Chris Sells <206-555-0003>";
  21:     task.Show();
  22: }
  24: private void btnNameAndNumberNoContact_Click(object sender, RoutedEventArgs e)
  25: {
  26:     SmsComposeTask task = new SmsComposeTask();
  27:     task.Body = "Name and Number - No Contact";
  28:     task.To = "Chris Sells <206-444-0000>";
  29:     task.Show();
  30: }

The first “Number Only” button creates a new task and sets the To to a number. This generates the following SMS:

Capture0    Capture1

When clicking on the number, the phone prompts us with some options where we can open the contact and see the contact’s information:

Capture2     Capture3

In this case we can see that since the number doesn’t exist in the contacts address book we are displayed with a temporary contact’s card (unknown). Furthermore, if the number were to belong to a contact it would display that contact’s information:

Capture4     Capture5

It is also important to notice that the formatting of the number doesn’t affect the OS’s ability to relate it to an existing contact. As you can see the SMS was created with the format xxx-xxx-xxxx and the contact was saved with the format (xxx)xxxxxxx. Very nice and expected detail for the Redmond guys!

The next button, “Name Only”, creates a new SMS with a name. For some reason, the OS is not capable of tying this name to a contacts (even though it exists):

Capture6    Capture7

As you can see it linked to a temporary contact and treats the To as an email. Weird.

Continuing to the next button, “Name and Number – Contact”, we are creating a new SMS specifying the To in the format of “Name <Number>” (similar to the email format: “Name <Email>”). In this case the result is similar to the number only, but with the exception that the To line of the SMS app shows the name instead of the number. A much better user experience for your users:

Capture8    Capture5

Finally, the “Name and Number – No Contact” button, creates a new SMS the same way as the previous case, but using the name of a contact that exists and a phone number of a contact that doesn’t exists. This results in an unknown contact:

Capture9    Capture10

It is very important to mention that the OS always ignores the name and uses the number as a link to a contact. For example, using the same example as above using “John Doe <206-555-0003>” will result in Chris Sells contact card to appear, but John Doe’s name will show in the SMS app.

Happy Programming!

Add comment