Saturday, March 31, 2007

Inserting a contact item in a Contacts Lists in a subsite through the UpdateListItems web service

First let me tell you that in order to use the list's web service in a subsite you have to add ?WSDL to the URI of the web service. Example: http://server/subsite/_vti_bin/Lists.asmx?WSDL. If you do not append ?WSDL you will not receive any error, but will operate with the web service of the top level site instead of the subsite and you will not find your list in the subsite.

We will use a Contacts List template to save our contacts. It let us to synchronize with MS Outlook 2007 in order to view these contacts in out Address Book. You can see a method to accomplish it:


static
bool insertContact(string company, string address, string name, string surname, string email, string phone, string language)

{


 

PCM.Lists list = new PCM.Lists();

list.Credentials = new System.Net.NetworkCredential("user", "P@$$w0rd");


 

System.Xml.XmlDocument doc = new System.Xml.XmlDocument();


 


XmlElement elBatch = doc.CreateElement("Batch");

elBatch.SetAttribute("OnError", "Continue");

elBatch.SetAttribute("ListVersion", "1");


 


XmlElement el1 = doc.CreateElement("Method");

el1.SetAttribute("ID", "1");

el1.SetAttribute("Cmd", "New");


 


XmlElement field1 = doc.CreateElement("Field");

field1.SetAttribute("Name", "Company");

field1.InnerText = company;


 


XmlElement field2 = doc.CreateElement("Field");

field2.SetAttribute("Name", "WorkAddress");

field2.InnerText = address;


 


XmlElement field3 = doc.CreateElement("Field");

field3.SetAttribute("Name", "FirstName");

field3.InnerText = name;


 


XmlElement field4 = doc.CreateElement("Field");

field4.SetAttribute("Name", "Title");

field4.InnerText = surname;


 


XmlElement field5 = doc.CreateElement("Field");

field5.SetAttribute("Name", "Email");

field5.InnerText = email;


 


XmlElement field6 = doc.CreateElement("Field");

field6.SetAttribute("Name", "WorkPhone");

field6.InnerText = phone;


 


XmlElement field7 = doc.CreateElement("Field");

field7.SetAttribute("Name", "WorkCountry");

field7.InnerText = language;


 


 

elBatch.AppendChild(el1);

el1.AppendChild(field1);

el1.AppendChild(field2);

el1.AppendChild(field3);

el1.AppendChild(field4);

el1.AppendChild(field5);

el1.AppendChild(field6);

el1.AppendChild(field7);


 


XmlNode rNode = list.UpdateListItems("Contacts", elBatch);


 


return
true;

}

Note: PCM is a Web Reference to our Lists.asmx web services.

Now you may synchronize the list with Outlook.

Related Links:

http://shlakapau.spaces.live.com/blog/cns!72ADBFE6717AFAF!107.entry

No comments:

 
Online Visitors