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 ...
随机推荐
- TCP/IP详解(二)
首先,不得不吐槽一下中文版的翻译,把英文版的很多部分的删除了.中文版的pdf只有400多页,英文版有1000多页.迫于时间,只有先将就着看中文版,但是遇到不懂的地方,一定要对照英文版来看. 滑动窗口协 ...
- 生成器模式(Builder)C++实现
意图:将一个复杂对象的创建与它的表示分离,使得同样的构建过程可以创建不同的表示. 适用性:1.当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式时. 2.当构建过程必须允许被构建的对象有 ...
- 关于SSL证书配置、升级的一些问题总结
SSL会成为网站.APP.小程序(小程序已经强制使用https)等项目的标配.关于SSL证书安装使用的问题今天总结下,以备用. 环境配置:windows server 2008 R2和IIS7.0 1 ...
- vue中的config配置
在webpack.base.conf文件中配置别名以及扩展名 resolve: { extensions: ['.js', '.vue', '.json', '.styl'], alias: { 'v ...
- GitHub上fork别人打代码后如何保持和原作者同步的更新
1.进入你的GitHub发起Pull request 2.选择compare across forks 3.反向操作.base fork改为自己的,head fork改为原作者的 4.点击 cre ...
- Deutsch lernen (02)
1. fließend a. 流利的 Meine französische Freundin spricht fließend Deutsch. 流动的 Der Verkehr wickelt ...
- MyEclipse 连接Oracle数据库(初学者必看)
前言:刚接触Oracle数据库,便有一个需求,编写控制台程序,实现主人登录.数据库为Oracle.下面详细介绍一下MyEclipse 连接Oracle数据库. package DbHelp; im ...
- Swift - AnyClass,元类型和 .self
在Swift中能够表示 “任意” 这个概念的除了 Any 和 AnyObject 以外,还有一个AnyClass.我们能够使用AnyClass协议作为任意类型实例的具体类型.AnyClass在Swif ...
- 微服务常见安全认证方案Session token cookie跨域
HTTP 基本认证 HTTP Basic Authentication(HTTP 基本认证)是 HTTP 1.0 提出的一种认证机制,这个想必大家都很熟悉了,我不再赘述.HTTP 基本认证的过程如下: ...
- 【maven】成功生成jar包,提示找不到主类?
问题描述: 使用maven构建zookeeper项目,完成一个简单的创建组的实例,代码调试完成,使用mvn clean install成功打包得到了jar包,但是在执行时发现使用java -cp ...