public async Task<List<SpTask>> GetTasks(int pageIndex, int pageSize) { StringBuilder requestUri = new StringBuilder(SettingsHelper.SharePointServiceEndpoint) .Append("/_api/web/lists/getbytitle('Tasks')/items") .Append("?$select=Id,Title,Status,Priority"); 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 spTaskJsonResponse = JsonConvert.DeserializeObject<SpTaskJsonCollection>(responseString); List<SpTask> tasks = new List<SpTask>(); foreach (var spListitem in spTaskJsonResponse.Data.Results) { SpTask task = new SpTask { Id = spListitem.Id.ToString(), Title = spListitem.Title, Status = spListitem.Status, Priority = spListitem.Priority }; tasks.Add(task); } return tasks.OrderBy(e => e.Title).Skip(pageIndex * pageSize).Take(pageSize).ToList(); }
public async Task <SpTask> GetTask(string Id) { StringBuilder requestUri = new StringBuilder(SettingsHelper.SharePointServiceEndpoint) .Append("/_api/web/lists/getbytitle('Tasks')/items") .Append("(" + Id + ")") .Append("?$select=Id,Title,Status,Priority"); 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 spTaskJsonResponse = JsonConvert.DeserializeObject <SpTaskJsonSingle>(responseString); SpTask task = new SpTask { Id = spTaskJsonResponse.Data.Id.ToString(), Title = spTaskJsonResponse.Data.Title, Status = spTaskJsonResponse.Data.Status, Priority = spTaskJsonResponse.Data.Priority }; return(task); }
public async Task <List <SpTask> > GetTasks(int pageIndex, int pageSize) { StringBuilder requestUri = new StringBuilder(SettingsHelper.SharePointServiceEndpoint) .Append("/_api/web/lists/getbytitle('Tasks')/items") .Append("?$select=Id,Title,Status,Priority"); 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 spTaskJsonResponse = JsonConvert.DeserializeObject <SpTaskJsonCollection>(responseString); List <SpTask> tasks = new List <SpTask>(); foreach (var spListitem in spTaskJsonResponse.Data.Results) { SpTask task = new SpTask { Id = spListitem.Id.ToString(), Title = spListitem.Title, Status = spListitem.Status, Priority = spListitem.Priority }; tasks.Add(task); } return(tasks.OrderBy(e => e.Title).Skip(pageIndex * pageSize).Take(pageSize).ToList()); }
public async Task UpdateTask(SpTask task) { StringBuilder requestUri = new StringBuilder(SettingsHelper.SharePointServiceEndpoint) .Append("/_api/web/lists/getByTitle('Tasks')/items") .Append("(" + task.Id + ")"); var newTaskJson = new SpTaskJson { __metadata = new __Metadata { Type = "SP.Data.TasksListItem" }, Title = task.Title, Status = task.Status, Priority = task.Priority }; 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; request.Headers.Add("IF-MATCH", "*"); request.Headers.Add("X-HTTP-Method", "MERGE"); await client.SendAsync(request); }
public async Task<ActionResult> Create(SpTask task) { SpTaskRepository repository = new SpTaskRepository(); if (Request.HttpMethod == "POST") { await repository.CreateTask(task); return Redirect("/"); } else { return View(task); } }
public async Task<ActionResult> Edit(string Id, SpTask task) { SpTaskRepository repository = new SpTaskRepository(); if (Request.HttpMethod == "POST") { await repository.UpdateTask(task); return Redirect("/"); } else { task = await repository.GetTask(Id); return View(task); } }
public async Task CreateTask(SpTask task) { StringBuilder requestUri = new StringBuilder(SettingsHelper.SharePointServiceEndpoint) .Append("/_api/web/lists/getByTitle('Tasks')/items"); var newTaskJson = new SpTaskJson { __metadata = new __Metadata { Type = "SP.Data.TasksListItem" }, Title = task.Title, Status = task.Status, Priority = task.Priority }; 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<SpTask> GetTask(string Id) { StringBuilder requestUri = new StringBuilder(SettingsHelper.SharePointServiceEndpoint) .Append("/_api/web/lists/getbytitle('Tasks')/items") .Append("(" + Id + ")") .Append("?$select=Id,Title,Status,Priority"); 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 spTaskJsonResponse = JsonConvert.DeserializeObject<SpTaskJsonSingle>(responseString); SpTask task = new SpTask { Id = spTaskJsonResponse.Data.Id.ToString(), Title = spTaskJsonResponse.Data.Title, Status = spTaskJsonResponse.Data.Status, Priority = spTaskJsonResponse.Data.Priority }; return task; }