Create and Call HttpHandler in SharePoint
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的更多相关文章
- how to create a custom form for sharepoint list
在VS中创建一个applicationPage映射到Layouts文件夹下,然后代码如下: SPList lstTest = web.Lists["Shared Documents" ...
- SharePoint 2013 create workflow by SharePoint Designer 2013
这篇文章主要基于上一篇http://www.cnblogs.com/qindy/p/6242714.html的基础上,create a sample workflow by SharePoint De ...
- 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 ...
- 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 ...
- 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 ...
- PowerShell实现基于SharePoint的网站HomePage Auto-Configure Solution
Home Page Web Parts Auto-Configuration PS:该项目为公司项目,我还是给他的名字屏蔽掉吧,这是我用PowerShell写的一个自动化升级工具,此为三部自动化工具的 ...
- SharePoint 2013 代码创建应用程序目录(App Catalog)
众所周知,SharePoint App是2013版本的一大特色,那么,关于App的分发有几种方式呢?SharePoint给我们提供了两种方式,一种是上载到SharePoint应用商店,另一种是在本地S ...
- SharePoint 2010 将带有工作流的模板移动到另一个站点集
HOWTO Move or Migrate SharePoint 2010 List-based Workflows between Sites and Site Collections I’ve e ...
- 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 ...
随机推荐
- bzoj1030 文本生成器(AC自动机+dp)
1030: [JSOI2007]文本生成器 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 4777 Solved: 1986[Submit][Stat ...
- 剑指offer笔记
1.在定义类的赋值描述符成员函数时,有以下几点要注意: 1)判断是否是自己赋值给自己 2)返回值是const类的引用(为了连续赋值) 3)参数是const类的引用 4)如果数据成员中有指针,注意要深拷 ...
- html body中的标签
HTML中的标签有两类 一.字体标签 字体标签包含:h1~h6.<font>.<u>.<b>.<strong><em>.<sup> ...
- python 进程理论基础
背景知识 顾名思义,进程即一个软件正在进行的过程.进程是对正在运行的程序的一个抽象 进程的概念起源于操作系统,是操作系统的最核心的概念,也是操作系统提供的最古老的也是最重要的抽象概念之一.操作系统的其 ...
- HBase里的官方Java API
见 https://hbase.apache.org/apidocs/index.html
- 5.17从零开始搭建springboot-dubbo的例子
https://www.cnblogs.com/baijinqiang/p/10848259.html
- 联想笋尖S90(S90-t 、S90-u)解锁BootLoader
工具下载链接: http://pan.baidu.com/s/1eSgZuka 备用下载链接: http://pan.baidu.com/s/1dFKqSId 本篇教程,仅限于联想笋尖S90(S90- ...
- IE浏览器发展史
- Windows下使用Caffe-Resnet
参考文章: 编译历程参考:CNN:Windows下编译使用Caffe和Caffe2 caffe的VS版本源代码直接保留了sample里面的shell命令,当然这些shell命令在Windows平台下是 ...
- 微服务常见安全认证方案Session token cookie跨域
HTTP 基本认证 HTTP Basic Authentication(HTTP 基本认证)是 HTTP 1.0 提出的一种认证机制,这个想必大家都很熟悉了,我不再赘述.HTTP 基本认证的过程如下: ...