之前本人的博客介绍了《sharepoint 2010自定义访问日志列表设置移动终端否和客户端访问系统等计算列的公式》,那如何通过Jquery提交访问日志到自定义的SharePoint的访问日志列表中。本文介绍通过Jquery ajax访问SharePoint 2010的list web服务并添加列表记录,这样就无需后台代码就可以实现操作列表,IP地址的解析采用搜狐提供的以JSON格式返回的IP地址数据:http://pv.sohu.com/cityjson?ie=utf-8,具体下图:
具体的js代码,如下图:
<script type="text/javascript" src="http://pv.sohu.com/cityjson?ie=utf-8" ></script>
Date.prototype.Format = function (fmt) { //author: meizz
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
function CreateNewItem(user) {
var vpage = window.location.href;
var cip = returnCitySN['cip'];
var city= returnCitySN['cname'];
var useragent = navigator.userAgent;
var Sys = {};
var ua = navigator.userAgent.toLowerCase();
var brw;
(brw = ua.match(/msie ([\d.]+)/)) ? Sys.ie = brw[1] :
(brw = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = brw[1] :
(brw = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = brw[1] :
(brw = ua.match(/opera.([\d.]+)/)) ? Sys.opera = brw[1] :
(brw = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = brw[1] : 0;
var time2 = new Date().Format("yyyy-MM-dd hh:mm:ss");
var title = $(document).find("title").text();
var batch =
"<Batch OnError=\"Continue\"> \
<Method ID=\"1\" Cmd=\"New\"> \
<Field Name=\"Title\">" + user + "</Field> \
<Field Name=\"UserName\">" + user + "</Field> \
<Field Name=\"VisitPage\">" + vpage + "</Field> \
<Field Name=\"VisitTime\">" + time2 + "</Field> \
<Field Name=\"VisitIPAddress\">" + cip + "</Field> \
<Field Name=\"VisitIPCity\">" + city + "</Field> \
<Field Name=\"UserAgent\">" + useragent + "</Field> \
<Field Name=\"Browser\">" + brw + "</Field> \
<Field Name=\"VisitPageName\">"+title+"</Field> \
</Method> \
</Batch>";
var soapEnv =
"<?xml version=\"1.0\" encoding=\"utf-8\"?> \
<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" \
xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"> \
<soap:Body> \
<UpdateListItems xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\"> \
<listName>用户访问日志</listName> \
<updates> \
" + batch + "</updates> \
</UpdateListItems> \
</soap:Body> \
</soap:Envelope>";
$.ajax({
url: "http://sharepoint.contoso.uat/MOA/_vti_bin/lists.asmx",
beforeSend: function(xhr) {
xhr.setRequestHeader("SOAPAction",
"http://schemas.microsoft.com/sharepoint/soap/UpdateListItems");
},
type: "POST",
dataType: "xml",
data: soapEnv,
complete: processResult,
contentType: "text/xml; charset=utf-8"
});
}
function processResult(xData, status) {
//alert(xData);
}
用这种方式还可以实现列表的更新、删除等操作,这样就可以通过前端JS代码实现操作后端的列表了,无需编写后台代码。
本博客为软件人生原创,欢迎转载,转载请标明出处:http://www.cnblogs.com/nbpowerboy/p/3363574.html 。演绎或用于商业目的,但是必须保留本文的署名软件人生(包含链接)。如您有任何疑问或者授权方面的协商,请给我留言。 |