Click here to Skip to main content
15,917,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I'm new to all these, I want to actually consume this webservice method from my widget to display out the events. My widget is consuming in JSON format, and I've heard that I need to ensure that the value returned in the web service mehod is in JSON format. So I would like to know which are the parts that I need to change.

Thank you very very much in advance.

[WebMethod(EnableSession = true, Description = "Typical Web Method")]
public List<string> retrieveEvents()
    List<string> retrievedData = new List<string>();
       using (SPSite oSPSite = new SPSite("http:....."))
          SPList list = oSPWeb.Lists.TryGetList("Calendar");
          SPQuery query = new SPQuery();
          query.Query = "<Where><And><Geq><FieldRef Name='EventDate' /><Value          Type='DateTime'>" + SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Today) + "</Value></Geq> + <Leq><FieldRef Name='EndDate' /><Value Type='DateTime'>" + SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Today) + "</Value></Leq></And></Where>";
                        // query.ExpandRecurrence = true;                     
                        query.ViewFields = @"<FieldRef Name='Title' /><FieldRef Name='EventDate' /><FieldRef Name='EndDate' /><FieldRef Name='Location' />";
                        SPListItemCollection items = list.GetItems(query);
                        foreach (SPListItem listItem in items)
                        return retrievedData;

            catch (Exception ex)

            return retrievedData;


To get a pure json format, you can use javascript serializer like below.

public class WebService1 : System.Web.Services.WebService
        [ScriptMethod(UseHttpGet=true ,ResponseFormat = ResponseFormat.Json)]
        public void HelloWorld()
            JavaScriptSerializer js = new JavaScriptSerializer();
            Context.Response.ContentType = "application/json";           


Yes at javascript side code you need to set
dataType: "json"

    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "YourWebServiceName.asmx/HelloWorld",
    data: "{''}",
    dataType: "json",
    success: function (msg) {
    error: function (xhr, status, error) {
Share this answer
I believe this will do it:

// you need to reference System.Web.Extensions

using System.Web.Script.Serialization;
var jsonSerialiser = new JavaScriptSerializer();
var json = jsonSerialiser.Serialize(aList);

Taken from[^]
Share this answer
Member 10498134 1-Jan-14 21:58pm    
Thank you for your feedback! Sorry to ask but what about the javascript script side? I'll have to use something like $ajax?
ZurdoDev 1-Jan-14 22:00pm    
Yes, you can call it with $.ajax. If you are in control of the client side though you can return it in any format you want, I would think.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900