Create a new Lookup field using JSOM in SharePoint 2013 / Office 365

Introduction

When we need to provision a new lookup column referencing a Library we can thing about using XML procedure that means using a code similar to this one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Field
ID="{6fe3193c-ad20-4f0f-ba9e-cdabb7d2c2e7}"
Name="StandardPageLookup"
StaticName="StandardPageLookup"
DisplayName="Related Page"
SourceID="http://schemas.microsoft.com/sharepoint/v3"
Type="Lookup"
ShowField="Title"
Mult="TRUE"
UnlimitedLengthInDocumentLibrary="TRUE"
List="$Resources:osrvcore,List_Pages_UrlName;"
Overwrite="TRUE"
Group="Site Columns">
</Field>
</Elements>

But, bad news here, this code is expecting something like List/ListName and it doesn’t works for a Libraries. Unless you put there directly the Library ID (http://www.sharepointnutsandbolts.com/2007/04/creating-list-based-site-columns-as.html).

I am pretty sure, we don’t know the exact GUID of the referred library, in my case it is Pages Library, so, this way doesn’t works for my scenario. Even if I change $Resources:osrvcore,List_Pages_UrlName; by Pages or by /Pages. It doesn’t works.

JSOM to rescue

Luckily, we have JSOM library that sometimes is helping us in the provisioning process. You can use this code to create a new lookup to Pages Library in the Documents Library.

image

Code

var ctx = new SP.ClientContext(_spPageContextInfo.webServerRelativeUrl);
var allprop = ctx.get_web().get_allProperties();
ctx.load(allprop);
ctx.executeQueryAsync(
function ()
{
// Extract the Pages Library GUID from __PagesListId property bag
var pagesListID = allprop.get_item("__PagesListId");
console.log("Pages Library GUID: " + pagesListID);
var listNameOrGuid = "Documents";
var list = ctx.get_web().get_lists().getByTitle(listNameOrGuid);
var fieldCollection = list.get_fields();
var fieldSchema = '<Field List="' + pagesListID + '" Type="Lookup" DisplayName="Related Page" Name="StandardPageLookup" StaticName="StandardPageLookup" ShowField="Title" Mult="FALSE" ID="{6fe3193c-ad20-4f0f-ba9e-cdabb7d2c2e7}" />';
fieldCollection.addFieldAsXml(fieldSchema, true, SP.AddFieldOptions.addToDefaultContentType);
ctx.load(fieldCollection);
ctx.executeQueryAsync(function (){
var message = "NewField added to " + listNameOrGuid + " list.\n\nThe following fields are available:\n\n";
var fields = '';
var listEnumerator = fieldCollection.getEnumerator();
while (listEnumerator.moveNext()) {
fields += listEnumerator.get_current().get_title() + "; ";
}
console.log(message + fields);
},
function(sender, args){
console.log('Request fieldCollection failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
);
},
function(sender,args)
{
console.log('Request allprop failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
);

 

Author: José Quinto
Link: https://blog.josequinto.com/2015/12/02/create-a-new-lookup-field-using-jsom-in-sharepoint-2013-office-365/
Copyright Notice: All articles in this blog are licensed under CC BY-SA 4.0 unless stating additionally.