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. sdut1269 走迷宫(dfs)

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1269 连着做了三个基本的dfs,终于弄懂了搜索 ...

  2. [Apple开发者帐户帮助]二、管理你的团队(6)找到您的团队ID

    该组ID是已分配给您的团队苹果产生了独特的10个字符的字符串.您需要为某些活动提供您的团队ID, 例如将您的应用程序转移到App Store Connect中的其他开发人员. 在您的开发者帐户中,单击 ...

  3. go之数组

    一.数组概念 go语言提供了数组类型的数据结构 数组是具有 [唯一类型] 的一组 [固定长度] 的数据项序列,这种类型可以是任意类型 二.数组声明 var variable_name [SIZE]va ...

  4. Oracle备份Scott

    @echo off echo ================================================ echo Windows环境下Oracle数据库的自动备份脚本 echo ...

  5. jquery.validate验证text,checkbox,radio,selected

    index.cshtml <form id="formLogin" method="post"> <div> <label for ...

  6. 5.27 sorm图

  7. 前端-Vue学习思维导图笔记

    看不清的朋友右键保存或者新窗口打开哦!喜欢我可以关注我,还有更多前端思维导图笔记有vue结构分析,JS基础,JQ,JS高级,Angular,git等等

  8. 【Linux】swap分区简介及空间增加方式

    swap分区简介 Swap分区在系统的物理内存不够用的时候,把硬盘空间中的一部分空间释放出来,以供当前运行的程序使用.那些被释放的空间可能来自一些很长时间没有什么操作的程序,这些被释放的空间被临时保存 ...

  9. 【原创】python中文编码问题深入分析(三):python2.7文件读写中文编码问题

    上一篇文章介绍和分析了python2.7中使用print遇到的中文编码问题的原因和解决方案,本篇主要介绍一下python2.7中执行文件读写可能遇到的编码问题. 1.文件读取 假如我们读取一个文件,文 ...

  10. 三维重建5:场景中语义分析/语义SLAM/DCNN-大尺度SLAM

    前言: 在实时/非实时大规模三维场景重建中,引入了语义SLAM这个概念,参考三维重建:SLAM的尺度和方法论问题和三维重建:SLAM的粒度和工程化问题 .大规模三维场景重建的尺度增大,因此相对于整个重 ...