Reputation: 1569
var objWeb = properties.Feature.Parent as SPWeb;
SPContentType contentType = objWeb.ContentTypes["Wiki Page"];
if (!contentType.Fields.ContainsField("Keywords"))
{
SPField field = objWeb.Fields["Keywords"];
SPFieldLink fieldLink = new SPFieldLink(field);
contentType.FieldLinks.Add(fieldLink);
contentType.Update(true);
}
I use This code in feature activation to add site column "KeyWord" to site content type "Wiki Page" my problem is "keyword" add in "wiki page" but not from the existing site column it's add new site column. is there problem in my code?
one other thing this code works fine on my MOSS server when i deploy on office365 this problem i found
Upvotes: 7
Views: 21906
Reputation: 3558
You should try the code below:
if (objWeb.IsRootWeb)
{
SPContentType contentType = objWeb.ContentTypes["Wiki Page"];
if (!contentType.Fields.ContainsField("Keywords"))
{
SPField field = objWeb.Fields["Keywords"];
SPFieldLink fieldLink = new SPFieldLink(field);
contentType.FieldLinks.Add(fieldLink);
contentType.Update(true);
}
}
else
{
SPContentType contentTyperoot = site.RootWeb.ContentTypes["Wiki Page"];
if (!contentTyperoot.Fields.ContainsField("Keywords"))
{
SPContentType contentType = site.RootWeb.ContentTypes["Wiki Page"];
if (!contentType.Fields.ContainsField("Keywords"))
{
SPField field = site.RootWeb.Fields["Keywords"];
SPFieldLink fieldLink = new SPFieldLink(field);
contentType.FieldLinks.Add(fieldLink);
contentType.Update(true);
}
}
}
I hope someone is being helped from my code :)
Upvotes: 13