0
<SharePoint:FormField 
                runat="server" 
                ID="field_Domain" 
                ControlMode="New" 
                FieldName="Domain"/>

I want to fill the formfield above with data comming from an array of string. The context: a list custom form (New), sharepoint 2010.

The control is part of the .ascx of a webpart used to create a list custom new form. The data processing is done in the code behind (.cs).

  • Aren't you missing any words in the question?? Not able to get the context of your question – Gaurravs Nov 16 '15 at 10:22
0

I'm not sure what you mean by "business" (I'm also still new at sharepoint). But you can always update or add items to dropdowns by Javascript.

Something like this:

ReplaceDropDown = function() {
    var apiCallUrl = '/_api/web/lists/getbytitle(\'Your List\')/items?$top=1000';   
    $.ajax({
        url: apiCallUrl,
        method: "GET",
        headers: {
            "Accept": "application/json; odata=verbose",
            "content-type": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },

        success: function(jsonData){
            if(jsonData.d.results[0] != null){  
                $("[title='Member Association']").empty();                      
                for (var i = 0; i < jsonData.d.results.length; i++) {
                    var selectedHTM = (currentId == jsonData.d.results[i].ID) ? 'selected="selected"': '';
                    $("[title='Your List']").append('<option ' + selectedHTM + 'value="' + jsonData.d.results[i].ID + '">' + jsonData.d.results[i].Title + '</option>');
                }

            }   
        },
        error: function(data){
            console.log("An error occured..." + data.responseText);
        }   
    });     
}

This is an example where you load the content of the dropdown out of another SP List with a REST Call.

  • Thank you for your answer but I want to get data from a string array. The array is instantiate inside the code behind. I wasn't clear enough. My bad. – ameliapond Nov 16 '15 at 10:48
  • So just use your Array. Actually it's almost the same: for (var i = 0; i < YOUR_ARRAY.length; i++) { var selectedHTM = (currentd == i) ? 'selected="selected"': ''; $("[title='Your List']").append('<option ' + selectedHTM + 'value="' + i + '">' + YOUR_ARRAY[i] + '</option>'); } – Dave Nov 16 '15 at 10:51
  • I just want to fill my formfield with an array of string declared in the code behind. I don't want to get a sharepoint list sorry. – ameliapond Nov 16 '15 at 10:58
  • 1
    This is what the above method does: It fills a given form select field with an array. More simplyfied: for (var i = 0; i < YOUR_ARRAY.length; i++) { $("#field_fieldName").append('<option ' + selectedHTM + 'value="' + YOUR_ARRAY[i] + '">' + YOUR_ARRAY[i] + '</option>'); } – Dave Nov 16 '15 at 11:02
  • As I said the array is coming from the code behind so there is no way for me to access it. – ameliapond Nov 16 '15 at 11:48
0

As per my knowledge i can suggest you these 2 methods. But if you have better one then please let me know also.

1) You should try saving that array with some special characters like '*' or something in the HiddenField and later on split that value using that special character and bind your dropdown. In this case if you are not getting value from hidden field as your HiddenField will be server control then try delaying you javascript method. To read server control's value you have use its ClientID.

2) OR else you have create a REST/Webservice which will return that Array of string then call that Service and on success bind your dropdown.

0

You can use the Field property of the form field (field_Domain) which returns an SPField object and cast it to your actual field type on the form and then set the values from your string array.

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.