简体   繁体   English

使用Sharepoint Rest API创建列表项; 结果是400错误的请求

[英]Create list item with Sharepoint Rest API; Result is 400 Bad request

I implement the following code: 我实现以下代码:

using Microsoft.SharePoint.Client;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Net;
using System.Security;
using System.Text;

namespace Sharepoint_sandbox {
   class Program {
      private static CookieContainer GetO365CookieContainer (SharePointOnlineCredentials credentials, string targetSiteUrl){
         CookieContainer container = null;

         Uri targetSite = new Uri (targetSiteUrl);
         string cookieString = credentials.GetAuthenticationCookie (targetSite);
         if (cookieString != null) {
            string trimmedCookie = cookieString.TrimStart ("SPOIDCRL=".ToCharArray ());
            container = new CookieContainer ();
            container.Add (new Cookie ("SPOIDCRL", trimmedCookie, string.Empty, targetSite.Authority));
         }

         return container;
      }

      private static SharePointOnlineCredentials GetO365Credentials (string userName, string password) {
         SecureString securePassword = new SecureString ();
         foreach (char c in password.ToCharArray ())
            securePassword.AppendChar (c);

         return new SharePointOnlineCredentials (userName, securePassword);
      }

      private static string GetFormDigest (string siteUrl, ICredentials credentials, CookieContainer cc) {
         string formDigest = null;

         string resourceUrl = siteUrl + "/_api/contextinfo";
         HttpWebRequest httpWebRequest = HttpWebRequest.Create (resourceUrl) as HttpWebRequest;
         httpWebRequest.Credentials = credentials;
         httpWebRequest.CookieContainer = cc;
         httpWebRequest.Timeout = 10000;
         httpWebRequest.Method = "POST";
         httpWebRequest.ContentType = "application/json; odata=verbose";
         httpWebRequest.Accept = "application/json;odata=verbose";
         httpWebRequest.ContentLength = 0;
         httpWebRequest.ContentType = "application/json";
         string result;
         using (WebResponse webResponse = httpWebRequest.GetResponse ()) {
            using (System.IO.StreamReader sr = new System.IO.StreamReader (webResponse.GetResponseStream ())) {
               result = sr.ReadToEnd ();
            }
         }
         var dObject = (JObject)JsonConvert.DeserializeObject (result);
         foreach (var item in dObject["d"].Children ()){
            formDigest = item.First["FormDigestValue"].ToString ();
         }

         return formDigest;
      }

      private static Tuple<string, List<string>> CreateListItem (string site, string userName, string password, string resourceUrl, string content) {
         HttpWebRequest httpWebRequest = HttpWebRequest.Create (resourceUrl) as HttpWebRequest;
         httpWebRequest.UseDefaultCredentials = false;
         SharePointOnlineCredentials credentials = GetO365Credentials (userName, password);
         httpWebRequest.Credentials = credentials;
         httpWebRequest.CookieContainer = GetO365CookieContainer (credentials, site);
         string formDigest = GetFormDigest (site, credentials, httpWebRequest.CookieContainer);
         httpWebRequest.Method = "POST";
         httpWebRequest.Headers.Add ("X-RequestDigest", formDigest);
         httpWebRequest.Timeout = 10000;
         httpWebRequest.ContentType = "application/json; odata=verbose";
         httpWebRequest.Accept = "application/json; odata=verbose";
         httpWebRequest.ContentLength = 0;

         List<string> errorMessages = new List<string> ();
         string response = "";
         try {
            byte[] binary = Encoding.Unicode.GetBytes(content);
            httpWebRequest.ContentLength = binary.Length;
            using (System.IO.Stream requestStream = httpWebRequest.GetRequestStream ()) {
               requestStream.Write (binary, 0, binary.Length);
            }

            using (WebResponse webResponse = httpWebRequest.GetResponse ()) {
               using (System.IO.StreamReader sr = new System.IO.StreamReader (webResponse.GetResponseStream ())) {
                  response = sr.ReadToEnd ();
               }
            }
         }
         catch (WebException e) {
            errorMessages.Add (e.Message);
            if (e.Response != null && e.Response.GetResponseStream () != null) {
               using (System.IO.StreamReader sr = new System.IO.StreamReader (e.Response.GetResponseStream ())) {
                  errorMessages.Add (sr.ReadToEnd ());
               }
            }
         }

         return new Tuple<string, List<string>> (response, errorMessages);
      }

      static void Main (string[] args) {
         string site = "https://*******";
         string user = "******";
         string password = "******";

         string resourceUrl = site + "/_api/Web/Lists(guid'0818cf14-4028-4c7d-adbc-489adb1c0cb4')/Items";
         string content = "{ '__metadata': { 'type':'SP.Data.Test_x005f_1ListItem' }, 'Title': 'TestItem'}";

         Tuple<string, List<string>> result = CreateListItem (site, user, password, resourceUrl, content);

         Console.Write (result.Item1 + "\n" + string.Join ("\n", result.Item2));
         Console.ReadKey ();
      }
   }
}

The result is: 结果是:

A távoli kiszolgáló a következő hibát küldte vissza: (400) Hibás kérelem.
{"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"hu-HU","value":"Invalid JSON. The property name '' is not valid. The name of a property cannot be empty."}}}

The hungarian text in English: The remote server returned an error: (400) Bad request. 英文的匈牙利文:远程服务器返回错误:(400)错误的请求。

I don't understend which property was empty. 我不明白哪个属性是空的。 If the content first and last characters is not { and } then the error is "Invalid JSON. A token was not recognized in the JSON content." 如果内容的第一个和最后一个字符不是{和},则错误为“无效的JSON。在JSON内容中无法识别令牌。” If the content is empty {} or any other string between the {} then the error is same as above (The property name '' is not valid. ...") 如果内容为空{}或{}之间的任何其他字符串,则错误与以上相同(属性名称“无效。...”)

Could you help me? 你可以帮帮我吗? Thanks 谢谢

Changing the encoding from Unicode to UTF8 solve the problem: 将编码从Unicode更改为UTF8可解决以下问题:

From: 从:

byte[] binary = Encoding.Unicode.GetBytes(content);

To: 至:

byte[] binary = Encoding.UTF8.GetBytes(content);

I would prefer to utilize StreamWriter class instead of Encoding class to avoid any troubles with encoding. 我宁愿使用StreamWriter class而不是Encoding class来避免编码方面的麻烦。

In your example to set request body replace: 在您的示例中设置请求正文,请替换:

byte[] binary = Encoding.Unicode.GetBytes(content);
httpWebRequest.ContentLength = binary.Length;
using (System.IO.Stream requestStream = httpWebRequest.GetRequestStream())
{
     requestStream.Write(binary, 0, binary.Length);
}

with

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
    streamWriter.Write(content);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM