Saturday, August 10, 2013

Creating vCard files in C#

vCard files stores the Contact Information of the Person. You can create an outlook Contact in outlook and save that item as .vcf file and see its underlying format by opening it as notepad file and compile a string on your C# code with similar format and send that to browser as attachment file using Response.Write.

                            StringBuilder sw = new StringBuilder();
                            sw.AppendLine("BEGIN:VCARD");
                            sw.AppendLine("VERSION:3.0");
                            sw.AppendLine("N:" + fName + ";" + lName + ";;;");
                            sw.AppendLine("FN:" + currentUser.Name);
                            sw.AppendLine("ORG:" + workEmail.Split('@')[1]);
                            sw.AppendLine("EMAIL;TYPE=work:" + workEmail);
                            sw.AppendLine("TITLE:" + workTitle);
                            if(currentProfile["WorkPhone"] != null && currentProfile["WorkPhone"].Value != null)
                            {
                               sw.AppendLine("TEL;WORK;VOICE:" + currentProfile["WorkPhone"].Value);
                            }
                            if (currentProfile["CellPhone"] != null && currentProfile["CellPhone"].Value != null)
                            {
                                sw.AppendLine("TEL;CELL;VOICE:" + currentProfile["CellPhone"].Value);
                            }
                            sw.AppendLine("END:VCARD");

                            string FileName = "vCard.vcf";

                            Response.Clear();
                            Response.ContentType = "text/x-vcard";
                            Response.AddHeader("content-disposition", "inline; filename=" + FileName);
                            Response.Write(sw.ToString());
                            Response.Flush();
                            
Related Posts Plugin for WordPress, Blogger...