UWP POST ListItems,但仅某些项目 [英] UWP POST ListItems but only some items
问题描述
好.感谢这里的一点帮助,我已经找到了如何从我的SharePoint列表中获取ListItems的方法.现在,我试图发布一个新的ListItem.到目前为止,我所使用的仅发布了一个空白的ListItem.发生了什么事,但是那里没有数据.
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.
因此在GET中,我使用了以下代码块:
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();
}
}
使用此类:
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();
}
}
我假设因为必须要有result.fields.somevalue才能在我的代码中获取GET的适当值,所以我需要对POST做类似的事情.
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.
任何帮助将不胜感激.
当我进一步深入研究时,我要放入SharePoint中的内容是:
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\"}"
我相信我需要的是
"{\"fields\":{\"Title\":\"4\",\"UserName\":\"JD\",\"UserAge\":\"28\",\"UserTitle\":\"Visitor\"}}"
或有影响的东西.
推荐答案
我终于明白了.这是更改后的代码:
I figured it out finally. Here is the changed code:
public async Task<string> PostHttpSPContentWithToken(string url, string token)
{
var httpClient = new HttpClient();
HttpResponseMessage response;
try
{
var root = new
{
fields = new Dictionary<string, string>
{
{ "Title", TitleText.Text },
{ "UserName", UserNameText.Text },
{ "UserAge", UserAgeText.Text },
{ "UserTitle", UserTitleText.Text }
}
};
string content = JsonConvert.SerializeObject(root);
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();
}
}
所以我有一个正确的主意,只需要弄清楚如何去做就可以了.
So I had the right idea, just had to figure out how to go about it.
这篇关于UWP POST ListItems,但仅某些项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!