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.
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." 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
I would prefer to utilize
StreamWriter class
instead ofEncoding class
to avoid any troubles with encoding.In your example to set request body replace:
with