1: public static IEnumerable<TeamContactsItem> ReadList()
2: {
3: SharedSiteCollectionDataContext dc = new SharedSiteCollectionDataContext(new Uri("http://team.contoso.local/sites/shared/_vti_bin/listdata.svc"));
4: dc.Credentials = CredentialCache.DefaultNetworkCredentials;
5:
6: var contacts = from c in dc.TeamContacts
7: orderby c.Id
8: select c;
9: return contacts;
10: }
11:
12: public static TeamContactsItem ReadItem(int id)
13: {
14: SharedSiteCollectionDataContext dc = new SharedSiteCollectionDataContext(new Uri("http://team.contoso.local/sites/shared/_vti_bin/listdata.svc"));
15: dc.Credentials = CredentialCache.DefaultNetworkCredentials;
16:
17: var contact = (from c in dc.TeamContacts
18: where c.Id == id
19: select c).FirstOrDefault();
20: return contact;
21: }
22:
23: public static TeamContactsItem Create(TeamContactsItem newContacts)
24: {
25: SharedSiteCollectionDataContext dc = new SharedSiteCollectionDataContext(new Uri("http://team.contoso.local/sites/shared/_vti_bin/listdata.svc"));
26: dc.Credentials = CredentialCache.DefaultNetworkCredentials;
27:
28: dc.AddToTeamContacts(newContacts);
29: dc.SaveChanges();
30:
31: return newContacts;
32: }
33:
34: public static void Delete(int id)
35: {
36: SharedSiteCollectionDataContext dc = new SharedSiteCollectionDataContext(new Uri("http://team.contoso.local/sites/shared/_vti_bin/listdata.svc"));
37: dc.Credentials = CredentialCache.DefaultNetworkCredentials;
38:
39: TeamContactsItem contactToDelete = (from c in dc.TeamContacts
40: where c.Id == id
41: select c).FirstOrDefault();
42:
43: if (contactToDelete != null)
44: {
45: dc.DeleteObject(contactToDelete);
46: dc.SaveChanges();
47: }
48: }
49:
50: public static void Update(TeamContactsItem contacts)
51: {
52: SharedSiteCollectionDataContext dc = new SharedSiteCollectionDataContext(new Uri("http://team.contoso.local/sites/shared/_vti_bin/listdata.svc"));
53: dc.Credentials = CredentialCache.DefaultNetworkCredentials;
54:
55: //retrieve the contact to update
56: TeamContactsItem contactToUpdate = (from c in dc.TeamContacts
57: where c.Id == contacts.Id
58: select c).FirstOrDefault();
59: if (contactToUpdate != null)
60: {
61: //set fields to update
62: contactToUpdate.FirstName = contacts.FirstName;
63: contactToUpdate.LastName = contacts.LastName;
64: contactToUpdate.HomePhone = contacts.HomePhone;
65: contactToUpdate.Address = contacts.Address;
66: contactToUpdate.BusinessPhone = contacts.BusinessPhone;
67: contactToUpdate.City = contacts.City;
68: contactToUpdate.Company = contacts.Company;
69: contactToUpdate.CountryRegion = contacts.CountryRegion;
70: contactToUpdate.EMailAddress = contacts.EMailAddress;
71: contactToUpdate.FaxNumber = contacts.FaxNumber;
72: contactToUpdate.FullName = contacts.FullName;
73: contactToUpdate.JobTitle = contacts.JobTitle;
74: contactToUpdate.MobileNumber = contacts.MobileNumber;
75: contactToUpdate.StateProvince = contacts.StateProvince;
76: contactToUpdate.WebPage = contacts.WebPage;
77: contactToUpdate.ZIPPostalCode = contacts.ZIPPostalCode;
78:
79: //update the entity
80: dc.UpdateObject(contactToUpdate);
81: dc.SaveChanges();
82: }
83: }