我有一个脚本如下:-
function getColorValue(aId,atitle) {
try{
var clientContext = new SP.ClientContext();
var oWebsite = clientContext.get_web();
var oList = oWebsite.get_lists().getByTitle('Item type');
var oListItem = oList.getItemById(parseInt(aId));
clientContext.load(oListItem);
clientContext.executeQueryAsync(function () {
var listItem = oListItem;
var colorname = listItem.get_item('Color_x0020_Name');
if (typeof colorname != 'undefined') {
if (colorname != null) {
$("div[title$='" + atitle + "']").css("background-color", colorname);
}
}
}, onColorQueryFail);
}
catch(e){
}
}
我需要在每次创建 SharePoint 日历项目后调用此脚本。
有人能帮忙吗?
最佳答案
以下 JavaScript 示例演示了如何注册将在创建日历项后触发的事件:
//custom handler that will be triggered after Calendar item is created
function onEventCreated(){
alert('Event has been created...');
}
function registerCalendarEventOnItemCreated(event)
{
var currentCtx = getCurrentContextInfo();
var calContainer = SP.UI.ApplicationPages.CalendarInstanceRepository.lookupInstance(currentCtx.ctxId);
for(var name in calContainer) {
if(calContainer.hasOwnProperty(name)) {
var p = calContainer[name];
if(p instanceof SP.UI.ApplicationPages.CalendarNewFormDialog) {
p.get_events().addHandler("newitemcreated",event);
}
}
}
}
//get current context info
function getCurrentContextInfo()
{
var currentListId = new SP.Guid(_spPageContextInfo.pageListId);
for(var ctxKey in g_ctxDict){
var curCtx = g_ctxDict[ctxKey];
if(curCtx.listName == currentListId.toString()){
return curCtx;
}
}
return null;
}
//register Calendar events
$('body').on('click', 'div#AsynchronousViewDefault_CalendarView', function() {
registerCalendarEventOnItemCreated(onEventCreated);
});
Has been tested against SharePoint 2013/Online
在你的例子中函数 getColorValue
可以从 onEventCreated
调用,例如:
function onEventCreated(){
getColorValue (id,title);
}
如何应用更改
- 将页面切换到编辑模式
- 添加
Script Editor
webpart 进入页面。 - 将指定的代码用
script
包裹起来标记代码到脚本编辑器中,例如:<script type="text/javascript">{JavaScipt code goes here}</script>
- 保存页面
结果
关于javascript - 保存日历项后调用 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28628286/