Quantcast
Channel: Bobby Beckner » jQuery
Viewing all articles
Browse latest Browse all 2

SharePoint List and LINQ using jQuery and IhttpHandler

$
0
0

Challenge

I was looking for a simple way to use jQuery and a few parameters to retrieve data from a SharePoint List in JSON format. Here’s my approach…

Javascript

In the below method, I pass in the list name, url to handler, and some query parameters:

//request menu items
$.ajax({
    dataType: 'json',
    url: '_layouts/data/list.ashx',
    data: {query: 'get', list: 'navigation'},
	success: function(data){
		//omitted
	}
});

Visual Studio 2008 SharePoint Project

I created a new SharePoint project in Visual Studio 2008. The .ashx file contains a page directive with the same class as the .cs file. In order for the handler to work, I added the following file structure to the project:

  • Templates/
    • LAYOUTS/
      • Data/
        • List.ashx
        • List.cs

C# Namespaces

My handler class (List.cs) uses the following namespaces:

using Microsoft.Office.Server;
using Microsoft.Office.Server.Administration;
using Microsoft.Office.Server.UserProfiles;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

The class must inherit from the IhttpHandler:

public class List : IHttpHandler

C# IhttpHandler

Don’t forget to implement IsReusable

#region IHttpHandler Members

    public bool IsReusable
    {
        // Return false in case your Managed Handler cannot be reused for another request.
        // Usually this would be false in case you have some state information preserved per request.
        get { return true; }
    }

C# Process Request

The code below simply gets the parameter “query” and uses it to map to a specific function. You will also notice I am using the JavaScriptSerializer class to return the JSON. This pattern allows me to easily use jQuery to query SharePoint Lists. The only thing more efficient would obviously be implementing a REST pattern.

    public void ProcessRequest(HttpContext context)
    {
        string query = context.Request.Params["query"];

        context.Response.Clear();
        context.Response.ContentType = "application/json";

        if (!string.IsNullOrEmpty(query))
        {
            try
            {
                IEnumerable items = null;
                switch (query)
                {
                    case "GetUser":
                        GetUser(context);
                        return;
                    case "get":
                        items = GetItems();
                        break;
                    //omitted
                }

                JavaScriptSerializer ser = new JavaScriptSerializer();
                context.Response.Write(ser.Serialize(items));

            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
        }
    }

    #endregion

C# LINQ

The code below is used to return a 2-dimensional list. It retrieves the list via a request header parameter known as “list”.

private IEnumerable GetItems()
    {
        string listName = HttpContext.Current.Request.Params["list"].ToString();
        SPList list = SPContext.Current.Web.Lists[listName];

        var items = from SPListItem item in list.Items
                    select new { Name = item.Name, Value = item["value"].ToString() };

        return items;
    }

If this code was of use to you, leave me some love. Recommendations are also appreciated.

Share and Enjoy:
  • RSS
  • del.icio.us
  • StumbleUpon
  • Digg
  • Twitter
  • Mixx
  • Facebook
  • LinkedIn
  • Reddit
  • Yahoo! Buzz
  • Technorati
  • co.mments

No related posts.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles



Latest Images