0

so I've been tasked by my company to basically start using Sharepoint in a more advanced manner. My first major task I'm trying to work through is building an add-in for a pop-up that basically does the same thing as the Alert Me button in the edit page.

I feel like I'm really close to creating my first alert, I have a valid reference to the list and can print out its contents, and I believe I have the user, but when I go to set the alert, I get an error:

"List does not exist.

The page you selected contains a list that does not exist. It may have been deleted by another user."

Here's my code, it's basically a compilation of different tutorials.

    'use strict';

ExecuteOrDelayUntilScriptLoaded(initializePage, "sp.js");

var Items = null;
var listItems;
var hostweburl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));
var appweburl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl'));

function getQueryStringParameter(paramToRetrieve) {  
    var params = document.URL.split("?")[1].split("&");  
    for (var i = 0; i < params.length; i = i + 1) {  
        var singleParam = params[i].split("=");  
        if (singleParam[0] == paramToRetrieve) return singleParam[1];  
    }  
}  

function initializePage()
{

    // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
    $(document).ready(function () {

        var context = new SP.ClientContext(appweburl);
        var ContextSite = new SP.AppContextSite(context, hostweburl);
        var web = ContextSite.get_web();
        context.load(web);
        var list = web.get_lists().getByTitle('AlertList');
        var user = context.get_web().get_currentUser();
        var alerts = user.get_alerts();

        $('.set_alert').click(function () {



            var caml = new SP.CamlQuery.createAllItemsQuery();
            Items = list.getItems(caml);
            context.load(Items);
            context.load(user);
            context.executeQueryAsync(onExecuteSuccess, onExecuteFail);

            debugger;


        });

        // This function is executed if the above call is successful
        // It replaces the contents of the 'message' element with the user name
        function onExecuteSuccess() {
            debugger;
            var listInfo = '';

            var listEnumerator = Items.getEnumerator();

            while (listEnumerator.moveNext()) {

                var oList = listEnumerator.get_current();

                listInfo += 'Title: ' + oList.get_item('Title') + '<br />';

            }

            let alertTime = new Date();
            alertTime.setHours(alertTime.getHours() + 24);

            let notify = new SP.AlertCreationInformation;

            debugger;

            notify.set_title('This is a test');
            notify.set_alertFrequency(SP.AlertFrequency.daily);
            notify.set_alertType(SP.AlertType.list);
            notify.set_list(list);
            notify.set_deliveryChannels(SP.AlertDeliveryChannel.email);
            notify.set_alwaysNotify(true);
            notify.set_status(SP.AlertStatus.on);
            notify.set_alertTime(alertTime);
            //notify.set_user(user.get_title());
            notify.set_user(user);
            notify.set_eventType(SP.AlertEventType.all);

            //// 0 = Anything Changes
            //// 1 = Someone else changes a document
            //// 2 = Someone else changes a document created by me
            //// 3 = Someone else changes a document modified by me
            notify.Filter = '0';

            //debugger;
            alerts.add(notify);
            user.update();

            context.executeQueryAsync(null, onExecuteFail);

            $("#message").html(listInfo);
        }

        // This function is executed if the above call fails
        function onExecuteFail(sender, args) {
            debugger;
            console.log('Failure. Error:' + args.get_message());
        }
    });
}

Any help with this would be much appreciated, been struggling for a few days now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.