Pages

Tuesday, 5 July 2016

DropDown from Sharepoint List with redirect onclick

<script>
    // Site collection url. Whatever the value given here the current site collection that
    // the web part is running is picked automaticaly. Not sure about how it happens.
    // Need to investigate more.
    var siteUrl = "XXXXXXXX";
    // Creating object to hold the item collection with the Id and Value. To enable
    // intellisence while coding(for the ItemContainer), uncomment the below line and
    // comment the right below one.
    //var ItemContainer = { ItemList : [{ Id: "", Value: "" }]};
    var ItemContainer = { ItemList: [] };

  function retrieveListItems() {

        var clientContext = new SP.ClientContext(siteUrl);
        // Get the SharePoint list by giving the display name of the list.
        var oList = clientContext.get_web().get_lists().getByTitle('XXXXXXXXX');
        // To retreive all items use the predefined createAllItemsQuery operation.
        var camlQuery = SP.CamlQuery.createAllItemsQuery();
        this.collListItem = oList.getItems(camlQuery);
        clientContext.load(collListItem);
        clientContext.executeQueryAsync(
            Function.createDelegate(this, this.onListDataLoadQuerySucceeded),
            Function.createDelegate(this, this.onListDataLoadQueryFailed));
    }

// Callback function if the item retrieval Async call get successful.
    function onListDataLoadQuerySucceeded(sender, args) {
        var listItemInfo = "";
        var listItemEnumerator = collListItem.getEnumerator();
        while (listItemEnumerator.moveNext()) {
            var oListItem = listItemEnumerator.get_current();
            // Fill a json object with Id and Value properties.
            var tempItem = { Id: oListItem.get_id(), Value: oListItem.get_item('Title'),val: oListItem.get_item('LinkLocation').get_url() };
            ItemContainer.ItemList.push(tempItem);
        }
        // Fill the drop down with retrieved data.
        fillDropDown();
    }
    // Callback function if item retrieval failes.
    function onListDataLoadQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }

// Fill  the drop down with the retrieved list item data.
    function fillDropDown() {
        var ddlCategory = document.getElementById('ddlCategory');
        if (ddlCategory != null) {
            for (var i = 0; i < ItemContainer.ItemList.length; i++) {
                var theOption = new Option;
                theOption.value = ItemContainer.ItemList[i].val;
                theOption.text = ItemContainer.ItemList[i].Value;
                ddlCategory.options[i] = theOption;
            }
        }
    }

function mohit() { alert("hello"); }
</script>

<body onload="retrieveListItems();">
<center><select id="ddlCategory" onchange="window.top.location.href = this.value;"></select> </center>
</body>

No comments:

Post a Comment