public async Task<List<SpContact>> GetContacts(int pageIndex, int pageSize) { StringBuilder requestUri = new StringBuilder(SettingsHelper.SharePointServiceEndpoint) .Append("/_api/web/lists/getbytitle('Contacts')/items") .Append("?$select=Id,Title,FirstName,Email,WorkPhone"); HttpClient client = new HttpClient(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri.ToString()); request.Headers.Add("ACCEPT", "application/json;odata=verbose"); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await GetAccessToken()); HttpResponseMessage response = await client.SendAsync(request); string responseString = await response.Content.ReadAsStringAsync(); var spContactJsonResponse = JsonConvert.DeserializeObject<SpContactJsonCollection>(responseString); List<SpContact> contacts = new List<SpContact>(); foreach (var spListitem in spContactJsonResponse.Data.Results) { SpContact contact = new SpContact { Id = spListitem.Id.ToString(), FirstName = spListitem.FirstName, LastName= spListitem.Title, Email = spListitem.Email, WorkPhone = spListitem.WorkPhone }; contacts.Add(contact); } return contacts.OrderBy(e => e.LastName).Skip(pageIndex * pageSize).Take(pageSize).ToList(); }
public async Task <SpContact> GetTask(string Id) { StringBuilder requestUri = new StringBuilder(SettingsHelper.SharePointServiceEndpoint) .Append("/_api/web/lists/getbytitle('Contacts')/items") .Append("(" + Id + ")") .Append("?$select=Id,Title,FirstName,Email,WorkPhone"); HttpClient client = new HttpClient(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri.ToString()); request.Headers.Add("ACCEPT", "application/json;odata=verbose"); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await GetAccessToken()); HttpResponseMessage response = await client.SendAsync(request); string responseString = await response.Content.ReadAsStringAsync(); var spContactJsonResponse = JsonConvert.DeserializeObject <SpContactJsonSingle>(responseString); SpContact contact = new SpContact { Id = spContactJsonResponse.Data.Id.ToString(), LastName = spContactJsonResponse.Data.Title, FirstName = spContactJsonResponse.Data.FirstName, Email = spContactJsonResponse.Data.Email, WorkPhone = spContactJsonResponse.Data.WorkPhone }; return(contact); }
public async Task <List <SpContact> > GetContacts(int pageIndex, int pageSize) { StringBuilder requestUri = new StringBuilder(SettingsHelper.SharePointServiceEndpoint) .Append("/_api/web/lists/getbytitle('Contacts')/items") .Append("?$select=Id,Title,FirstName,Email,WorkPhone"); HttpClient client = new HttpClient(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri.ToString()); request.Headers.Add("ACCEPT", "application/json;odata=verbose"); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await GetAccessToken()); HttpResponseMessage response = await client.SendAsync(request); string responseString = await response.Content.ReadAsStringAsync(); var spContactJsonResponse = JsonConvert.DeserializeObject <SpContactJsonCollection>(responseString); List <SpContact> contacts = new List <SpContact>(); foreach (var spListitem in spContactJsonResponse.Data.Results) { SpContact contact = new SpContact { Id = spListitem.Id.ToString(), FirstName = spListitem.FirstName, LastName = spListitem.Title, Email = spListitem.Email, WorkPhone = spListitem.WorkPhone }; contacts.Add(contact); } return(contacts.OrderBy(e => e.LastName).Skip(pageIndex * pageSize).Take(pageSize).ToList()); }
public async Task CreateTask(SpContact contact) { StringBuilder requestUri = new StringBuilder(SettingsHelper.SharePointServiceEndpoint) .Append("/_api/web/lists/getByTitle('Contacts')/items"); var newTaskJson = new SpContactJson { __metadata = new __Metadata { Type = "SP.Data.ContactsListItem" }, Title = contact.LastName, FirstName = contact.FirstName, Email = contact.Email, WorkPhone = contact.WorkPhone }; StringContent requestContent = new StringContent(JsonConvert.SerializeObject( newTaskJson, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore })); requestContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json;odata=verbose"); HttpClient client = new HttpClient(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUri.ToString()); request.Headers.Add("ACCEPT", "application/json;odata=verbose"); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await GetAccessToken()); request.Content = requestContent; await client.SendAsync(request); }
public async Task<ActionResult> Create(SpContact task) { SpContactRepository repository = new SpContactRepository(); if (Request.HttpMethod == "POST") { await repository.CreateTask(task); return Redirect("/"); } else { return View(task); } }
public async Task<ActionResult> Edit(string Id, SpContact task) { SpContactRepository repository = new SpContactRepository(); if (Request.HttpMethod == "POST") { await repository.UpdateTask(task); return Redirect("/"); } else { task = await repository.GetTask(Id); return View(task); } }
public async Task CreateTask(SpContact contact) { StringBuilder requestUri = new StringBuilder(SettingsHelper.SharePointServiceEndpoint) .Append("/_api/web/lists/getByTitle('Contacts')/items"); var newTaskJson = new SpContactJson { __metadata = new __Metadata { Type = "SP.Data.ContactsListItem" }, Title = contact.LastName, FirstName = contact.FirstName, Email = contact.Email, WorkPhone = contact.WorkPhone }; StringContent requestContent = new StringContent(JsonConvert.SerializeObject( newTaskJson, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore })); requestContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json;odata=verbose"); HttpClient client = new HttpClient(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUri.ToString()); request.Headers.Add("ACCEPT", "application/json;odata=verbose"); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await GetAccessToken()); request.Content = requestContent; await client.SendAsync(request); }
public async Task<SpContact> GetTask(string Id) { StringBuilder requestUri = new StringBuilder(SettingsHelper.SharePointServiceEndpoint) .Append("/_api/web/lists/getbytitle('Contacts')/items") .Append("(" + Id + ")") .Append("?$select=Id,Title,FirstName,Email,WorkPhone"); HttpClient client = new HttpClient(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri.ToString()); request.Headers.Add("ACCEPT", "application/json;odata=verbose"); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await GetAccessToken()); HttpResponseMessage response = await client.SendAsync(request); string responseString = await response.Content.ReadAsStringAsync(); var spContactJsonResponse = JsonConvert.DeserializeObject<SpContactJsonSingle>(responseString); SpContact contact = new SpContact { Id = spContactJsonResponse.Data.Id.ToString(), LastName = spContactJsonResponse.Data.Title, FirstName = spContactJsonResponse.Data.FirstName, Email = spContactJsonResponse.Data.Email, WorkPhone = spContactJsonResponse.Data.WorkPhone }; return contact; }