Create and Call HttpHandler in SharePoint
2017-05-21 15:16
591 查看
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 steps2. 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 your own custom 404 error page and handle redirect in SharePoint 2007 (MOSS)?
- 通过HttpHandler和属性用Javascript调用C#方法(Using a HttpHandler and Attributes to call C# methods in Javascript)
- How to create your own custom 404 error page and handle redirect in SharePoint
- Creating and Using a New Content Type in SharePoint 2007(zhuan:http://www.sharepointproconnections.com/article/sharepoint-portal
- 记录一个:Create, Update, Delete and Copy Item Activity in SharePoint 2010 Workflow
- Create and Consume WCF in Sharepoint
- Create custom Task List and Forms in SharePoint 2010 with Visual Studio 2012
- How to create your own custom 404 error page and handle redirect in SharePoint 2007 (MOSS)?
- There is not a header with name ServiceContext and namespace http://schemas.microsoft.com/sharepoint/servicecontext in the messa
- SharePoint 2007 Deployment: List Instance Features(http://blogit.create.pt/blogs/andrevala/archive/2008/06/17/SharePoint-2007-De
- Images; How to create an HTTP handler to dynamically resize images and change quality.
- [转]Working with user roles and permissions in SharePoint Object Model
- How to change service accounts and service account passwords in SharePoint Server 2007 and in Windows SharePoint Services 3.0
- HTTP Handlers and HTTP Modules in ASP.NET
- HTTP Handlers and HTTP Modules in ASP.NET(Reship)
- How to create and delete a SharePoint site by web application
- Images; How to create an HTTP handler to dynamically resize images and change quality.
- HTTP Handlers and HTTP Modules in ASP.NET
- SharePoint Designer Check In and Check Out Error – Cannot perform this operation.The file is no longer check out or has been del