- Create a Separate page in SharePoint site and a content editor Webpart.
- Upload the JavaScript code to site Assests and copy the link.
- Edit the CEWP and add the path of the script txt file and save it.
- Enter the list name and click the create list button and refresh the page.
- The list created in SharePoint site.
- $( document ).ready(function() {
- CreateSiteLookupColumn();
- });
- var clientContext;
- var oWebSite;
- var oList;
- function CreateSiteLookupColumn()
- {
- clientContext= new SP.ClientContext.get_current();
- oWebSite= clientContext.get_web();
- //Lookup List
- oList=oWebSite.get_lists().getByTitle('TestList');
- clientContext.load(oList);
- clientContext.load(oWebSite);
- clientContext.executeQueryAsync(function () {
- var fieldXml="<Field Name='Country' DisplayName='Country' Type='Lookup' Required='FALSE' Group='Operations'/>";
- var lookupField=oWebSite.get_fields().addFieldAsXml(fieldXml,true,SP.AddFieldOptions.addFieldCheckDisplayName);
- //Cast to Lookup field to set List Name and lookup column
- var fieldLookup = clientContext.castTo(lookupField, SP.FieldLookup);
- fieldLookup.set_lookupList(oList.get_id());
- fieldLookup.set_lookupField("Title");
- fieldLookup.update();
- clientContext.executeQueryAsync(function () {
- alert('Look up Field Added successfully');
- }, function (sender, args) {
- alert(args.get_message() + '\n' + args.get_stackTrace());
- });
- },function (sender, args) {
- alert(args.get_message() + '\n' + args.get_stackTrace());
- });
- }

Adarsh GumashtaPosted Aug 9, 2017, 11:11 PM
For Lookup fields you can change the schema to include "mult" attribrute . var fieldXml="<Field Name='Country' DisplayName='Country' Type='Lookup' Required='FALSE' Mult='TRUE' Group='Operations'/>"; or include this line fieldLookup.set_allowMultipleValues(true); anyone of 2 will work :)
Gurdeep KaurPosted Jul 21, 2016, 5:55 AM
What if more than one site columns needs to be created?
Gurdeep KaurPosted Jul 21, 2016, 5:54 AM
What if muliptle site columns needs to be created. How to modify this script?
Vijay SPosted Jun 17, 2015, 9:15 AM
Good one