2
<script language="javascript" type="text/javascript">

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', MyPopUp);

function createCookie(name,value,days) {
var expires;
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        expires = "; expires="+date.toGMTString();
    }
    else {
expires = "";
}
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}

function MyPopUp() {
             var options = { url: '/ict-services/Pages/the-rule.aspx', title: '10 Day Rule', width: 800, height:  600 };
var visited = readCookie('visited');
if (!visited || visited !== "true") {
        createCookie('visited', "true", 1);
              SP.UI.ModalDialog.showModalDialog(options);
}
          }
          _spBodyOnLoadFunctionNames.push("MyPopUp()");


</script>

I need a modal dialog which will load as shown in the code below.

 <script language="javascript" type="text/javascript">

    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', yourFunction);

      function yourFunction() {
     var options = { url: '/_layouts/viewlsts.aspx', title: 'Title,Description, and Icon', width: 640, height:  400 };
           SP.UI.ModalDialog.showModalDialog(options);
       }

       _spBodyOnLoadFunctionNames.push("yourFunction()");

  </script> 

The script has to be written in such a way that the popup appears once a day. An example would be appreciated.

Ok...my update script including cookie that expires after I minute (this is for testing purposes - will change to 7 days on production)

 <script language="javascript" type="text/javascript">

 SP.SOD.executeFunc('sp.js', 'SP.ClientContext', MyPopUp);

 function createCookie(name,value,days) { var expires;
     if (days) {
         var date = new Date();
         date.setTime(date.getTime()+(days*24*60*60*1000));
         expires = "; expires="+date.toGMTString();
     }
     else { expires = ""; }
     document.cookie = name+"="+value+expires+"; path=/"; }

 function readCookie(name) {
     var nameEQ = name + "=";
     var ca = document.cookie.split(';');
     for(var i=0;i < ca.length;i++) {
         var c = ca[i];
         while (c.charAt(0)==' ') c = c.substring(1,c.length);
         if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
     }
     return null; }

 function eraseCookie(name) {
     createCookie(name,"",-1); }

  function MyPopUp() {
         var options = { url: '/new.aspx', title: 'new title', width: 800, height:  600 }; 
    var visited = readCookie('visited'); if (!visited || visited !== "true") {
    createCookie('visited', "true", 1);
          SP.UI.ModalDialog.showModalDialog(options); }
      }
         _spBodyOnLoadFunctionNames.push("MyPopUp()");


  </script>
4
  • you want to show the popup once a day for every user?
    – Taran Goel
    Commented Sep 23, 2015 at 5:41
  • It is amazing Mike!!, I searched on cookie once a day and found: stackoverflow.com/questions/8132173/show-div-once-a-day Commented Sep 23, 2015 at 6:08
  • 2
    @DannyEngelman If you know the existence, and functioning, of cookies it sure is easy to do that search. If you don't it is not so easy Commented Sep 23, 2015 at 6:22
  • Yes - I do need to show the popup once a day for every user
    – garvon-77
    Commented Sep 23, 2015 at 21:56

2 Answers 2

4

Create a list like ModalLogs with following column IsModalShownForToday

Now find a item where Created By equals current user and Created equals Today and IsModalShownForToday is true. If you do not find any item by this filter, then show the modal and save a new item to ModalLogs list. Put the script in your master page.

<script language="javascript" type="text/javascript">
         ExecuteOrDelayUntilScriptLoaded(yourFunction, 'SP.js');
         function yourFunction() {
             var options = { url: '/_layouts/viewlsts.aspx', title: 'Title, Description, and Icon', width: 640, height:  400 };
              if(modalLogExitsForToday()){
                      SP.UI.ModalDialog.showModalDialog(options);
                     }
          }

          _spBodyOnLoadFunctionNames.push("yourFunction()");

</script>

Right now I don't have the environment so that I am unable to give the code.

Update: As garvon-77 mentioned in comment that I he is quite new in scripting, so I am giving the script to do this. I am little bit busy today. So I could not refactor it. Anyway it will work.

<script language="javascript" type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(modalLogExitsForToday, 'SP.js');

function modalLogExitsForToday() {

    var query = "<View><Query><Where><And><And><Eq><FieldRef Name=\"IsModalShownForToday\" /><Value Type=\"Boolean\">1</Value></Eq><Eq><FieldRef Name=\"Author\" /><Value Type=\"Integer\"><UserID Type=\"Integer\" /></Value></Eq></And><Eq><FieldRef Name=\"Created\" /><Value Type=\"DateTime\"><Today /></Value></Eq></And></Where></Query></View>";

    var data = {
        query: {
            __metadata: {
                'type': 'SP.CamlQuery'
            },
            ViewXml: query
        }
    };

    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('ModalLogs')/getitems",
        type: "POST",
        headers: {
            "accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "content-Type": "application/json;odata=verbose"
        },
        data: JSON.stringify(data),
        success: function(data) {
            if (data.d.results.length == 0) {
                saveNewLog();
            }
        },
        error: function(error) {
            alert(JSON.stringify(error));
        }
    });
}

function saveNewLog() {
    var data = {
        __metadata: {
            'type': 'SP.Data.ModalLogsListItem'
        },
        IsModalShownForToday: true
    };

    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('ModalLogs')/items",
        type: "POST",
        headers: {
            "accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "content-Type": "application/json;odata=verbose"
        },
        data: JSON.stringify(data),
        success: function(data) {
            var options = {
                url: '/_layouts/viewlsts.aspx',
                title: 'Title, Description, and Icon',
                width: 640,
                height: 400
            };
            SP.UI.ModalDialog.showModalDialog(options);
        },
        error: function(error) {
            alert(JSON.stringify(error));
        }
    });
}

_spBodyOnLoadFunctionNames.push("modalLogExitsForToday()");
</script>
8
  • Why would you not want to use cookies for this?
    – Etheryte
    Commented Sep 23, 2015 at 8:47
  • Hey @Nit, I did not say that. I said it should work without cookie. I also mentioned that it could be bad solution. down vote was not expected. Commented Sep 23, 2015 at 8:51
  • 1
    localstorage in conjunction with a custom user profile property or a list like you say, so it does not matter if you use another device later. And the localstorage to only need to check once on the same device for property maybe. Commented Sep 23, 2015 at 9:35
  • Yes you are right @AndersAune. Same problem will be for cookie also. So I am updating my answer. Thanks Commented Sep 23, 2015 at 9:39
  • Thanks Atish this will be a good start for me - I'm going to test this out but not till next week. If anyone has a cookie solution I would love to see this as well. I do have questions - I know how to create lists and set in validation but I am new to scripting - this looks like I will need to create some script for this "Now find a item where Created By equals current user and Created equals Today and IsModalShownForToday is true. If you do not find any item by this filter, then show the modal and save a new item to ModalLogs list."
    – garvon-77
    Commented Sep 23, 2015 at 22:47
0

You can create a webpart with a personal property field and copy the script in a literal control. In the personal scope property will be stored current date. If the date is 'today', we can hide the literal, so there won't be model window. If the date is not 'today', we can assume that the user is login in first time today so we can enable the literal control and update personal field with current date.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.