博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Create and Call HttpHandler in SharePoint
阅读量:7104 次
发布时间:2019-06-28

本文共 3633 字,大约阅读时间需要 12 分钟。

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

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
jsonResult = new List
(); string caml = string.Empty; try { SPContext currentContext = SPContext.Current; SPWeb web = SPContext.Current.Web; SPList list = web.Lists["Products"]; caml = "
"+ "
"+ "
"+ "
" + featured + "
" + "
"+ "
"; SPQuery query = new SPQuery(); query.Query = caml; //query.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

 

 

 

 

转载地址:http://duchl.baihongyu.com/

你可能感兴趣的文章
从键盘上连续录入一批整数,比较并输出其中的最大值和最小值,当输入数字0时结束循环...
查看>>
2018焦作区域赛E. Resistors in Parallel
查看>>
html--特殊字符过滤
查看>>
Linux中断(interrupt)子系统之一:中断系统基本原理【转】
查看>>
SOA会不会造成IT黑洞
查看>>
查询存储过程所需参数
查看>>
HTML5 Web app开发工具Kendo UI Web教程:如何配置Kendo UI Calendar
查看>>
vue Element动态设置el-menu导航当前选中项
查看>>
session的使用
查看>>
Centos6.8通过yum安装mysql5.7
查看>>
NCBI通过氨基酸位置查看相邻SNP
查看>>
CAS SSO单点登录框架学习
查看>>
好书推荐——《启动大脑》
查看>>
网络流24题 -No.17 运输问题
查看>>
MySQL数据库的主从复制简单学习使用
查看>>
kprobe原理与实现笔记
查看>>
sql语句优化
查看>>
Topological Sorting
查看>>
神经网络
查看>>
WINDOWS之入侵痕迹清理总结
查看>>