PopupsHandler.ashx:
using System;
using System.Collections.Generic;
using System.Web;
using System.Collections;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.Sdk.Query;
using Microsoft.Crm.Sdk.Utility;
using Microsoft.Crm.SdkTypeProxy;
using Frensworkz.CRM.XCMGCC.Web.Common;
namespace Frensworkz.CRM.XCMGCC.Web.ISV.tanhua
{
/// <summary>
/// PopupsHandler 的摘要说明
/// </summary>
public class PopupsHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
#region 判断从js回调获取的ownerid不为null或空
if (!string.IsNullOrEmpty(context.Request.Params["ownerid"]))
{
CrmService service = Query.instance.InitServer(context);
context.Response.Write(GetAllPopups(service,new Guid(context.Request.Params["ownerid"]),0,DateTime.Now,"new_popups"));
context.Response.End();
}
#endregion
#region 判断获取的poupid不为null或空
if (!string.IsNullOrEmpty(context.Request.Params["poupid"]) )
{
CrmService service = Query.instance.InitServer(context);
SetState(service,new Guid(context.Request.Params["poupid"]));
context.Response.End();
}
#endregion
}
public bool IsReusable
{
get
{
return false;
}
}
#region 获取当前时间之前的所有记录
/// <summary>
/// 获取当前时间之前的所有记录
/// </summary>
/// <param name="service">服务</param>
/// <param name="ownerid">负责人ID</param>
/// <param name="statecode">状态</param>
/// <param name="currenttime">当前时间</param>
/// <param name="entityName">实体名</param>
public string GetAllPopups(CrmService service,Guid ownerid,int statecode,DateTime currenttime,string entityName)
{
try
{
//设置条件
ConditionExpression conditionExpression = new ConditionExpression();
conditionExpression.AttributeName = "ownerid";//绑定字段名
conditionExpression.Operator = ConditionOperator.Equal;//设置操作条件
conditionExpression.Values = new object[] { ownerid };//赋值
ConditionExpression conditions = new ConditionExpression();
conditions.AttributeName = "statecode";
conditions.Operator = ConditionOperator.Equal;
conditions.Values = new object[] { statecode };
ConditionExpression conditionTime = new ConditionExpression();
conditionTime.AttributeName = "new_popuptime";
conditionTime.Operator = ConditionOperator.OnOrBefore;
conditionTime.Values = new string[] { currenttime.ToString() };
//筛选
FilterExpression filters = new FilterExpression();
filters.FilterOperator = LogicalOperator.And;
filters.Conditions.Add(conditionExpression);
filters.Conditions.Add(conditions);
filters.Conditions.Add(conditionTime);
//排序
OrderExpression orders = new OrderExpression();
orders.AttributeName = "new_popuptime";
orders.OrderType = OrderType.Ascending;
//查询集合信息
BusinessEntityCollection businessEntityCollection = Query.instance.QueryByExpression(service, entityName, filters, new ArrayList { orders }, null, null, true, new string[] { "new_entityname", "new_primaryid", "new_popupsid" });
if (businessEntityCollection.BusinessEntities.Count < 0) return "";
string nentityName = string.Empty; Guid primaryid = Guid.Empty;
Guid popupid = Guid.Empty;
string entitynameAndId = string.Empty;
if (businessEntityCollection.BusinessEntities.Count > 0)
{
foreach (var item in businessEntityCollection.BusinessEntities)
{
DynamicEntity d = (DynamicEntity)item;
if (d.Properties.Contains("new_entityname")) nentityName = d.Properties["new_entityname"].ToString();
if (d.Properties.Contains("new_primaryid")) primaryid = new Guid(d.Properties["new_primaryid"].ToString());
if (d.Properties.Contains("new_popupsid")) popupid = ((Key)d.Properties["new_popupsid"]).Value;
entitynameAndId += nentityName + "," + primaryid + "," + popupid + ":";//返回实体名加ID
}
}
return entitynameAndId;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 弹出Popus信息之后,禁用
/// <summary>
/// 弹出Popus信息之后,禁用
/// </summary>
/// <param name="service">服务</param>
/// <param name="targetid">目标ID</param>
/// <param name="entityname">实体名</param>
public void SetState(CrmService service,Guid targetid)
{
try
{
Moniker target = new Moniker();
target.Id = targetid;
target.Name = "new_popups";
Query.instance.SetStateDynamicEntity(service, target, ContactState.Inactive.ToString(), 2);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
}
popup.js
//----------------------------------------------------
//JQuery获取PopupsHandler.ashx的值,弹出窗体。
//----------------------------------------------------
function PopupsWindow(ownerid) {
//debugger;
$.post({ url: '../Tanhua/PopupsHandler.ashx'
, data: { "ownerid": ownerid}//负责人Id
, success: function (data) {
if (data != null && data != undefined && data != "") {
var strs = data.split(':');
for (var i = 0; i < strs[i].length; i++) {
var sp = strs[i].split(',');
var strEntityName = sp[0];
var strEntityId = sp[1];
var strpopusId = sp[2];
if (strEntityName != null && strEntityName != undefined && strEntityName != "" && strEntityId != null && strEntityId != undefined && strEntityId != "") {
var newurl = "" + serverURL + "/" + orgName + "/activities/" + strEntityName + "/edit.aspx?id=" + strEntityId + "";
window.open(newurl);
}
if (strpopusId != null && strpopusId != undefined && strpopusId !="") {
GetPouppid(strpopusId); //弹出窗体后,则将该记录禁用(状态为0)。
}
}
}
}
});
}
//给弹出窗体的记录禁用
function GetPouppid(poupid) {
$.ajax({ url: 'PopupsHandler.ashx'
, data: { "poupid": poupid }
, success: function (data) {
if (data != null && data != undefined && data != "") {
var id = data;
}
}
});
}
window.setInterval(function () {
// PopupsWindow("{4D21D02D-EA18-E211-BAD3-00155D02F004}");
},1000);