SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
var objWeb = properties.Feature.Parent as SPWeb;
Guid webPublishingGuid = new Guid("94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb");
SPFeature feature = null;
SPSite site = objWeb.Site;
feature = objWeb.Features[webPublishingGuid];
if (feature == null) 
    SPContentType contentType = objWeb.ContentTypes["Wiki Page"];
    SPField field = objWeb.Fields["Keywords"];
    SPFieldLink fieldLink = new SPFieldLink(field);

I want to add site column keyword to site content type wiki page. Above is my code I wrote in my FeatureActivated method.

This works when I execute this code in the default site. My problem is it throws an exception that your keyword column is null when I execute that code in sub site or other team sites.

share|improve this question
up vote 1 down vote accepted

Your site column may not exist in the sub-site.

You probably are creating the Keywords site column in the root web of the site collection (Or is keywords a default one? If so, it'll be in the root web too).

You can check this from the Site Column Gallery in the Site Settings. Check to see what site it's created in.

If so, you'll need to use:

SPField field = site.RootWeb.Fields["Keywords"];

That's my guess.

share|improve this answer
Thanks Andy your solution working like charm thanks – vishal Jun 29 '11 at 13:40
@vishal Please use the tick next to the post that helped you the most so that the question is marked as answered. Thanks! – Alex Angas Jul 15 '11 at 10:51

In your feature, since it can be activated at multiple scopes, you should check if the field exists, if it doesn't create it, then add it to your content type. This removes the manual process of creating the site column.

SPWeb web = properties.Feature.Parent as SPWeb;
SPField field = web.Fields.TryGetFieldByStaticName("Keywords");
if (field != null)
    SPContentType contentType = web.ContentTypes["Wiki Page"];
    SPFieldLink fieldLink = new SPFieldLink(field);

The method web.Fields.TryGetFieldByStaticName(); will not throw an exception. It returns null if no fields exist.

As a side question, what are you trying to do? Are you checking for a dependent feature?

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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