4

Is it possible to change the data source on a lookup column? I have the source set to list A but I want to change it to List B.

  • 1
    Does the list have data? If so, have you thought about what will happen to it if you are able to do this? – Derek Gusoff Sep 20 '12 at 23:25
2

Disclamer: I can not say this is good practice, or very wise, or that it will not break anything.

You can update the SPField Schema XML directly, for example using Client Object Model like this:

var fromList = 'MyList';
var fromField = 'SomeLookupField';
var toListId = '{aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaa}';
var ctx = new SP.ClientContext();
var field = ctx.get_web().get_lists().getByTitle(fromList).get_fields().getByInternalNameOrTitle(fromField);

ctx.load(field);
ctx.executeQueryAsync(function() {
  var schema = field.get_schemaXml();
  schema = schema.replace(/list=\"\S*\"/i, '').replace(/\/\>/, 'List="' + toListId + '" />');

  field.set_schemaXml(schema);
  ctx.executeQueryAsync();
});
3

You cannot achieve that from the UI, you must use SharePoint Powershell Management shell directly from the app server machine. This is the cleaner and most immediate approach:

$sitecol = Get-SPWeb http://yousite/coll
$lookup = $sitecol.Fields["YourLookupField"]
$schema = [xml] $lookup.SchemaXml

# in this example i'm going to reset the lookup list
$schema.Field.Attributes["List"].Value = "{" + $sitecol.Lists["YouNewLookupList"].ID + "}"
# you can also change the display value for the lookup
$schema.Field.Attributes["ShowField"].Value = "Title"

$lookup.SchemaXml = $schema.OuterXml
$lookup.Update($true)
$sitecol.Dispose()

# if you have set to show additional fields for the lookup, 
# you have to to the same for those fields. 
# You can list them as follows:

$sitecol.Fields | ? { $_.Title -match "YourLookupField" }
0

Or You can try this:

$w=get-spweb http://site/
$f=$w.fields["field_name"]
$f.schemaXml = "schema xml with another list ID"
$f.update()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.