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 ...
随机推荐
- yii引入js文件
作者:zccst 四.在视图层(../views/..)添加CSS文件或JavaScript文件 Yii::app()->clientScript->registerScriptFile( ...
- bzoj 2152 聪聪可可(点分治模板)
2152: 聪聪可可 Time Limit: 3 Sec Memory Limit: 259 MBSubmit: 3194 Solved: 1647[Submit][Status][Discuss ...
- springboot启动报错:Cannot determine embedded database driver class for database type NONE.
package cn.zb.test; import org.springframework.boot.SpringApplication; import org.springframework.bo ...
- .Net Core中使用Quartz.Net Vue开即用的UI管理
Quartz.NET Quartz.Net 定制UI维护了常用作业添加.删除.修改.停止.启动功能,直接使用cron表达式设置作业执行间隔,有完整的日志记录. Quartz.NET是一个功能齐全的开源 ...
- Bootstrap栅格系统&媒体查询
bootstrap中几乎所有元素的盒子模型为IE下的盒模型,通俗点说就是box-sizing设置成了:border-box. 栅格系统 媒体查询 媒体查询是非常别致的"有条件的 CSS ...
- 通过getSystemServices获取手机管理大全
getSystemService是Android很重要的一个API,它是Activity的一个方法,根据传入的NAME来取得对应的Object,然后转换成相应的服务对象.以下介绍系统相应的服务. 传入 ...
- poj3669 广搜
//好久没刷题了,生疏了. 题意分析: 题意理解为在一个二维的正向坐标轴上,一个点(流星)连同它的上下左右的四个点会在某一个时刻被破坏.一个人在原点,问她到达安全区的最小时间是多少. 代码思路: 从原 ...
- java Web(3)
Servlet 是运行在Web服务器或应用服务器上的Java程序 在Web上创建动态内容的有效而强大的解决方案 由容器来管理生命周期与Web服务器交互 由Sun规范了其功能 Servlet部署: 一个 ...
- numpy安装失败-小失误
1. 古老的方法: 安装python numpy库AMD64 失败,网上的教程是这样的:http://www.cnblogs.com/zhuyp1015/archive/2012 ...
- NET MVC FileResult 导出/下载 文件/Excel
参考http://www.cnblogs.com/ldp615/archive/2010/09/17/asp-net-mvc-file-result.html 1.引入NPOI 2.代码 using ...