Archive

Archive for March 19, 2009

Adding & Modifying Field in your Content Types

March 19, 2009 Leave a comment

How do you add or modify fields in content types which you deployed as a solution.

After I have deployed a solution which contains my Content Types there is a strong possibility that, in time, your users will request field changes and even additional fields. So what do you do? You could make your changes to your feature, retract and redeploy this solution but I find this troublesome.

 

My approach is to update the feature to keep things up-to-date but then just make the required changes to the live site using API through a console app.

Here is an example for modifying an existing field and also adding a new field

 

Modifying an existing field

Modifying a site column fields is straight forward enough but there is a property to watch out for – SPField.PushChangesToList   which will ensure your changes filter down to the lists which use this field.

 

public void modifyField(string url)
     {
         using (SPSite site = new SPSite(url))   
          {   
              using (SPWeb rootWeb = site.RootWeb)   
              {   
                  try
                  {
                      SPField fld = rootWeb.Fields[new Guid("3D864E5E-721E-4ae0-929B-CF7473E11049")];
                      if (fld != null)
                      {
                          fld.Title = "New display name";
                          fld.PushChangesToLists = true;
                          fld.Update();
                      }
                      else
                      {
                          Console.WriteLine("Field could not be found");
                      }
                  }
                  catch (Exception ex)
                  {
                      Console.WriteLine(ex.Message);
                  }
              }
          }
 
     }

 

Adding a new site column field

In this example I add a new site column field to the root site, then locate the appropriate list in which this is being used and add it there too. I then get the default view for the list and add the field to that too..

using (SPSite site = new SPSite(url))   
     {   
         using (SPWeb rootWeb = site.RootWeb)   
         {   
             try
             {
 
                 
                 string fldXml = "<Field ID=\"{639968F7-6D5C-42f9-8B51-B9EA9BBD212E}\" Type=\"Text\" Name=\"NewFieldName\" DisplayName=\"Sample Field Name\" Sealed=\"TRUE\" Group=\"ChrisForbesBlogs\" />";
 
 
                 //Create new site column field
                 rootWeb.Fields.AddFieldAsXml(fldXml);
                 SPField fld = rootWeb.Fields[new Guid("639968F7-6D5C-42f9-8B51-B9EA9BBD212E")];
 
                 //Add the filed to the required list
                 SPList lst = rootWeb.Lists["NameOfList"];
                 lst.Fields.Add(fld);
                 
                //Add the new column to the view - in this example I only have a default view so can retrieve it by getting the first index - Zero
                SPView view = lst.Views[0];
                 view.
 
             }   
             catch (Exception ex)   
             {   
                 Console.WriteLine(ex.Message);   
             }   
        }
    }

I’d love to hear your approaches to change control. Leave a comment or drop me an email.

 

Categories: Uncategorized
Follow

Get every new post delivered to your Inbox.