【问题标题】:UWP POST ListItems but only some itemsUWP POST ListItems 但只有一些项目
【发布时间】:2018-08-12 16:10:45
【问题描述】:

好的。多亏了这里的一些帮助,我已经弄清楚了如何从我的 SharePoint 列表中获取 ListItems。现在我正在尝试发布一个新的 ListItem。到目前为止,我使用的只是发布了一个空白的 ListItem。所以发生了一些事情,但是那里没有数据。

所以对于 GET,我使用了以下代码块:

    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; }
    }
}

所以这对我来说是有道理的,至少我是这么认为的。

但现在我正尝试使用此代码添加到列表中,但正如我之前所说,我只是在前面的行下方得到一个空白行。

    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 做类似的事情。

任何帮助将不胜感激。

当我进一步深入研究时,我试图放入 SharePoint 的内容是:

"{\"Title\":\"4\",\"UserName\":\"JD\",\"UserAge\":\"28\",\"UserTitle\":\"Visitor\"}"

我相信我需要的是这样的:

"{\"fields\":{\"Title\":\"4\",\"UserName\":\"JD\",\"UserAge\":\"28\",\"UserTitle\":\"Visitor\"}}"

或者有什么影响。

【问题讨论】:

    标签: c# json rest post microsoft-graph-api


    【解决方案1】:

    我终于明白了。这是修改后的代码:

        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();
            }
        }
    

    所以我有正确的想法,只是必须弄清楚如何去做。

    【讨论】:

      猜你喜欢
      • 2021-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-16
      • 2018-03-04
      • 1970-01-01
      相关资源
      最近更新 更多