1

I need to create a site column programmatically during feature activation (requirements - supose I can't change that).

My code is fairly straight forward:

string internalName = web.Fields.AddLookup("foo", someList.ID, false);
// *field name*, *ID of the list to point* and *required*, respectively.

SPFieldLookup foo = (SPFieldLookup)web.Fields.GetField("foo");
foo.Group = "bar";
foo.update();
web.update();

The field is created, alright - but when I navigate to the site where it was created, it does not show up in the "site columns" page, in the site administration.

I can't add it again because it's already in the site. I can't associate it to any content type because it doesn't show up in any page at all. I can't delete it programatically, either:

  • With C# it tells me there is some list or content type using the field (which is not true at all)
  • and with Powershell I either get no errors, or get a "wrong parameters" error when running Delete in any form below. But even after the commands run without errors the column is not truly deleted (I can still fetch it via C# and Powershell):

 

$site = Get-SPSite(siteUrl)
$web = $site.rootweb
$web.Fields["foo"].delete()
$foo = $web.Fields["foo"]
$foo.delete()
$web.Fields.Delete($foo)
etc...

(The only web in the site colletion is the root one.)

I have tried the brute force solution, that is, deleting the site collection and recreating it again (I'm just fiddling with a dev environment for now), thinking my site might have gone corrupt. It just doesn't solve the problem. When I create it again in a whole new site, the column stays there, and I can neither use it nor delete it.

Help?

1

Your code looks like it will only create a Field Link, linking an existing Field to for example a list.

You should use this method instead: http://msdn.microsoft.com/en-us/library/ms472869.aspx

Like web.Fields.Add("foo", SPFieldType.Text, false) before creating a field link.

This will create a new SiteColumn of type Text. Here you can find the available field types.

  • Problem is, I need the field to be a lookup. If you try to add a lookup with SPFieldCollection.Add, it throws an excetion telling you to use SPFieldCollection.AddLookup instead (and the documentation says it does create a field). I have confirmed that the field is created and can be accessed with a SField reference, so I believe it is not a field link. – Renan Jul 4 '13 at 13:52
  • While the solution wasn't exactly what you suggested, this discussion did make me read all the documentation of the Add and AddLookup methods again, and that's where I found the solution for my problem. So, +1 for you. – Renan Jul 4 '13 at 14:08
1

I found out what the problem was. I totally forgot to set the LookupField property of my new field.

FML.

While it was somewhat stupid of me to expect a lookup to work without pointing to a field in the target list... I think one would expect an inconsistent field like that to not work at all. Sorry for the rant, but if it was me designing Sharepoint, I would make sure you can't save a lookup until all the things it needs to work are set. I would sure force the target field to be informed in the AddLookup method, for example.

So there. I set that property and my site column is no longer haunting my site collection.

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.