Sharepoint: Add an existing site column to an existing content type.

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 when activating the function to add the KeyWord site column to the site content type Wiki Page. My problem is to add the β€œkeyword” to the β€œwiki page”, but not from an existing site column, it adds a new site column. is there a problem in my code?

one more thing, this code works fine on my MOSS server when I deploy this problem that I found on office365

+6
source share
1 answer

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 my code helps someone :)

+12
source

Source: https://habr.com/ru/post/893233/


All Articles