QueryString and spBodyOnLoadFunctionNames

So SharePoint regularly utilizes pages (often in the Layouts folder) which prepopulate field values based on QueryString values (that is to say, values in the address are used to prefill data on the page). Unfortunately, such functionality is only included in the Microsoft maintained pages, and not in the user maintained pages.
Since it's a frequent need, the SharePoint Program Manager decided to write a blog entry to explain how the SharePoint team implements the functionality, so that the process may be repeated consistently. That blog entry is located at: http://blogs.msdn.com/sharepointdesigner/archive/2007/06/13/using-javascript-to-manipulate-a-list-form-field.aspx

Based on the blog entry (and a few other blogs on related information), I wrapped the code into some generic function calls to simplify the reuse.

Walking through the code...
_spBodyOnLoadFunctionNames ensures that the "fillDefaultValues" function will be called on Body Load (functionality provided by SharePoint) within fillDefaultValues (the primary function), it performs a getQueryStringParameters.

If the QueryStringParameters contains a value for the parameter ("Course", "Class"), use the setLookupFromFieldName to set the field ("Course, "Class") to the querystring value (vals["Course"], vals["Class"])

Finally, in some cases disable the field by calling setFieldDisabled(fieldname).

Additionally, getAccountName is used to identify the User control, and pull the account name... this is useful for populating People Picker fields.

Notes: unfortunately, disabling a field will FORCE the field to use its default value. This means that you CANNOT set the fields' value from querystring, AND disable the field. If any technique to perform the contrary is known, please reply with a sample.
Language:
JavaScript
Keywords:
SharePoint
Code Snippet

<script type="text/javascript">

    _spBodyOnLoadFunctionNames.push("fillDefaultValues");

    function
fillDefaultValues()
    {
        var vals = getQueryStringParameters();
        if (vals["Course"] != null)
        {
            setLookupFromFieldName(
"Course", vals["Course"]);
        }
        if (vals["Class"] != null)
        {
            setLookupFromFieldName(
"Class", vals["Class"]);
        }
        else
        {
            setFieldDisabled(
"Class");
        }

        var assignedToInput = getTagFromIdentifierAndTitle("div", "", "People Picker");

        if (vals["Attendee"] != undefined)
        {
            assignedToInput.innerHTML = vals[
"Attendee"];
            assignedToInput.disabled =
true;
        }
else {
            assignedToInput.innerHTML = getAccountName();

            if (vals["RegisterSelf"] != null)
                if (vals["RegisterSelf"])
                    assignedToInput.disabled =
true;
        }

        setFieldDisabled(
"Class Approval");
        setFieldDisabled(
"Completed Class");
        setFieldDisabled(
"Travel - Itinerary Received");
    }

    function getQueryStringParameters()
    {
        var qs = location.search.substring(1, location.search.length);
        var args = qs.split("&");
        var vals = new Object();
        for (var i=0; i < args.length; i++)
        {
            var nameVal = args[i].split("=");
            var temp = unescape(nameVal[1]).split('+');
            nameVal[1] = temp.join(
' ');
            vals[nameVal[0]] = nameVal[1];
        }

        return vals;
    }

    function getAccountName()
    {
        account = document.getElementById(
"zz7_Menu_t").children[0].innerText;

        return
(account.substr(8));
    }

    function setFieldDisabled(fieldName)
    {
        if (fieldName == null)
            return;

        var theSelect = getTagFromIdentifierAndTitle("select","",fieldName);
        if (theSelect != null)
        {
            setElementDisabled(theSelect);
            return;
        }

        var theInput = getTagFromIdentifierAndTitle("input","",fieldName);

        if (theInput != null)
        {
            setElementDisabled(theInput);
            return;
        }
    }

    function setElementDisabled(element)
    {
        if (element == null)
            return;
        element.disabled =
true;
    }

    function
setLookupFromFieldName(fieldName, value)
    {
        if (value == undefined)
            return;

        var theSelect = getTagFromIdentifierAndTitle("select","Lookup",fieldName);

        // if theSelect is null, it means that the target list has more than
        // 20 items, and the Lookup is being rendered with an input element
        if (theSelect == null)
        {
            var theInput = getTagFromIdentifierAndTitle("input","",fieldName);

            ShowDropdown(theInput.id);
//this function is provided by SharePoint

            var opt=document.getElementById(theInput.opt);

            setSelectedOption(opt, value);

            OptLoseFocus(opt);
//this function is provided by SharePoint

        } else {
            setSelectedOption(theSelect, value);
        }
    }

    function setSelectedOption(select, value)
    {
        var opts = select.options;
        var l = opts.length;

        if
(select == null)
            return
;

        for (var i=0; i < l; i++)
        {
            if (opts[i].value == value)
            {
                select.selectedIndex = i;
                return true;
            }
        }

        return false;
    }

    function getTagFromIdentifierAndTitle(tagName, identifier, title)
    {
        var len = identifier.length;
        var tags = document.getElementsByTagName(tagName);

        for (var i=0; i < tags.length; i++)
        {
            var tempString = tags[i].id;

            if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len))
            {
                return tags[i];
            }
        }
        return null;
    }

</
script>


Created 2012-01-25
comments powered by Disqus
Login