Second Life of a Hungarian SharePoint Geek

May 29, 2016

Reference Default Calendar Scopes in a URL

Filed under: JavaScript, SP 2013, Views — Tags: , , — Peter Holpar @ 05:41

As you know, you can create calendar views with various default scopes (e.g. Day / Week / Month, see screenshot below).

image

It means a single .aspx page per view, for example CalendarDay.aspx, CalendarWeek.aspx, CalendarMonth.aspx. You can then link these pages in HTML hyperlinks or send a link in mail by referring the given page.

But if you try to avoid creating and administering these three views for each of your calendars, is it possible to have only a single, universal page (like Calendar.aspx), and still have the ability to create a URL for the page that determines, with which scope the calendar would be displayed by default?

Unfortunately, it is not possible out-of-the-box, but with some scripting we can find a way.

Add a Script Editor Web Part (category: Media and Content) to the calendar view page after the calendar itself. Include the following script in the web part:

  1. <script type="text/javascript">
  2.  
  3. function getQueryStringParameter(paramToRetrieve) {
  4.     var params =
  5.     document.URL.split("?")[1].split("&");
  6.     var strParams = "";
  7.     for (var i = 0; i < params.length; i = i + 1) {
  8.         var singleParam = params[i].split("=");
  9.         if (singleParam[0] == paramToRetrieve)
  10.             return singleParam[1];
  11.     }
  12. }
  13.  
  14. var timer;
  15.  
  16. function registerMoveView() {
  17.  
  18.     var cirInstance = SP.UI.ApplicationPages.CalendarInstanceRepository.firstInstance();
  19.  
  20.     if (cirInstance) {
  21.         MoveView(startView);
  22.         window.clearInterval(timer);
  23.     }
  24.     else if (!timer) {
  25.         timer = window.setInterval(registerMoveView, 100);
  26.     }
  27. }
  28.  
  29. var viewNames = ["month", "week", "day"];
  30.  
  31. var startView = getQueryStringParameter("StartView")
  32.  
  33. if (viewNames.indexOf(startView) != -1) {
  34.     SP.SOD.executeOrDelayUntilScriptLoaded(registerMoveView, "sp.ui.applicationpages.calendar.js");
  35. }
  36.  
  37. </script>

After you save the changes on the page, one can access the calendar with the daily scope using a URL like this:

http://YourSharePoint/subweb/Lists/Calendar/calendar.aspx?StartView=day

One can access the monthly and weekly scopes as well via URL, simply by using the StartView query string values “month” and “week” respectively.

Note 1: The query string values “month”, “week” and “day” are strictly case sensitive, using a value like “Day” displays the default scope defined for the view (see the screenshot above).

Note 2: The sample above assumes you had a single calendar view on your you page. If you happen to have more, it would affect only the first one, see call to the firstInstance method in the script.

Note 3: The calendar instance is first available in the script after the real default scope completely rendered by the browser. We introduced a timer to overcome that issue, but if your browser is slow, you can still experience a blinking effect when switching to the new “default” scope, the one you selected via the StartView query string parameter.

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: