0

I have a list with a dropdown value containing options Comp A, Comp B, Comp C, Comp D. I have a created the folders with the same values as that of dropdown.

When I access a folder and create an item, can the folder name information auto-populate in the dropdown field? For ex: If I go into folder Comp A and add an item, the dropdown value should auto-populate as Comp A

How can I do that? Please suggest

0

When an item is created inside a folder, the NewForm.aspx receives a query string which contains RootFolder. This has the name of the Folder. Retrieve this folder name using:

GetUrlKeyValue("RootFolder");

The result may look something similar to this "/sites/TestSite/Lists/Cust/F1". So Extract the folder name from it. Once you have the folder name, you can use JQuery to prepoulate the dropdown:

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

See the link below for more information. It shows how to prepoulate a lookup field. But the code can be used for drop down as well.

http://www.sharepointnadeem.com/2014/10/sharepoint-pre-populate-lookup-field-in.html

0

When you navigate to that folder the url has contains the folder name(as query string) of the current folder, you can get the value of folder name from the query string(Root Folder), but this will contain the actual path of folder, i.e the name of document library and then the folder name.

for eg. RootFolder=%2FInfoPath%2FTesting

Here Infopath is is the Documnet Library name and Testing is the folder name. when you get the query string value you need to split the string by % and also you need to avoid '2F' from the last string then, which will get you the value of folder.

Below is the code to get the values from Query String.

function getQueryStringParameter(paramToRetrieve) {
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] == paramToRetrieve)
        return singleParam[1];
}

After this do a split using

.split('%');

Then to avoid 2F use .substring(starting pos,len);

Then on document.ready() method you can call the method which performs the above task and one more line, where you take the id or name attribute of the column in the form and set the value of it like.

$( "input[name$='letter']" ).val(stringWhichContainsTheFolderName);

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.