0

probably an easy one, but I can not get it to work.

I have a list with two columns:

  1. Title (Single line of text)
  2. Team (Choice - Dropdown)

enter image description here

I want to preset the "Team" column via a URL-String. I don't want to do it afterwards with an Workflow.

I tried: http://sp13/lists/listname/NewForm.aspx?Team=Finance

In the end, I need it auto populated via an button from different team sites, preset with their team.

Any ideas would be great.

1

Put this JavaScript code in your newForm.aspx page in SEWP:

<script type="text/javascript">
        function myFunction() {
    document.getElementById("your dropdown control's id").value = getParameterByName('Team');
}
        window.onload = myFunction;

        function getParameterByName(name) {
    var url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}
        </script>
|improve this answer|||||
  • I guess even more easy, how to get the dropdown control's id? – Patrick Jun 20 '16 at 9:59
  • You can get it using firebug in FF. – Dikesh Gandhi Jun 20 '16 at 10:30
  • @Dikesh, just one suggestion: for this scenario the SEWP (Script Editor Webpart) is better. Actually the SEWP is generally better for HTML, CSS and JS. CEWP is better for adding rich content to the page. – Mihail-Florin Popa Jun 20 '16 at 12:45
  • Anybody faced issue with this in IE11? It's working fine in Chrome. It's extracting the value correctly from URL, but .value is just not setting in IE. No errors either. – Shanka Sep 5 '17 at 9:17
1

Something like this. Get the value of query string using GetUrlKeyValue and then use javascript/jquery to set the selected dropdown value.

$(document).ready(function () {    
    var dropDownValue = GetUrlKeyValue('Team');
    if (dropDownValue) {
        $("select[title='Team']").children("option").each(function () {
            if ($(this).text() == dropDownValue) {
                $(this).attr('selected', 'selected');
            }
        });
    }
});

See this for more info: http://www.sharepointnadeem.com/2014/10/sharepoint-pre-populate-lookup-field-in.html

|improve this answer|||||
0

Here is one way your setTeam function can look like:

function setTeam(ctx) {

    // Set the default value of the drop-down field from query string.
    ctx.CurrentFieldValue = GetUrlKeyValue('Team');

    // Return the default rendering function for choice fields.
    return SPFieldChoice_Edit(ctx);
}

Notes

  • There are no IF statements required because the GetUrlKeyValue function always returns an empty string if something is wrong, thus setting the drop-down field to nothing which is the default value anyway.
  • If you want to make sure this code works for cases where the Team paramater in the query string is lowercase (team) or uppercase (TEAM) or random (tEaM), then use replace GetUrlKeyValue('Team') with GetUrlKeyValue('Team', true, null, true);.
|improve this answer|||||

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.