Home > MOSS 2007, SharePoint, SharePoint 2010 > Programmatically getting the SPField object you just added

Programmatically getting the SPField object you just added

When you add an SPField to an SPList programmatically using the SharePoint Object Model you can get the SPField object representing the newly added field via SPList.Fields[string displayName]. But this assumes that no other field with the same display name as the newly added SPField exists in the SPList.

The work around is listed below:


Guid fieldGuid = Guid.NewGuid();
string newFieldCAML =
 "<Field Type="Text" DisplayName="MyNewField" ID="+fieldGuid+"/>";
splist.Fields.AddFieldAsXml(newFieldCAML, false, SPAddFieldOptions.AddFieldInternalNameHint);
SPField newField = spList.Fields[fieldGuid];

You can use the AddFieldAsXml to add the new SPField. It takes a CAML string as a parameter in which you can specify your own Guid. Later you can use this Guid to retrieve the SPField you just added.

  1. Lubomir Mishkovsky
    February 17, 2010 at 6:27 pm

    What about using another approach for adding the new field to the list:

    SPField someField = myList.Fields.CreateNewField(“Text”, “SomeField”);
    myList.Fields.Add(someField );

    In this way we get the field reference immediately.

    • jasear
      March 30, 2010 at 9:36 pm

      Hi Lubo,

      The code sample you have posted is actually what I tried before discovering the xml method. Unfortunately, that method is very misleading. It does not do what you think it does. It does not return a reference of the SPField you just added. I know it is very silly but we found that Microsoft were simply using the SPField object to pass in the arguments rather than returning a proper SPField.

      Please refer to this post:


  1. April 3, 2009 at 9:58 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: