1

Im trying to create a site using the REST API from a console app. I keep getting:

"Not well formed JSON Stream"

I cant find much on calling the REST API from c# so have been piecing it together from the scaps I could find. Below is my code. Any idea?

RestClient RC = new RestClient("http://someserver/_api");
            NetworkCredential NCredential = new NetworkCredential("somelogin", "somepassword", "somedomain");
            RC.Authenticator = new NtlmAuthenticator(NCredential);

            RestRequest Request = new RestRequest("contextinfo?$select=FormDigestValue", Method.POST);
            Request.AddHeader("Accept", "application/json;odata=verbose");
            Request.AddHeader("Body", "");

            //rethink this!!!!!!!!
            string ReturnedStr = RC.Execute(Request).Content;
            int StartPos = ReturnedStr.IndexOf("FormDigestValue") + 18;
            int length = ReturnedStr.IndexOf(@""",", StartPos) - StartPos;
            string FormDigestValue = ReturnedStr.Substring(StartPos, length);

            Console.WriteLine(FormDigestValue);

            string Data = string.Concat(
                "{ '__metadata': { 'type': 'SP.WebCreationInformation' },",
                "'Title': 'Team projects', 'Url': 'TeamProjects', 'WebTemplate': 'STS'",
                "'UseSamePermissionsAsParentSite': true }");

            Request = new RestRequest("web/webs/add", Method.POST);
            Request.AddHeader("Accept", "application/json;odata=verbose");
            Request.AddHeader("content-type", "application/json;odata=verbose");
            Request.AddHeader("content-length", Data.Length.ToString());
            Request.AddHeader("X-RequestDigest", FormDigestValue);
            Request.AddParameter("parameters", Data);

            Console.WriteLine("Creating Site");
            Console.WriteLine(RC.Execute(Request).Content);
            Console.WriteLine("Created Site");
            Console.ReadLine();

UPDATE It seems I had a typo in my JSON string. Fixing this sorted the "well formatted json" error, however it still doesn't work as expected. I found by adding a parameter and specifying that the string is of type "application/json;odata=verbose" sorted the problem.

RestClient RC = new RestClient("http://grpcphsp02d:1819/_api");
            NetworkCredential NCredential = new NetworkCredential("SomeUserName", "SomePassword", "SomeDomain");
            RC.Authenticator = new NtlmAuthenticator(NCredential);

            RestRequest Request = new RestRequest("contextinfo?$select=FormDigestValue", Method.POST);
            Request.AddHeader("Accept", "application/json;odata=verbose");
            Request.AddHeader("Body", "");

            //rethink this!!!!!!!!
            string ReturnedStr = RC.Execute(Request).Content;
            int StartPos = ReturnedStr.IndexOf("FormDigestValue") + 18;
            int length = ReturnedStr.IndexOf(@""",", StartPos) - StartPos;
            string FormDigestValue = ReturnedStr.Substring(StartPos, length);

            Console.WriteLine(FormDigestValue);

            var Data = string.Concat(
                "{'parameters':{'__metadata':{'type':'SP.WebCreationInformation'},",
                "'Title':'Team projects 3','Url':'TeamProjects3','WebTemplate':'STS',",
                "'UseSamePermissionsAsParentSite': true}}");

            Request = new RestRequest("web/webs/add", Method.POST);
            Request.RequestFormat = DataFormat.Json;
            Request.AddHeader("Accept", "application/json;odata=verbose");
            Request.AddHeader("X-RequestDigest", FormDigestValue);
            Request.AddParameter("application/json;odata=verbose", Data, ParameterType.RequestBody);

            Console.WriteLine("Creating Site");
            Console.WriteLine(RC.Execute(Request).Content);
            Console.WriteLine("Created Site");
            Console.ReadLine();
  • I suggest that you consider using the Client Object Model instead of the REST API when writing managed code. The code will be a lot cleaner, and it will be easier to debug and maintain. You'll also be working with real types instead of raw XML or JSON. – Rob Windsor Jan 29 '15 at 12:52
1

I haven't used C# to do REST calls in awhile, but when I did, I used the HttPWebRequest, as that's what I found on MSDN:

var Data = string.Concat(
            "{ '__metadata': { 'type': 'SP.WebCreationInformation' },",
            "'Title': 'Team projects', 'Url': 'TeamProjects', 'WebTemplate': 'STS'",
            "'UseSamePermissionsAsParentSite': true }");
var endpointRequest = (HttpWebRequest)HttpWebRequest.Create("http://<site url>/_api/Web/webs/add");
endpointRequest.Method = "POST";
endpointRequest.ContentLength = Data.Length;
endpointRequest.ContentType = "application/json;odata=verbose";
endpointRequest.Accept = "application/json;odata=verbose";
endpointRequest.Headers.Add("X-RequestDigest", digest);
var stream = endpointRequest.GetRequestStream();
stream.Write(Data, 0, Data.Length);
stream.Close();

var endpointResponse = (HttpWebResponse)endpointRequest.GetResponse();

I've been using JavaScript for REST lately, so there may be slight errors, but I believe I got it right. See this for some code examples.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.