I got this request from Khan:
Alexander,
… Another question I have is how I can display two fields (and their labels) in the same row? I know I am asking too much. But I really hope you can give me some advice o this. Thanks man.
Here is an image of a few random fields put side-by-side:
As always we start like this:
Create a document library to hold your scripts (or a folder on the root created in SharePoint Designer). In this example i have made a document library with a relative URL of “/test/English/Javascript” (a sub site named “test” with a sub site named “English” with a document library named “Javascript”):
The jQuery-library is found here. The pictures and the sourcecode refers to jquery-1.3.2.min. If you download another version, be sure to update the script reference in the sourcecode.
Add a CEWP below your NewForm/DispForm/EditForm with this code:
<script type="text/javascript" src="/test/English/Javascript/jquery-1.3.2.min.js"></script> <script type="text/javascript" src="/test/English/Javascript/FieldsSideBySide.js"></script> <script type="text/javascript"> // Append fields to the field "User" arrToPutSideBySide = ['StartTime','EndTime']; customSideBySide(arrToPutSideBySide,'User'); // Appends another set of fields to the field "User" arrToPutSideBySide = ['FirstName','MiddleName','LastName']; customSideBySide(arrToPutSideBySide,'User'); // To append the fields to the top use this code: //arrToPutSideBySide = ['FirstName','MiddleName','LastName']; //customSideBySide(arrToPutSideBySide,'',true); </script>
This code is not the full code from the screenshot above. Each horizontal “section” is made with a separate call to the function “customSideBySide()”.
Parameters explained:
- arr: Array or fields to put in one line.
- insertAfterField: If you want to append the fields below another field, insert the FieldInternalName here. Set this parameter to “” if you want to use the next parameter.
- appendToTop: To insert the fields in the top of the form, set it to true. To insert at the bottom of the form, set it to false.
The sourcecode for the file “FieldsSideBySide.js” looks like this:
/* Sharepoint form: Present fields side-by-side * --------------------------------------------- * Created by Alexander Bautz * alexander.bautz@gmail.com * https://spjsblog.com * v1.0 * LastMod: 26.01.2010 * --------------------------------------------- * Must include reference to: * jQuery - http://jquery.com * --------------------------------------------- */ function customSideBySide(arr,insertAfterField,appendToTop){ if(typeof(fields)=='undefined')fields = init_fields(); // Does the field specified as "insertAfterField" exist? if(insertAfterField!='' && typeof(fields[insertAfterField])=='undefined'){ alert("The FieldInternalName "" + insertAfterField + "", which is specified as "insertAfterField" does not exist."); return false; } var str = ''; $.each(arrToPutSideBySide,function(i,fin){ // Check if FieldInternalName is correct if(typeof(fields[fin])=='undefined'){ alert("The FieldInternalName "" + fin + "" does not exist."); return false; } // Adapt width of single line and multi lines of text if($(fields[fin]).html().indexOf('FieldType="SPFieldText"')>0){ $(fields[fin]).find('input').css({'width':'100%'}); }else if($(fields[fin]).html().indexOf('FieldType="SPFieldNote"')>0){ $(fields[fin]).find('textarea').css({'width':'100%'}); } var tdWidth = Math.round(100/arr.length); str += "<td class='ms-formbody' style='width:"+tdWidth+"%'>" + $(fields[fin]).find('.ms-formlabel').html() + $(fields[fin]).find('.ms-formbody').html() + "</td>"; // Remove original field $(fields[fin]).remove(); }); str = "<tr><td colspan='2'><table cellpadding='0' cellspacing='0' style='width:100%'><tr>" + str + "</tr></table></td></tr>"; if(insertAfterField!=''){ $(fields[insertAfterField]).after(str); }else{ if(appendToTop){ $("table.ms-formtable tbody:first").prepend(str); }else{ $("table.ms-formtable tbody:first").append(str); } } } function init_fields(){ var res = {}; $("td.ms-formbody").each(function(){ if($(this).html().indexOf('FieldInternalName="')<0) return; var start = $(this).html().indexOf('FieldInternalName="')+19; var stopp = $(this).html().indexOf('FieldType="')-7; var nm = $(this).html().substring(start,stopp); res[nm] = this.parentNode; }); return res; }
Save as “FieldsSideBySide.js” – mind the file extension, and upload to the script library as shown above.
Ask if something is unclear.
Regards
Alexander
this is the coolest!
Hey Alexander,
Thanks man. Real nice solution. Keep up your good work.
Fantastic Alexander! This gives SharePoint users a tremendous advantage.
Thanks-
Hi Alexander,
Thank you for this post. Can you provide an example for the script on how to use the insertAfterField and appendToTop? Thanks
Nevermind Alexander, I see how it works now. Thanks
hi i am trying to use the field side by side and I can get it to work for the first section but how do you have it skip a few fileds and go to another set to be side by side.
Do you have and example of the CEWP for the screenshot that would probably answeer my question
Hi,
This is archived by using “insertAfterField” = “User” like this:
Alexander
So if I wanted to have first name ,middle name, and last name on a line and lets say have start time and end time vertical and then have address and telephone vertical which is the default for a form would I just just set a new array and call it the same as the example with endtime being my insertafterfield?
i meant to say have address and telephone side by side
Yes, that’s right.
Alexander
Thanks this what I tried but have been unsuccessful. If you are not busy could you provide a quick example because I am stumped.
I tried to update the and additional array and have been unsuccessful. it just keeps posting the the first array instead of rolling down to the next custom side by side
Hi,
You are missing one argument in the function call. You must provide either “insertAfterField” or “appendToTop”.
Alexander
Hi Alexander,
I followed your instructions and the fields are dropping to the correct place but it keeps replicating the firstname,middlename,and lastname field instead of populating starttime and endtime on the second customsidebyside
I am using the following…
Hi,
In your second function call (line 4) you have the function name wrong.
Alexander
the first array keeps populating instedad of rolling to the next array as stated in my example above.
I have updated the code in the CEWP part of the code with a little more examples.
Alexander
thanks. what i was doing wrong was giving my array’s unique names. this worked great. I appreciate all your help.
I do not have the option to add a CEWP the ‘editform.aspx’ page.
I have tried including the list in a web part page, but when editing items from within this web part page – the edit page does not open within the web part page…it forward to the editform.aspx page.
Any ideas?
Thanks muchly!
Hi,
You find the answer here
Alexander
Hey Alexander,
I have create list as show in the picture , when i add the code in CEWP am getting error FieldInternalError “FirstName” does not exit in window pop . am new to developer role could u please explain how add the code where to place the code. i have jquery-1.4.1 & FieldsSideBySide.js in doc library
this is following code i have add in CEWP
// Append fields to the field “User”
//arrToPutSideBySide = [‘StartTime’,’EndTime’];
//customSideBySide(arrToPutSideBySide,’User’);
// Appends another set of fields to the field “User”
arrToPutSideBySide = [‘MiddleName’,’LastName’];
customSideBySide(arrToPutSideBySide,’User’);
// To append the fields to the top use this code:
//arrToPutSideBySide = [‘FirstName’,’MiddleName’,’LastName’];
//customSideBySide(arrToPutSideBySide,”,true);
Hi,
The script looks at the “FieldInternalName” of the fields, and not the DisplayName.
Look here to learn how to find the FieldInternalName
Alexander
Hi Alexander,
This is my JS Code
// Append fields to the field “User”
//arrToPutSideBySide = [‘StartTime’,’EndTime’];
//customSideBySide(arrToPutSideBySide,’User’);
// Appends another set of fields to the field “User”
arrToPutSideBySide = [‘FirstName’,’MiddleName’,’LastName’];
customSideBySide(arrToPutSideBySide,’User’);
// To append the fields to the top use this code:
//arrToPutSideBySide = [‘FirstName’,’MiddleName’,’LastName’];
//customSideBySide(arrToPutSideBySide,”,true);
You have to check your FieldInternalName. Follow Alexander’s directions just above. It is important to understand that your FieldInternalName most likely does not match your display name.
Once you confirm you have the correct FieldInternalName for all your fields and ‘User’ is one of your fields your JS should work.
I have this working correctly but I am trying to get this to work with the show/hide script. when I do this it breaks. Any suggestions?
This script brakes the page apart and basically deletes the original fields. The hiding will work if the fields are not wrapped, but not if they are.
You would have to write another function to identify the fields after they are wrapped (the init_fields() does not work after the fields are wrapped).
Try to make a function like init_fields() and run it after the fields are wrapped. Use the “object” you get from that function like you do with the object “fields”.
Give it a go, and let me know if you get it working.
Alexander
Hi, I’m having the same problem with the side-by-side script not working with the show/hide script. I don’t understand how to create the second init_fields script. Can you point me in the right direction please?
Thank you
thanks I’ll give it a try.
Hi Alex I did like you said and it worked. I just had to initiate the fields after I made them side by side and it worked like a charm. Thanks for all all you do it is greatly appreciated.
Alexander/Mark :
I am trying to do the exact same thing, tried init_fields() after wrapping but it is not working for me, can you point me in the right direction, please!..Thanks much!!
Please help. Everythang checks out but my code fail at the “typeof(fields[insertAfterField])” area. The code return an “The FieldInternalName does not exist.” error. I have confirmed that the am using valid FieldInternalNames. Any Ideas?
.
Please disregard my last reply. I found the problem.
Hi John
I would like to Know how u have solved your problem, am also getting same problem, I have checked the FieldInternalName is correct, even i have given The FieldInternalName as Title, still am getting same error.
The FieldInternalName does not exist.” error.
How would I put Title and User side by side at the top?
Thanks.
Hi,
Just change the FieldInternalName to match your fields in the function call
Alexander
Also is there a way to insert a blank row and put text on it, so as to break the fields up into groups?
Take a look at these two:
Headings in SharePoint Forms – jQuery
Headings for SharePoint forms – another method
Alexander
Thanks!!!
Another question: how do I put a field beside the field I’m using to append to/below? Using your example how would you put a field beside User?
Hi, my solution to this question was to create a “dummy” field which will be used as the first field. The code to hide this field is simple and it solves the problem to your question.
Hi Alexander
I have checked the FieldInternalName is correct, even i have given The FieldInternalName as Title, still am getting same error.
The FieldInternalName does not exist.” error.
Could please help me in this issuse
Hi,
Is the form customized? – if so How to use these scripts in a customized form
Alexander
Hi All,
I have resolved The FieldInternalName does not exist.” error.
Just add ContentEditorWebPart below all web part (At bottom of the main webpart zone.)
I am trying to add this script and the cascading script, but they seem to conflict. Is there a way to use both scripts on the same fields?
Is there a way to suppress the error prompts? I have content types on a list and certain content types have arranged fields. Others do not, hence the error.
Hi Alexander. Is there any way to combine this solution with your Tabs in Sharepoint Forms solution? I tried to put both scripts on my DispForm and it “worked” but with undesireable results.
For example: the side by side fields picked up strange styles, and they appear on every tab instead of just after the single appended field that I specified in the CEWP.
Does anyone know how to get the side by side code to work on a multiline enhanced text field? Thanks
Hi,
This is not possible due to the complexity of the rich text fields.
Alexander
I cannot get this fieldssidebyside.js to work with Firefox. It works in IE – Can you provide assistance?
The error says, “The Field Internal Name does not exist”, but again, it does work in IE
It looks like in Firefox, the last letter of the field internal name is being cut off.
Thanks!
Hi,
You need to change the function init_fields() with the one found here: https://spjsblog.com/2010/04/07/revised-function-init_fields/
Alexander
This is working great with the exception of date fields. The calendar pop-up is not working. I am not getting any errors, the pop-up just does not display Did anyone else run into this issue? If so, how did you resolve?
Hi,
What browser do you use?
Alexander
IE 8
Hi,
I cannot recreate the problem – do you have other scripts in the same page?
Alexander
This will not work on a custom form as there is no FiledInternalName…
The function init_fields needs to be flexible to accomodate this i.e.
function init_fields(type)
There is no way you could have a function that identifies fields in all customized forms. The fact that the form is customized by individual users makes it impossible to span them all in one function. If you read the topmost note on the front page of this site, I have given an example on how you can make your own custom “init_fields” function.
Alexander
Ahh, thanks. I didn’t see that.
I am receiving the following message: “The FieldInternalName “User”, which is specified as “insertAfterField” does not exist.
Any ideas?
Thanks,
Brian
Are you sure the correct FieldInternalName is “User”?
Look here for tips on getting the FieldInternalName
Alexander
Thanks Alexander! I will review it. Another Question? I have a list that has over 200 columns and when viewing and editing the contents of the list, I have to scroll through the entire list to review fields that have information in them traversing fields that are blank. The fields are unique for each record and may be populated depending on the record so hiding them is not an option. Is there a way without using SharePoint Designer to dynamically sort fields on a list on New, Edit, and Disp form that have content in them for each unique record?
Your feedback is greatly appreciated.
Regards,
Brian
Hi,
I’m not sure I understand. What do you mean by “each unique record”? How would the script know which fields to show or hide?
Please answer by adding a new comment (with the full explanation) in the requests-page
Alexander
Hi Alexander,
First, I got the side by side functionality to work. It was the “Field Internal Name”. Thanks! I have a list that has a large number of records and some of those records do not have values in all of the columns. Some of the fields are blank which is why I called them unique. I am importing a spreadsheet into SharePoint as a list. The data is an output from another system. The issue is that each record is too long to scroll through. When I display a record or edit I have to scroll down through all of these fields in the record that some of them are blank. I am only interested in seeing the ones with content in them. I can’t hide them because while one record may have a column that is empty it maybe populated in another record. I am looking to better display the list record when viewing or editing by not having to scroll an entire record for information. I want the ability to show the record with fields that have content first then the blank fields to appear after them on a sharepoint list.
Thanks,
Brian
Hi Alexander,
your code is working fine for me. But I need to customize the page like both label and textbox field should together (in one line), not one below the other.
Can you please suggest me?
Thanks,
Gomathy
Hello Alexander,
this code works great for me but I tried to combine this with your Script “Tabs for SharePoint Forms v.2.0”.
Both scripts in one form are not working. Do you know how I can use both scripts?
Regards,
uddu
Hi Alexander
This works great, however the issue I have on a from that is quite wide is the spacing between the fields.
I have three fields with about 3 inches between them.
Is it possible using jquery to set the width between fields on a form ?
Iain
Hi Alexander,
Thank you so much for this post. I have a question… How can adjust the field name to the right side (only for the first field) – I want the form to look like below:
Middle Name Last Name
First Name [text box] [text box] [text box]
Thanks again,
Srikanth
I used scripts as said above but still did not solve anything. I’ve tried to follow other options and they gave nothing, I was wondering if it was possible to give me some help with this issue.
In init_fields() {… says “un canght reference error: $ is not defined.”
This means that your reference to jquery is missing, or is wrong. Alexander
How to float the label names to left/right/top/bottom of the input field?
I’ve got a custom list with 3 fields:
1 Promo Code (FieldInternalName=”Title”)
2. Pxt (FieldInternalName=”PXT”)
3. test2 (FieldInternalName=”test2″)
I want to put Pxt and test2 side by side on my newform.
I’ve added the FieldsSideBySide.js to my site, and below is my code which I’ve added directly after ” on newform.aspx, however it’s not working. Any ideas?
Cheers,
Matt
arrToPutSideBySide = [‘PXT’,’test2′];
customSideBySide(arrToPutSideBySide, ‘Title’);
Hi,
I cannot get this one to work. I have your great WrapChoiceFields working beautifully using the same newform. The function tip you suggest using has a function not defined for customSideBySide but I cannot figure it out.
I am using
arrToPutSideBySide = [‘Side1’, ‘Side2’];
customSideBySide(arrToPutSideBySide, ‘User’);
I confirmed Side1, Side2 and User are the internalfieldnames.
I even changed the jquery version from 1.9.1 back to 1.3.2 in case it was a jquery issue.
Thank you!
Hello Alexander-
I am stumped on the CEWP with SP 2010. I add the CEWP below the new form as stated in the instructions. I add the files to the document library. I change the jquery to reference 1.10. When I save the webpart nothing changes. Am I missing something?
You should use this one: https://spjsblog.com/2013/08/12/dynamic-forms-for-sharepoint-v3/
Alexander
Same solution Iam trying at SP 2013. It works as expected but side by side field values are not saving into list. Can you pls. look into this.
Hi,
Sorry, but this code is no longer maintained, but you find side-by-side functionality in DFFS: https://spjsblog.com/dffs/
Alexander