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:
Hi,

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.


C#
[WebMethod(EnableSession = true, Description = "Typical Web Method")]
public List<string> retrieveEvents()
{
    List<string> retrievedData = new List<string>();
    try
    {
       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)
                        {
                            retrievedData.Add(listItem["Title"].ToString());
                            retrievedData.Add(listItem["EventDate"].ToString());
                            retrievedData.Add(listItem["EndDate"].ToString());
                            retrievedData.Add(listItem["Location"].ToString());
                        }
                        return retrievedData;
                    }
                }

            }
            catch (Exception ex)
            {

            }
            return retrievedData;

        }
Posted

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

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


        }
    }


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


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

C#
// you need to reference System.Web.Extensions

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


Taken from http://stackoverflow.com/questions/9110724/serializing-a-list-to-json[^]
 
Share this answer
 
Comments
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