C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Create Lookup Site Column using JavaScript
WhatsApp
Gowtham Rajamanickam
10y
12.8
k
0
1
25
Blog
Steps
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());
});
}
Save the page and check the list settings field added successfully.
Recommended related topics
Membership not found