locked
Sharepoint 2007 cannot add site column to content type RRS feed

  • Question

  • HI guys,

    I've doing this several time but actually I have an environnement where I executing the bellow code to add a site column to a content type but Im stuck on the error cannot modified this collection. On the content type update method. Surely some of you have already faced this problem I need help please.

    My Site column has been created trough a feature. 

    using

    (SPSite site = new SPSite("*******************"))

     

    {

     

    SPFieldChoice siteColumnPending = (SPFieldChoice)web.AvailableFields[new Guid("{2F3AA44F-7FB3-4c09-9999-90DD93226883}")];

     

    //Content type ID differs from one environnement to another so its ID is provided by feature properties
     

     

    SPContentType gsrNotificationContentType = web.AvailableContentTypes[new SPContentTypeId("0x01010007FF3E057FA8AB4AA42FCB67B453FFC100E214EEE741181F4E9F7ACC43278EE81100727FB216CF3BB043ACAF4D6D64FADA60")];

     

    if (!gsrNotificationContentType.Fields.ContainsField(siteColumnPending.Title))

    {

    gsrNotificationContentType.FieldLinks.Add(

    new SPFieldLink(siteColumnPending));

    gsrNotificationContentType.Update(

    true);

    }

     

    Console.ReadKey();

    }

    }

    {

     

    using (SPWeb web = site.OpenWeb())
    Wednesday, February 1, 2012 5:05 PM

Answers

  •  

    HI guys I get the problem. I dont know if the fact that I imported the new site column from feature didnt help (which wouldnt be logic) but I decided to create my site column by code and retrieve the content type by doing we.ContentTypes instead of Available content type and everything is working now. Here is the exact corrected code:

    string gsrNotificationContentTypeID = properties.Feature.Properties["GSRNotificationContentTypeID"].Value + string.Empty;

     

    SPContentType gsrNotificationContentType = currentWeb.ContentTypes[new SPContentTypeId(gsrNotificationContentTypeID)];

     

    SPFieldCollection currentWebFieldCollection = currentWeb.Fields;

     

    string siteColumnPendingAsXml = "<Field Name=\"GSRNotificationPending\" StaticName=\"GSRNotificationPending\" DisplayName=\"Pending\" Type=\"Choice\" Group=\"SL_GSR\" ID=\"{4B1D904E-8B48-4c39-B24E-60878722D746}\" SourceID=\"http://schemas.microsoft.com/sharepoint/v3\" ><Default>Yes</Default><CHOICES><CHOICE>Yes</CHOICE><CHOICE>No</CHOICE></CHOICES></Field>";

     

    string strNewColumnPendingInternalName = currentWebFieldCollection.AddFieldAsXml(siteColumnPendingAsXml);

     

    SPFieldLink columnPendingFieldLink = new SPFieldLink(currentWeb.AvailableFields.GetFieldByInternalName(strNewColumnPendingInternalName));

    gsrNotificationContentType.FieldLinks.Add(columnPendingFieldLink);

    gsrNotificationContentType.Update(

    true);

     

    Hope this helps other brothers 


    Wednesday, February 1, 2012 7:00 PM