Create and Call HttpHandler in SharePoint

Requirement:

1. Create a httphandler, and reture json data when call the httphandler in client.

2. Create a list named "Products", including a column named "FeaturedProduct" which of type is Boolean and the column named "ProductCategory" which of type is metadata

3. In server, return the json that the value of the field named "FeaturedProduct" is Yes.

4. In client, get the json data with the url

Here is the steps:

1. Create sharepoint project and httphandler class

according the steps

2. Here is the code to create httphandler class in vs(using System.Web.Extentions), then deploy.

  1. using System;
  2. using Microsoft.SharePoint;
  3. using Microsoft.SharePoint.WebControls;
  4. using System.Web;
  5. using System.Web.Script.Serialization;
  6. using Microsoft.SharePoint.Taxonomy;
  7. using System.Collections.Generic;
  8. namespace Testashx
  9. {
  10. public partial class Test : IHttpHandler
  11. {
  12. public bool IsReusable
  13. {
  14. get { return true; }
  15. }
  16. //http://webUrl/_layouts/15/handler/Test.ashx?featuredProduct=1
  17. public void ProcessRequest(HttpContext context)
  18. {
  19.  
  20. JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
  21. context.Response.ContentType = "application/json";
  22. int featured = int.Parse(context.Request.QueryString.Get("featuredProduct"));
  23. List<Product> jsonResult = new List<Product>();
  24. string caml = string.Empty;
  25. try
  26. {
  27. SPContext currentContext = SPContext.Current;
  28. SPWeb web = SPContext.Current.Web;
  29. SPList list = web.Lists["Products"];
  30. caml = "<Where>"+
  31. "<Eq>"+
  32. " <FieldRef Name='FeaturedProduct' /> "+
  33. "<Value Type='Boolean'>" + featured + "</Value>" +
  34. "</Eq>"+
  35. "</Where>";
  36.  
  37. SPQuery query = new SPQuery();
  38. query.Query = caml;
  39. //query.ViewFields = "<ViewFields><FieldRef Name='ProductCategory'/><FieldRef Name='Size'/></ViewFields>";
  40. SPListItemCollection items = list.GetItems(query);
  41. foreach (SPListItem item in items)
  42. {
  43. Product product = new Product();
  44. if (item["ProductCategory"] as TaxonomyFieldValueCollection != null)
  45. {
  46. TaxonomyFieldValueCollection taxonomyFieldValueCollection = item["ProductCategory"] as TaxonomyFieldValueCollection;
  47. if (taxonomyFieldValueCollection.Count > 0)
  48. {
  49. product.ProductCategory = taxonomyFieldValueCollection[0].Label;
  50. }
  51. }
  52. product.Size = item["Size"].ToString();
  53. jsonResult.Add(product);
  54. }
  55. }
  56. catch (Exception ex)
  57. {
  58. context.Response.Write(ex.Message);
  59. }
  60.  
  61. context.Response.Write(jsonSerializer.Serialize(jsonResult));
  62. }
  63. }
  64. class Product
  65. {
  66. public string ProductCategory { get; set; }
  67. public string Size { get; set; }
  68. }
  69. }

3. In client, here is the code for call service

  1. $.ajax({
  2. url: "http://webUrl/_layouts/15/handler/Test.ashx?
  3.  
  4. featuredProduct=0",
  5. type: "get",
  6. dataType:"json",
  7. success: function (data) {
  8.  
  9. var html = "";
  10. $.each(data, function (index, key) {
  11. html += "<div>" + key.ProductCategory + "</div>";
  12. html += "<div>" + key.Size + "</div>";
  13. });
  14. $("#myDiv").append(html) ;
  15.  
  16. }
  17. });

Note: "featuredProduct=0" is Variable

