jquery定时ajax调用web service
2009-09-30 10:42 Virus-BeautyCode 阅读(3067) 评论(0) 编辑 收藏 举报jquery:1.3.2
vs:2008
最经一直在做moss方面的开发,客户也上了exchange,也做了moss和exchange的sso,但是有新邮件还是要点击进入邮箱才可以查看。客户说要是有新邮件的时候可以提示一下,以为我们以前做过有【每日提醒】,包括每天的日程、文档、邮件,好吧,如果有新的就让【每日提醒】这几个字闪动吧。
思路就是用ajax定时查看有无新内容,如果有的用一个定时器让文字闪动(通过变化文件的color实现),如果没有就关闭定时器,恢复文字的颜色。这里邮件的获取用到了exchange的web service http://mail.moss.com/ews/exchange.asmx ,结合jquery和一般处理程序ashx的ajax。
一般处理程序的代码如下
Handler1.ashx
using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.IO;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Portal.SingleSignon;
using Microsoft.SharePoint.Portal.SingleSignonAdministration;
using System.Net;
using WebApplication1.mtn.gfdx.cds.ucs.mail;
using Microsoft.SharePoint.Portal;
using System.Text;
using System.Text.RegularExpressions;
namespace WebApplication1
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Handler1 : IHttpHandler
{
private const string exchangeMailWebRefUrl = @"http://mail.moss.com/ews/exchange.asmx";
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
bool resultjson = false ;
resultjson = GetMail();
string reStr;
if (resultjson)
reStr = string.Format("{{contains:'{0}'}}", "true");
else
reStr = string.Format("{{contains:'{0}'}}", "false");
context.Response.Write(reStr);
}
private void WriteFile(string str)
{
try
{
File.AppendAllText(@"c:\test.txt", str);
File.AppendAllText(@"c:\test.txt", Environment.NewLine);
}
catch { }
}
/// <summary>
/// 获取是否包含未读Exchange邮件信息
/// </summary>
/// <param name="table"></param>
public bool GetMail()
{
bool result = false;
//DataRow row;
try
{
ICredentials creds = new NetworkCredential("administrator", "123", "moss");
ExchangeServiceBinding exchangeServer = new ExchangeServiceBinding();
exchangeServer.Credentials = creds;
exchangeServer.Url =exchangeMailWebRefUrl
exchangeServer.Credentials = creds;
DistinguishedFolderIdType[] folderIdArray = new DistinguishedFolderIdType[1];
folderIdArray[0] = new DistinguishedFolderIdType();
folderIdArray[0].Id = DistinguishedFolderIdNameType.inbox;
PathToUnindexedFieldType ptuftDisplayName = new PathToUnindexedFieldType();
ptuftDisplayName.FieldURI = UnindexedFieldURIType.folderDisplayName;
PathToExtendedFieldType pteftComment = new PathToExtendedFieldType();
pteftComment.PropertyTag = "0x3004";
pteftComment.PropertyType = MapiPropertyTypeType.String;
GetFolderType myFolderType = new GetFolderType();
myFolderType.FolderIds = folderIdArray;
myFolderType.FolderShape = new FolderResponseShapeType();
myFolderType.FolderShape.BaseShape = DefaultShapeNamesType.IdOnly;
myFolderType.FolderShape.AdditionalProperties = new BasePathToElementType[2];
myFolderType.FolderShape.AdditionalProperties[0] = ptuftDisplayName;
myFolderType.FolderShape.AdditionalProperties[1] = pteftComment;
GetFolderResponseType myFolder = exchangeServer.GetFolder(myFolderType);
FolderInfoResponseMessageType firmtInbox = (FolderInfoResponseMessageType)myFolder.ResponseMessages.Items[0];
PathToUnindexedFieldType ptuftSubject = new PathToUnindexedFieldType();
ptuftSubject.FieldURI = UnindexedFieldURIType.itemSubject;
PathToUnindexedFieldType ptuftBody = new PathToUnindexedFieldType();
ptuftBody.FieldURI = UnindexedFieldURIType.itemAttachments;
PathToExtendedFieldType ppteftFlagStatus = new PathToExtendedFieldType();
ppteftFlagStatus.PropertyTag = "0x1090";
ppteftFlagStatus.PropertyType = MapiPropertyTypeType.Integer;
FindItemType findItemRequest = new FindItemType();
findItemRequest.Traversal = ItemQueryTraversalType.Shallow;
findItemRequest.ItemShape = new ItemResponseShapeType();
findItemRequest.ItemShape.BaseShape = DefaultShapeNamesType.Default;
findItemRequest.ParentFolderIds = new FolderIdType[1];
findItemRequest.ParentFolderIds[0] = firmtInbox.Folders[0].FolderId;
FindItemResponseType firt = exchangeServer.FindItem(findItemRequest);
MessageType mt = new MessageType();
//int newMail = 0;
//int totalMail = 0;
foreach (FindItemResponseMessageType firmtMessage in firt.ResponseMessages.Items)
{
if (firmtMessage.RootFolder.TotalItemsInView > 0)
{
foreach (ItemType it in ((ArrayOfRealItemsType)firmtMessage.RootFolder.Item).Items)
{
mt = it as MessageType;
if (mt.IsRead == false)
{
result = true;
break;
}
}
}
}
}
#region
catch (SingleSignonException ssoex)
{
}
catch (Exception ex)
{
}
#endregion
return result;
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
前台的JavaScript代码如下using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.IO;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Portal.SingleSignon;
using Microsoft.SharePoint.Portal.SingleSignonAdministration;
using System.Net;
using WebApplication1.mtn.gfdx.cds.ucs.mail;
using Microsoft.SharePoint.Portal;
using System.Text;
using System.Text.RegularExpressions;
namespace WebApplication1
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Handler1 : IHttpHandler
{
private const string exchangeMailWebRefUrl = @"http://mail.moss.com/ews/exchange.asmx";
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
bool resultjson = false ;
resultjson = GetMail();
string reStr;
if (resultjson)
reStr = string.Format("{{contains:'{0}'}}", "true");
else
reStr = string.Format("{{contains:'{0}'}}", "false");
context.Response.Write(reStr);
}
private void WriteFile(string str)
{
try
{
File.AppendAllText(@"c:\test.txt", str);
File.AppendAllText(@"c:\test.txt", Environment.NewLine);
}
catch { }
}
/// <summary>
/// 获取是否包含未读Exchange邮件信息
/// </summary>
/// <param name="table"></param>
public bool GetMail()
{
bool result = false;
//DataRow row;
try
{
ICredentials creds = new NetworkCredential("administrator", "123", "moss");
ExchangeServiceBinding exchangeServer = new ExchangeServiceBinding();
exchangeServer.Credentials = creds;
exchangeServer.Url =exchangeMailWebRefUrl
exchangeServer.Credentials = creds;
DistinguishedFolderIdType[] folderIdArray = new DistinguishedFolderIdType[1];
folderIdArray[0] = new DistinguishedFolderIdType();
folderIdArray[0].Id = DistinguishedFolderIdNameType.inbox;
PathToUnindexedFieldType ptuftDisplayName = new PathToUnindexedFieldType();
ptuftDisplayName.FieldURI = UnindexedFieldURIType.folderDisplayName;
PathToExtendedFieldType pteftComment = new PathToExtendedFieldType();
pteftComment.PropertyTag = "0x3004";
pteftComment.PropertyType = MapiPropertyTypeType.String;
GetFolderType myFolderType = new GetFolderType();
myFolderType.FolderIds = folderIdArray;
myFolderType.FolderShape = new FolderResponseShapeType();
myFolderType.FolderShape.BaseShape = DefaultShapeNamesType.IdOnly;
myFolderType.FolderShape.AdditionalProperties = new BasePathToElementType[2];
myFolderType.FolderShape.AdditionalProperties[0] = ptuftDisplayName;
myFolderType.FolderShape.AdditionalProperties[1] = pteftComment;
GetFolderResponseType myFolder = exchangeServer.GetFolder(myFolderType);
FolderInfoResponseMessageType firmtInbox = (FolderInfoResponseMessageType)myFolder.ResponseMessages.Items[0];
PathToUnindexedFieldType ptuftSubject = new PathToUnindexedFieldType();
ptuftSubject.FieldURI = UnindexedFieldURIType.itemSubject;
PathToUnindexedFieldType ptuftBody = new PathToUnindexedFieldType();
ptuftBody.FieldURI = UnindexedFieldURIType.itemAttachments;
PathToExtendedFieldType ppteftFlagStatus = new PathToExtendedFieldType();
ppteftFlagStatus.PropertyTag = "0x1090";
ppteftFlagStatus.PropertyType = MapiPropertyTypeType.Integer;
FindItemType findItemRequest = new FindItemType();
findItemRequest.Traversal = ItemQueryTraversalType.Shallow;
findItemRequest.ItemShape = new ItemResponseShapeType();
findItemRequest.ItemShape.BaseShape = DefaultShapeNamesType.Default;
findItemRequest.ParentFolderIds = new FolderIdType[1];
findItemRequest.ParentFolderIds[0] = firmtInbox.Folders[0].FolderId;
FindItemResponseType firt = exchangeServer.FindItem(findItemRequest);
MessageType mt = new MessageType();
//int newMail = 0;
//int totalMail = 0;
foreach (FindItemResponseMessageType firmtMessage in firt.ResponseMessages.Items)
{
if (firmtMessage.RootFolder.TotalItemsInView > 0)
{
foreach (ItemType it in ((ArrayOfRealItemsType)firmtMessage.RootFolder.Item).Items)
{
mt = it as MessageType;
if (mt.IsRead == false)
{
result = true;
break;
}
}
}
}
}
#region
catch (SingleSignonException ssoex)
{
}
catch (Exception ex)
{
}
#endregion
return result;
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function todayReminderColor() {
var todayReminder = document.getElementById("todayReminder");
if (!todayReminder.style.color) {
todayReminder.style.color = "red";
}
if (todayReminder.style.color == "red") {
todayReminder.style.color = "black";
} else {
todayReminder.style.color = "red";
}
}
var timerReminder0 = undefined;
var timerReminderColor0 = undefined;
function todayReminder() {
$.get("Handler1.ashx",
function(data, textStatus) {
var color = document.getElementById("todayReminder").style.color;
if (data == "{contains:'true'}") {
if (timerReminderColor0 == undefined) {
timerReminderColor0 = setInterval("todayReminderColor();", 400);
//alert("New Timer is: " + timerReminderColor0);
}
//alert("New Timer is: " + timerReminderColor0);
} else {
if (timerReminderColor0 == undefined) {
//alert("no timerReminderColor0");
}
else {
//alert("Close Timer is: " + timerReminderColor0);
document.getElementById("todayReminder").style.color = "green";
clearInterval(timerReminderColor0);
timerReminderColor0 = undefined;
}
}
});
}
$(document).ready(function() {
timerReminder0 = setInterval("todayReminder()", 2000);
});
function stopTimer() {
if (timerReminder0 == "undefined")
clearInterval(timerReminder0);
if (timerReminderColor0 == "undefined")
clearInterval(todayReminderColor0);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<a id="todayReminder" href="#">今日提醒</a>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function todayReminderColor() {
var todayReminder = document.getElementById("todayReminder");
if (!todayReminder.style.color) {
todayReminder.style.color = "red";
}
if (todayReminder.style.color == "red") {
todayReminder.style.color = "black";
} else {
todayReminder.style.color = "red";
}
}
var timerReminder0 = undefined;
var timerReminderColor0 = undefined;
function todayReminder() {
$.get("Handler1.ashx",
function(data, textStatus) {
var color = document.getElementById("todayReminder").style.color;
if (data == "{contains:'true'}") {
if (timerReminderColor0 == undefined) {
timerReminderColor0 = setInterval("todayReminderColor();", 400);
//alert("New Timer is: " + timerReminderColor0);
}
//alert("New Timer is: " + timerReminderColor0);
} else {
if (timerReminderColor0 == undefined) {
//alert("no timerReminderColor0");
}
else {
//alert("Close Timer is: " + timerReminderColor0);
document.getElementById("todayReminder").style.color = "green";
clearInterval(timerReminderColor0);
timerReminderColor0 = undefined;
}
}
});
}
$(document).ready(function() {
timerReminder0 = setInterval("todayReminder()", 2000);
});
function stopTimer() {
if (timerReminder0 == "undefined")
clearInterval(timerReminder0);
if (timerReminderColor0 == "undefined")
clearInterval(todayReminderColor0);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<a id="todayReminder" href="#">今日提醒</a>
</form>
</body>
</html>