Change anchor link url in sharepoint calender

here am unsing sharepoint calender to display events.
The calender list is inhereted from event content type.

for every event there are several columns like event name, event link(URL), Event Date...etc.
for some events there may be some url in link column some may be empty.

So in calender if i click the event, suppose if that event has the LINK with url then i have to redirect to the new url,
if the link url is empty then i have to redirect to the event url.

here when am clilcking on anchor tag in calender am calling a rest api function to get the link value of that event.
am checking the link value , if value in null then am redirecting to event details and the value is not null then am redirecting to the link which am get from rest api call.

here am using two mail functions which are
1)e.stopPropagation();
means it will stop the redirecting value which is assigned initially.

2)e.preventDefault();
means it will stop the default value of the anchor tag href value.

this code will be inserted in additional page head tag.

here is my code

<script type="text/javascript">
  ExecuteOrDelayUntilScriptLoaded(
    MyCalendarHook,
    "SP.UI.ApplicationPages.Calendar.js");
    function MyCalendarHook() {    
    var _patchCalendar = function() {      
       
        $('.ms-acal-rootdiv .ms-acal-item').click(function (e) {
            var itemId = $('a', $(this)).attr('href').split('?ID=')[1];
            //alert('Item with ID ' + itemId + ' was clicked!');
           
             $.ajax({
  url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('2017 Calender')/Items?$select=Link&$filter=ID eq '"+itemId +"'",
  type: "GET",
  async: false,//new changed
  headers: {"accept": "application/json;odata=verbose"},
  success: function (data) {
  $.each(data.d.results, function(index, item){
//chacking the value is null or not
if(item.Link!=null)
{
e.stopPropagation();
    e.preventDefault();
  window.location.href =item.Link;//"https://kellyservices1.sharepoint.com/sites/creative/hqcsr/Lists/2017v2/Item/editifs.aspx?List=c8c4fc65-151d-4c5b-a113-11724d79db79&amp;ID=3";
              }
           });
   
  },
  error: function (xhr) {
     alert(xhr.status + ': ' + xhr.statusText);
  }
});            

e.preventDefault();
            return false;
        });
    };

    var _onItemsSucceed = SP.UI.ApplicationPages.CalendarStateHandler.prototype.onItemsSucceed;
    SP.UI.ApplicationPages.CalendarStateHandler.prototype.onItemsSucceed = function($p0, $p1) {
        _onItemsSucceed.call(this, $p0, $p1);
        _patchCalendar();
    };
}
  </script>

Comments

Popular posts from this blog

what is Event Cache table in sharepoint

CAML Query syntax and options in SharePoint