Create and Call HttpHandler in SharePoint的更多相关文章

  1. how to create a custom form for sharepoint list

    在VS中创建一个applicationPage映射到Layouts文件夹下,然后代码如下: SPList lstTest = web.Lists["Shared Documents" ...

  2. SharePoint 2013 create workflow by SharePoint Designer 2013

    这篇文章主要基于上一篇http://www.cnblogs.com/qindy/p/6242714.html的基础上,create a sample workflow by SharePoint De ...

  3. Installing FIM 2010 R2 SP1 Portal on SharePoint Foundation 2013

    http://www.fimspecialist.com/fim-portal/installing-fim-2010-r2-sp1-portal-on-sharepoint-foundation-2 ...

  4. Searching External Data in SharePoint 2010 Using Business Connectivity Services

    from:http://blogs.msdn.com/b/ericwhite/archive/2010/04/28/searching-external-data-in-sharepoint-2010 ...

  5. Quickstart: Embed a Power BI Report Server report using an iFrame in SharePoint Server

    In this quickstart you will learn how to embed a Power BI Report Server report by using an iFrame in ...

  6. PowerShell实现基于SharePoint的网站HomePage Auto-Configure Solution

    Home Page Web Parts Auto-Configuration PS:该项目为公司项目,我还是给他的名字屏蔽掉吧,这是我用PowerShell写的一个自动化升级工具,此为三部自动化工具的 ...

  7. SharePoint 2013 代码创建应用程序目录(App Catalog)

    众所周知,SharePoint App是2013版本的一大特色,那么,关于App的分发有几种方式呢?SharePoint给我们提供了两种方式,一种是上载到SharePoint应用商店,另一种是在本地S ...

  8. SharePoint 2010 将带有工作流的模板移动到另一个站点集

    HOWTO Move or Migrate SharePoint 2010 List-based Workflows between Sites and Site Collections I’ve e ...

  9. Creating a Custom Page Layout in SharePoint 2013

    Creating a Custom Page Layout in SharePoint 2013 In my last article, I documented how to create a Ma ...

随机推荐

  1. 一个对象toString()方法如果没有被重写,那么默认调用它的父类Object的toString()方法,而Object的toString()方法是打印该对象的hashCode,一般hashCode就是此对象的内存地址

    昨天因为要从JFrame控件获取密码,注意到一个问题,那就是用toString方法得到的不一定是你想要的,如下: jPasswordField是JFrame中的密码输入框,如果用下面的方法是得不到密码 ...

  2. struts2标签(五)

    标签体系结构 jsp出现目的是为了取代servlet,结果逻辑代码,数据库代码都放到了jsp页面中. 为了解决jsp中代码过多的问题,struts2标签分为普通标签和UI标签. 使用struts2标签 ...

  3. HTML简单入门

    - Java攻城狮学习路线 - 基本结构 标准文档:www.w3.org <!DOCTYPE html> <html> <head> <meta charse ...

  4. 运用<body>属性,渲染页面效果

    新建一个HTML5文件,为<body>标签添加样式,代码如下: 01 <!doctype html> 02 <html> 03 <head> 04 &l ...

  5. Android 解析JSON

    上次讲了XML格式数据的解析方式,这次要说的是如何解析JSON数据格式,相对与XML,JSON解析数据的方式在于它的体积更小,在网络上传输可以更省流量. 这次在网上找到一个中国天气json数据的API ...

  6. 数据库 'tempdb' 的事务日志已满。若要查明无法重用日志中的空间的原因

    最常的做法: --1.清空日志 DUMP TRANSACTION tempdb WITH NO_LOG --2.截断事务日志: BACKUP LOG tempdb WITH NO_LOG --3.收缩 ...

  7. REST、RESTful、SOA

    1.http://www.imooc.com/article/17650 2.SOA面向服务架构

  8. win7 64位装sql2000

    1.运行不了安装程序 右击安装exe文件->属性->兼容性->以xp sp3兼容和管理员身份 2.安装过程中提示“被挂起”的故障 解决:打开注册表编辑器,在HKEY_LOCAL_MA ...

  9. AI:模式识别的数学表示(集合—函数观点)

    前言: 模式识别的定义,参考:模式识别两种方法:知识和数据 .百科定义:模式识别(英语:Pattern Recognition),就是通过计算机用数学技术方法来研究模式的自动处理和判读.我们把环境与客 ...

  10. CorelDRAW图片导出变色,如何解决?

    很多小伙伴反映说CDR颜色导出不准确,特别是CorelDRAW X4以及之前的版本,那么CDR导出变色的问题是怎么导致的,如何解决呢,本文小编分享一些自己的心得. 一:出现问题. 比如下面这个问题,明 ...