//GetOne internal void GetOne(int itemId) { using (var client = new SPHttpClient(webUri, userName, password)) { var listTitle = "HttpClientList"; //var itemId = 7; var endpointUrl = string.Format("{0}/_api/web/lists/getbytitle('{1}')/items({2})", webUri, listTitle, itemId); var data = client.ExecuteJson(endpointUrl); Console.WriteLine("Column 1: " + data["d"]["Title"] + ". Column 2: " + data["d"]["Somecolumn"] + ". - id: " + data["d"]["ID"]); } }
//VERBS //GetAll internal void Get() { using (var client = new SPHttpClient(webUri, userName, password)) { var listTitle = "HttpClientList"; var endpointUrl = string.Format("{0}/_api/web/lists/getbytitle('{1}')/items?$select=ID%2CTitle%2CSomecolumn", webUri, listTitle); var data = client.ExecuteJson(endpointUrl); foreach (var item in data["d"]["results"]) { Console.WriteLine(item["Title"] + " - " + "id: " + item["ID"]); } } }
//Delete internal void Delete(int itemId) { using (var client = new SPHttpClient(webUri, userName, password)) { var listTitle = "HttpClientList"; //var itemId = 2; var endpointUrl = string.Format("{0}/_api/web/lists/getbytitle('{1}')/items({2})", webUri, listTitle, itemId); var headers = new Dictionary <string, string>(); headers["IF-MATCH"] = "*"; headers["X-HTTP-Method"] = "DELETE"; client.ExecuteJson(endpointUrl, HttpMethod.Post, headers, default(string)); Console.WriteLine("Task item has been deleted"); } }
//Put internal void Put(string title, string value, int itemId) { using (var client = new SPHttpClient(webUri, userName, password)) { var listTitle = "HttpClientList"; //var itemId = 6; var itemPayload = new { __metadata = new { type = "SP.Data.HttpClientListListItem" }, //All the columns goes here comma seperetad Title = title, Somecolumn = value }; var endpointUrl = string.Format("{0}/_api/web/lists/getbytitle('{1}')/items({2})", webUri, listTitle, itemId); var headers = new Dictionary <string, string>(); headers["IF-MATCH"] = "*"; headers["X-HTTP-Method"] = "MERGE"; client.ExecuteJson(endpointUrl, HttpMethod.Post, headers, itemPayload); Console.WriteLine("Task item has been updated"); } }
//Post internal void Post(string title, string value) { using (var client = new SPHttpClient(webUri, userName, password)) { var listTitle = "HttpClientList"; var itemPayload = new { __metadata = new { type = "SP.Data.HttpClientListListItem" }, //All the columns goes here comma seperetad Title = title, Somecolumn = value }; var endpointUrl = string.Format("{0}/_api/web/lists/getbytitle('{1}')/items", webUri, listTitle); var data = client.ExecuteJson(endpointUrl, HttpMethod.Post, itemPayload); Console.WriteLine("Task item '{0}' has been created", data["d"]["Title"]); } }