TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Create Lookup Site Column using JavaScript
Gowtham Rajamanickam
Apr 14, 2015
12.6
k
0
1
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
In this blog i would like to share how to create a Lookup column in sharepoint using jsom .
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.
Next Recommended Reading
Creating the Site Columns/Fields Using CSOM in PowerShell in Office 365 / SharePoint Online