Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Unable to get property 'Selection' of undefined or null reference while developing a SharePoint Add-in with CSOM

$
0
0

Hi All,

I amdeveloping a SharePoint add in and provisioned a custom action on ribbon control. I am usingCSOM and if users multiple items in a SharePoint list, and click on custom action, they should be prompted multiple items they have selected.

However, the App.js code this giving this error while retrieving items from SharePoint list.  Unable to get property 'Selection' of undefined or null reference


and also giving an error saying List does not exist at the with URL. The URL is in fact  my Add-in URL.  

This is COMPLETEApp.js code.

'use strict';

ExecuteOrDelayUntilScriptLoaded(initializePage, "sp.js");



function initializePage() {
    var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));

    var context = SP.ClientContext.get_current();

    var hostContext = new SP.AppContextSite(context, hostUrl);
    var user = context.get_web().get_currentUser();

    alert('Host URL is' + hostUrl);


    // 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 () {
        getUserName();
    });

    // This function prepares, loads, and then executes a SharePoint query to get the current users information
    function getUserName() {
        context.load(user);
        context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail);
    }

    // This function is executed if the above call is successful
    // It replaces the contents of the 'message' element with the user name
    function onGetUserNameSuccess() {
        $('#message').text('Hello ' + user.get_title());
    }

    //Get Selected Items
    getSelectedItems(function (items) {
        for (var i = 0 ; i < items.length; i++) {
            console.log(items[i].get_item('Title'));

        }

    }, function (sender, args) {
        console.log('An error occured: ' + args.get_message());
    });


    // This function is executed if the above call fails
    function onGetUserNameFail(sender, args) {
        alert('Failed to get user name. Error:' + args.get_message());

    }
}


function getSelectedItems() {
    var selectedItems = null;
    var context = SP.ClientContext.get_current();
    var currlist = context.get_web().get_lists().getByTitle("Approvers");
    context.load(currlist);


    if (selectedItems != null && selectedItems != "") {
        alert('No Items are selected and value is null/empty');
    }
    else
    {
        var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);
        return selectedItems;
    }

}



function getQueryStringParameter(urlParameterKey) {

    var params = document.URL.split('?')[1].split('&');

    var strParams = '';

    for (var i = 0; i < params.length; i = i + 1) {

        var singleParam = params[i].split('=');

        if (singleParam[0] == urlParameterKey)

            return decodeURIComponent(singleParam[1]);

    }

}

Any help would be appreciated.
Thanks. 

 


Sandy



Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>