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.

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web;
using System.Web.Script.Serialization;
using Microsoft.SharePoint.Taxonomy;
using System.Collections.Generic;
namespace Testashx
{
public partial class Test : IHttpHandler
{
public bool IsReusable
{
get { return true; }
}
//http://webUrl/_layouts/15/handler/Test.ashx?featuredProduct=1
public void ProcessRequest(HttpContext context)
{ JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
context.Response.ContentType = "application/json";
int featured = int.Parse(context.Request.QueryString.Get("featuredProduct"));
List<Product> jsonResult = new List<Product>();
string caml = string.Empty;
try
{
SPContext currentContext = SPContext.Current;
SPWeb web = SPContext.Current.Web;
SPList list = web.Lists["Products"];
caml = "<Where>"+
"<Eq>"+
" <FieldRef Name='FeaturedProduct' /> "+
"<Value Type='Boolean'>" + featured + "</Value>" +
"</Eq>"+
"</Where>"; SPQuery query = new SPQuery();
query.Query = caml;
//query.ViewFields = "<ViewFields><FieldRef Name='ProductCategory'/><FieldRef Name='Size'/></ViewFields>";
SPListItemCollection items = list.GetItems(query);
foreach (SPListItem item in items)
{
Product product = new Product();
if (item["ProductCategory"] as TaxonomyFieldValueCollection != null)
{
TaxonomyFieldValueCollection taxonomyFieldValueCollection = item["ProductCategory"] as TaxonomyFieldValueCollection;
if (taxonomyFieldValueCollection.Count > 0)
{
product.ProductCategory = taxonomyFieldValueCollection[0].Label;
}
}
product.Size = item["Size"].ToString();
jsonResult.Add(product);
}
}
catch (Exception ex)
{
context.Response.Write(ex.Message);
} context.Response.Write(jsonSerializer.Serialize(jsonResult));
}
}
class Product
{
public string ProductCategory { get; set; }
public string Size { get; set; }
}
}

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

   $.ajax({
url: "http://webUrl/_layouts/15/handler/Test.ashx? featuredProduct=0",
type: "get",
dataType:"json",
success: function (data) { var html = "";
$.each(data, function (index, key) {
html += "<div>" + key.ProductCategory + "</div>";
html += "<div>" + key.Size + "</div>";
});
$("#myDiv").append(html) ; }
});

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. 【IDEA】(4)---很好用的DEBUG功能

    IDEA-DEBUG功能 一.常用快捷键 快捷键并不是完全一样的,我这边是MAC安装的IDEA, 这边最主要还是知道DEBUG时常用的功能. 1.快捷键 F7 #进入下一步,如果当前行是一个方法,则进 ...

  2. Struts2 在登录拦截器中对ajax请求的处理

    前言: 由于ajax请求不像http请求,可以直接进行页面跳转,你返回的所有东西,ajax都只会识别为一个字符串. 之前尝试的方法是在拦截器中返回一个标识给ajax,然后再在每一个ajax请求成功之后 ...

  3. div自动适应浏览器窗口水平和垂直居中

    html <body> <div id="centerDiv">自动适应水平和垂直居中</div> </body> css ;;} ...

  4. PHP开发笔记(三)关于PHP伪静态的问题总结

    Apache 第一个问题就是关于PHPStudy集成Apache环境下5.5版本以上”No input file specified“问题. 针对TP5框架,以下是.htaccess文件的配置,PHP ...

  5. JavaScript中赋值运算符的使用

    JavaScript中的赋值运算可以分为简单赋值运算和复合赋值运算.简单赋值运算是将赋值运算符(=)右边表达式的值保存到左边的变量中:而复合赋值运算混合了其他操作(例如算术运算操作)和赋值操作.例如: ...

  6. CDC之Metastability

    1 CDC  A clock domain crossing occurs whenever data is transferred from a flop driven by one clock t ...

  7. 机器学习:随机森林RF-OBB袋外错误率

    文章讲解比较详细,且有Python代码,可以作为有用的参考. 原文链接:http://blog.csdn.net/zhufenglonglove/article/details/51785220  参 ...

  8. Object::connect: No such slot (QT槽丢失问题)

    1.看看你的类声明中有没有Q_OBJECT,并继承public QMainWindow{ 例如: class CPlot: public QMainWindow{ Q_OBJECT 2.你声明的函数要 ...

  9. 【sqli-labs】 less43 POST -Error based -String -Stacked with tiwst(POST型基于错误的堆叠变形字符型注入)

    和less42一样 login_user=&login_password=1');insert into users(id,username,password) value(15,'root' ...

  10. 学习Spider 了解 Scrapy的流程

    Scrapy 先创建项目 在windows下 scrapy startproject      myproject         #myproject是你的项目名称 cd 项目名称 scrapy g ...