Create and Call HttpHandler in SharePoint

本文将指导您如何在SharePoint项目中创建HTTP处理器类,并实现客户端调用返回JSON数据的功能。此外,我们将创建一个名为Products的列表,包含FeaturedProduct和ProductCategory两个元数据类型的列。在服务器端,我们通过URL获取FeaturedProduct字段为Yes的JSON数据。在客户端,使用AJAX方法调用服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值