This project has moved. For the latest updates, please go here.

unable to set value of lookup field by ID number in querystring

Jun 2, 2014 at 5:39 PM
Edited Jun 2, 2014 at 5:47 PM
Great solution. I am attempting to use SPUtility.js to "pre-populate" a lookup field (Department name) in newform.aspx.
I am using SharePoint 2010 and IE10 (have also seen same result in Chrome).

I am able to use the query string to populate the lookup field using the column's text value (e.g., Finance) but when I pass the item's ID (e.g, 1) for the value, it is not found (the lookup dropdown stays on "(None)") .

I can get it to work properly by pass the item's ID via the "helloworld" method but not a query string.

To ensure that it was being treated as a number, I confirmed that I could populate a numeric field from the same query string parameter.

Thoughts?

Here is what I am using:

<script src="/sites/CorpDev4/Testsite/autopopulation%20scripts/jquery-1.11.1.min.js"></script>
<script src="/sites/CorpDev4/Testsite/autopopulation%20scripts/sputility.min.js"></script>
<script> // url parsing from http://stackoverflow.com/a/2880929/98933 var urlParams; (window.onpopstate = function () { var match, pl = /\+/g, // Regex for replacing addition symbol with a space search = /([^&=]+)=?([^&]*)/g, decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); }, query = window.location.search.substring(1); urlParams = {}; while (match = search.exec(query)) urlParams[decode(match[1])] = decode(match[2]); })(); // wait for the window to load $(window).load(function () { try { var urlValue = urlParams['SelectedID']; SPUtility.GetSPField('Department').SetValue(urlValue); //works if text value on querystring but not number SPUtility.GetSPField('Numericcolumn').SetValue(urlValue); SPUtility.GetSPField('ReceiveText').SetValue('2nd column value provided by script').MakeReadOnly(); } catch (ex) { alert(ex.toString()); } }); </script>
Coordinator
Jun 2, 2014 at 11:23 PM
Kim,
Try this:
<script src="/sites/CorpDev4/Testsite/autopopulation%20scripts/jquery-1.11.1.min.js"></script>
<script src="/sites/CorpDev4/Testsite/autopopulation%20scripts/sputility.min.js"></script>
<script>
    // url parsing from http://stackoverflow.com/a/2880929/98933 
    var urlParams;
    (window.onpopstate = function () {
        var match, pl = /\+/g, // Regex for replacing addition symbol with a space 
            search = /([^&=]+)=?([^&]*)/g,
            decode = function (s) {
                return decodeURIComponent(s.replace(pl, " "));
            },
            query = window.location.search.substring(1);
        urlParams = {};
        while (match = search.exec(query)) urlParams[decode(match[1])] = decode(match[2]);
    })();
    // wait for the window to load 
    $(window).load(function () {
        try {
            var urlValue = parseInt(urlParams['SelectedID'], 10);
            SPUtility.GetSPField('Department').SetValue(urlValue); //works if text value on querystring but not number SPUtility.GetSPField('Numericcolumn').SetValue(urlValue); 
            SPUtility.GetSPField('ReceiveText').SetValue('2nd column value provided by script').MakeReadOnly();
        } catch (ex) {
            alert(ex.toString());
        }
    });
</script>
The key is converting the urlValue from a string to an integer using parseInt (10 because javascript is weird and we work in base 10). This is because SPUtility checks if it is an integer in order to tell if it should set using ID (or if it is a string then it will set using the text).

Hopefully this helps!
Thanks,
Kit
Jun 3, 2014 at 9:05 PM
That did it. Thanks.
Kim