Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sharepoint : Add existing site column to existing content type Programatically

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

like image 499
V_B Avatar asked Jul 21 '11 06:07

V_B


People also ask

How do I change the content type in a column in SharePoint?

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.


1 Answers

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 :)

like image 89
Jignesh Rajput Avatar answered Sep 30 '22 20:09

Jignesh Rajput