Pregunta

I am running SP 2016 on premise. I can successfully run the script below to add a site column to a list on the "root" site.

clientContext = new SP.ClientContext();
var olistCollection = clientContext.get_web().get_lists();
var oList = olistCollection.getByTitle("listName");
var oSiteColumn = clientContext.get_web().get_fields ().getByInternalNameOrTitle('siteColName');
oList.get_fields().add(oSiteColumn);
oList.update();
clientContext.executeQueryAsync(onsuccess, onfailed);

function onsuccess() {
    console.log('Success');   
}
function onfailed(sender, args) {
    console.log('Failed' + args.get_message() + '\n' + args.get_stackTrace());
}

However, I run the very same script on a subsite and get this error message:

FailedColumn 'siteColName' does not exist. It may have been deleted by another user.

Any idea?

update after answer from Divya

I try to use dedicated URLs for client contexts for root and sub site like so:

var rootContext = new SP.ClientContext("https://company.org/sites/root");
var oSiteColumn = rootContext.get_web().get_fields().getByInternalNameOrTitle('siteColName');
rootContext.executeQueryAsync(onsuccess1, onfailed);
function onsuccess1() {
    var clientContext = new SP.ClientContext("https://company.org/sites/root/sub");
    var olistCollection = clientContext.get_web().get_lists();
    var oList = olistCollection.getByTitle("listName");
    oList.get_fields().add(oSiteColumn); // <-- triggers the error
    oList.update();
    clientContext.executeQueryAsync(onsuccess2, onfailed);

}
function onsuccess2() {
    console.log('Success');   
    }
function onfailed(sender, args) {
    console.log('Failed' + args.get_message() + '\n' + args.get_stackTrace());
}

which yields the following error message when trying to add the site column:

Uncaught Sys.InvalidOperationException: Sys.InvalidOperationException: The object is used in the context different from the one associated with the object.
¿Fue útil?

Solución

For adding the Site Column in sub-site, kindly get the Site Column using the Root site's web object as done in the below PowerShell script. You can convert this PowerShell into JSOM according to your code.

$web = Get-SPWeb http://siteURL/

$siteCol = $web.Fields.GetFieldByInternalName("ColumnName")

$subWeb = Get-SPWeb "http://siteurl/subsiteurl"

$spList = $subWeb.Lists["ListName"]

$spList.Fields.Add($siteCol)

$spList.Update()

Update in the answer after update in question

Try the below code in which the same context has been used for fetching the Site Column:

clientContext = new SP.ClientContext();
var olistCollection = clientContext.get_web().get_lists();
var oList = olistCollection.getByTitle("listName");
var oSiteColumn = clientContext.get_site().get_rootWeb().get_fields().getByInternalNameOrTitle('siteColName');
oList.get_fields().add(oSiteColumn);
oList.update();
clientContext.executeQueryAsync(onsuccess, onfailed);

function onsuccess() {
    console.log('Success');   
}
function onfailed(sender, args) {
    console.log('Failed' + args.get_message() + '\n' + args.get_stackTrace());
}
Licenciado bajo: CC-BY-SA con atribución
scroll top