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
How to change content type of existing Items (documents) in SharePoint? You can change the content type of the existing items by getting into the edit properties page of the particular item and switching the “Content Type” field.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With