Here,in this post I am going to describes how we can programmatically add an contact to the SharePoint 2010.
We go here step by steps.So.let’s begin.
(1) Creating the Contact List.
//Get the Site under which you want to create Contacts.
SPSite site = new SPSite(http://localhost);
SPWeb web = site.AllWebs[0];
//Give the Type of List you want to create,in Our case it’s noting but Contacts.
SPList list = web.Lists["Contacts"];
Now,Up to this we has the instance of Contact list.
(2)Adding Contact In to Contact list.
To Add/Create the Contacts we need to update following field.
| Field Name |
Type |
| Title |
String |
| FirstName |
String |
| FullName |
String |
| Email |
String |
| Company |
String |
| JobTitle |
String |
| WorkPhone |
String |
| HomePhone |
String |
| CellPhone |
String |
| WorkFax |
String |
| WorkAddress |
String |
| WorkCity |
String |
| WorkState |
String |
| WorkZip |
String |
| WorkCountry |
String |
| WebPage |
String |
| Comments |
String |
//Adding event into list
SPListItem contact = list.Items.Add();
contact["Title"] = “Rajput”;
contact["FirstName"] = “Chirag”;
contact["FullName"] = “Rajput Chirag”;
contact["Email"] = “Chirag@test.com”;
contact["Company"] = “My Company”;
contact["JobTitle"] = “Software Eng.;
contact["WorkPhone"] = “0303- 1233344” ;
contact["HomePhone"] = “2121-323232”;
contact["CellPhone"] = “+1213232323”;
contact["WorkFax"] = “122132-3232”
contact["WorkAddress"] = “ddadadada”;
contact["WorkCity"] = “Bangalore”;
contact["WorkState"] = “Karnataka”;
contact["WorkZip"] = “1212-32”;
contact["WorkCountry"] = workcountry" ;
contact["WebPage"] = “http://Mylog.com”;
contact["Comments"] = “MyComments”;
contact.Update();
Your,final code will look like this.
//Get the Site under which you want to create Contacts.
SPSite site = new SPSite(http://localhost);
SPWeb web = site.AllWebs[0];
//Give the Type of List you want to create,in Our case it’s noting but Contacts.
SPList list = web.Lists["Contacts"];
//Adding Contact into list
SPListItem contact = list.Items.Add();
contact["Title"] = “Rajput”;
contact["FirstName"] = “Chirag”;
contact["FullName"] = “Rajput Chirag”;
contact["Email"] = “Chirag@test.com”;
contact["Company"] = “My Company”;
contact["JobTitle"] = “Software Eng.;
contact["WorkPhone"] = “0303- 1233344” ;
contact["HomePhone"] = “2121-323232”;
contact["CellPhone"] = “+1213232323”;
contact["WorkFax"] = “122132-3232”
contact["WorkAddress"] = “ddadadada”;
contact["WorkCity"] = “Bangalore”;
contact["WorkState"] = “Karnataka”;
contact["WorkZip"] = “1212-32”;
contact["WorkCountry"] = workcountry" ;
contact["WebPage"] = “http://Mylog.com”;
contact["Comments"] = “MyComments”;
contact.Update();
Thanks.