admin管理员组

文章数量:1410682

I am having a script as below:-

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){

}
}

I need to call this script each time after a SharePoint Calendar Item is created.

Can anyone help?

I am having a script as below:-

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){

}
}

I need to call this script each time after a SharePoint Calendar Item is created.

Can anyone help?

Share Improve this question asked Feb 20, 2015 at 11:49 Sharique Hussain AnsariSharique Hussain Ansari 1,4561 gold badge12 silver badges22 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4 +50

The following JavaScript example demonstrates how to register event that will be triggered after Calendar item is created:

//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

In your case the function getColorValue could be invoked from onEventCreated, for example:

function onEventCreated(){
    getColorValue (id,title);     
}

How to apply changes

  1. Switch the page into Edit mode
  2. Add Script Editor webpart into page.
  3. Put the specified code by wrapping it using script tag code into the Script Editor, for example: <script type="text/javascript">{JavaScipt code goes here}</script>
  4. Save the page

Results

Create an Event Receiver with List Item Events for type and Calendar for Source then check 'An item is being added' in handling the event.

Then in the code behind of your Event Receiver:

public override void ItemAdding(SPItemEventProperties properties)
       {
           base.ItemAdding(properties);

           //Call your function through this
           Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "MyFunction()", true);
       }

Hope that helps :)

I know this is an old question, but there is an issue with the solution given.

I had a requirement to implement an workaround to the missing Resource Reservation feature in Sharepoint online. It was not possible to use an approach more suitable to the Modern Experience, due its dependency of Azure (no Azure subscription available for it), so I use Sharepoint API calls to perform the the same functionality of Resource Reservation.

To use the Sharepoint API, some Ajax calls were needed. But I observed that two calls were executed for each request.

The point is when you register calendar events, it is needed to attach the click event using one, as seen below, to prevent the click event to be fired more than once.

//register Calendar events 
$('body').one('click', 'div#AsynchronousViewDefault_CalendarView', function() {
    registerCalendarEventOnItemCreated(onEventCreated);
});

本文标签: sharepointCall Javascript After Saving Calendar ItemStack Overflow