OK. I've figured out how to GET ListItems from my SharePoint List thanks to a little help from here. Now I am trying to POST a new ListItem. What I have used so far has only posted a blank ListItem. So something happened, but there is no data there.
So with the GET I used the following code block:
public async Task<string> GetHttpSPContentWithToken(string url, string token, string listitem)
{
var httpClient = new HttpClient();
HttpResponseMessage response;
try
{
var request = new HttpRequestMessage(HttpMethod.Get, url);
//Add the token in Authorization header
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
response = await httpClient.SendAsync(request);
var content = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<SharePointListItems.RootObject>(content);
if (listitem == "Title")
{
return result.fields.Title;
}
else if (listitem == "UserName")
{
return result.fields.UserName;
}
else if (listitem == "UserAge")
{
return result.fields.UserAge;
}
else
{
return result.fields.UserTitle;
}
}
catch (Exception ex)
{
return ex.ToString();
}
}
With this Class:
using Newtonsoft.Json;
using System;
public class SharePointListItems
{
public class UserCreated
{
public string email { get; set; }
public string id { get; set; }
public string displayName { get; set; }
}
public class CreatedBy
{
public UserCreated user { get; set; }
}
public class UserModified
{
public string email { get; set; }
public string id { get; set; }
public string displayName { get; set; }
}
public class LastModifiedBy
{
public UserModified user { get; set; }
}
public class ParentReference
{
}
public class ContentType
{
public string id { get; set; }
}
public class Fields
{
[JsonProperty("@odata.etag")]
public string ODataETag { get; set; }
public string Title { get; set; }
public string UserName { get; set; }
public string UserAge { get; set; }
public string UserTitle { get; set; }
public string id { get; set; }
public DateTime Modified { get; set; }
public DateTime Created { get; set; }
public string ContentType { get; set; }
public string AuthorLookupId { get; set; }
public string EditorLookupId { get; set; }
public string _UIVersionString { get; set; }
public bool Attachments { get; set; }
public string Edit { get; set; }
public string LinkTitleNoMenu { get; set; }
public string LinkTitle { get; set; }
public int ItemChildCount { get; set; }
public int FolderChildCount { get; set; }
public string _ComplianceFlags { get; set; }
public string _ComplianceTag { get; set; }
public string _ComplianceTagWrittenTime { get; set; }
public string _ComplianceTagUserId { get; set; }
}
public class RootObject
{
[JsonProperty("@odata.context")]
public string ODataContext { get; set; }
[JsonProperty("@odata.etag")]
public string ODataETag { get; set; }
public DateTime createdDateTime { get; set; }
public string eTag { get; set; }
public string id { get; set; }
public DateTime lastModifiedDateTime { get; set; }
public string webUrl { get; set; }
public CreatedBy createdBy { get; set; }
public LastModifiedBy lastModifiedBy { get; set; }
public ParentReference parentReference { get; set; }
public ContentType contentType { get; set; }
[JsonProperty("fields@odata.context")]
public string FieldsODataContext { get; set; }
public Fields fields { get; set; }
}
}
So that makes sense to me, at least so I thought anyway.
But now I am trying to add to the list with this code, but as I said before, I just get a blank row below the previous rows.
public async Task<string> PostHttpSPContentWithToken(string url, string token)
{
var httpClient = new HttpClient();
HttpResponseMessage response;
try
{
var values = new Dictionary<string, string>
{
{ "Title", TitleText.Text },
{ "UserName", UserNameText.Text },
{ "UserAge", UserAgeText.Text },
{ "UserTitle", UserTitleText.Text }
};
string content = JsonConvert.SerializeObject(values);
var request = new HttpRequestMessage(HttpMethod.Post, url);
//Add the token in Authorization header
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
request.Content = new StringContent(content, Encoding.UTF8, "application/json");
response = await httpClient.SendAsync(request);
var responseString = await response.Content.ReadAsStringAsync();
return responseString;
}
catch (Exception ex)
{
return ex.ToString();
}
}
I am assuming since I had to you result.fields.somevalue to get to the appropriate value in my code for the GET I need to do something similar for my POST.
Any help would be appreciated.
As I dig further into this, the content that I am trying to put into SharePoint is:
"{\"Title\":\"4\",\"UserName\":\"JD\",\"UserAge\":\"28\",\"UserTitle\":\"Visitor\"}"
I believe what I need is this:
"{\"fields\":{\"Title\":\"4\",\"UserName\":\"JD\",\"UserAge\":\"28\",\"UserTitle\":\"Visitor\"}}"
Or something to that affect.