7 out of 18 rated this helpful - Rate this topic

Walkthrough: Create a Custom Field, Content Type, List Definition, and List Instance

The following procedure illustrates how to create a custom field in SharePoint called Patient Name, as well as a content type that uses the field. It also demonstrates how to add a list definition to the project, along with a list instance based on the list definition, that uses the new content type and field.

This walkthrough demonstrates the following tasks:

  • Creating and customizing a field.

  • Creating and customizing a content type that uses the field.

  • Creating and customizing a list definition.

  • Creating and customizing a list instance of the list definition.

  • Viewing the new field in the content type and list instance.

NoteNote

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.

You need the following components to complete this walkthrough:

First, create a custom field item as a SharePoint project in Visual Studio called Patient Name.

To create a custom field item

  1. Display the New Project dialog box by pointing to New on the File menu, and then clicking New Project.

  2. Expand the SharePoint node under either Visual C# or Visual Basic, and then click 2010.

  3. Click Empty SharePoint Project in the Templates pane, change the name of the project to CustomField1, and then click OK.

    The SharePoint Customization Wizard appears. The Empty Project template is used for custom field items because there is no template for them.

  4. On the Specify the site and security level for debugging page, enter the URL for the SharePoint server site to which you want to add the new custom field item, or use the default location (http://<system name>/).

  5. In the What is the trust level for this SharePoint solution? section, use the default value Deploy as a sandboxed solution.

    For more information about sandboxed and farm solutions, see Sandboxed Solution Considerations.

  6. Click Finish. You should see the project appear in Solution Explorer.

  7. Add an Empty Element project item to the project. To do this, click the field's project node (CustomField1) in Solution Explorer and then click Add New Item on the Project menu.

  8. Expand the SharePoint node under either Visual C# or Visual Basic, and then click 2010.

  9. In the Templates pane, select Empty Element, keep the default name EmptyElement1, and then click Add.

  10. In the Elements.xml file under EmptyElement1, add the following after the <Elements> tag:

    <Field ID="{5744d18c-305e-4632-8bd1-09d134f4830d}" 
        Type="Note" 
        Name="PatientName" 
        DisplayName="Patient Name" 
        Group="A Custom Group">
    </Field>
    

    This adds a field that is named Patient Name.

    Important noteImportant

    When adding a custom field, you must specify the ID, Type, Name, DisplayName, and Group attributes for the field to appear correctly in SharePoint. Statement completion erroneously indicates that you need only the first three of these attributes when in fact all five are required.

  11. Press F5 to run and deploy the project to SharePoint.

    If you get a message that script debugging is disabled, click Yes to continue debugging.

  12. If this is the first time you have debugged a Web application in Visual Studio, you will be asked whether you want to modify the Web configuration file to enable debugging. Answer Yes.

  13. At the top of the SharePoint Web page, click the Site Actions button and then click Site Settings.

  14. Under the Galleries section of the Site Settings page, click the Site columns link.

  15. In the Site Column Gallery page, scroll the page down to the Custom Columns heading and note the new field item added, Patient Name.

  16. Close the browser.

Next, create a content type, based on the SharePoint Contacts list, that uses the new field created in the previous procedure.

To create a custom content type

  1. Add a content type to the project. To do this, click the project node in Solution Explorer and then click Add New Item on the Project menu.

  2. Expand the SharePoint node under either Visual C# or Visual Basic, and then click 2010.

  3. In the Templates pane, select Content Type and use the default name, ContentType1.

  4. In the SharePoint Customization Wizard, select Contact as the content type on which to base the new one, and then click Finish.

    This opens the content type's Elements.xml file for editing.

  5. In the <FieldRefs> section of Elements.xml, copy and paste in:

    <FieldRef ID="{5744d18c-305e-4632-8bd1-09d134f4830d}" 
        Name="PatientName" 
        DisplayName="Patient Name" 
        Required="TRUE"/>
    

    The FieldRef ID used here refers to the Field ID we created for the custom field.

  6. Press F5 to run and deploy the project.

  7. At the top of the SharePoint Web page, click the Site Actions button and then click Site Settings.

  8. Under the Galleries section of the Site Settings page, click the Site content types link.

  9. In the Site Content Types Gallery page, note the new content type we just created, CustomField1 - ContentType1. Click it to view its fields. In the list of fields for CustomField1 - ContentType1, note the field Patient Name is added.

  10. Close the browser.

Now, create a list definition and list instance that will use the new content type and field. Because we want to create the list definition based on the content type we just created, we will use the List Definition From Content Type project item template.

To create a list definition and list instance

  1. Add a list definition to the project. To do this, click the project node in Solution Explorer and then click Add New Item on the Project menu.

  2. Expand the SharePoint node under either Visual C# or Visual Basic, and then click 2010.

  3. In the Templates pane, select List Definition From Content Type and use the default name, ListDefinition1.

  4. In the What is the display name of the list definition? box, leave the default value, CustomField1 - ListDefinition1.

  5. In the What content type would you like to use for your list definition? list, use the default (and only) value, CustomField1 - ContentType1.

    This creates a customizable list definition based on the new, modified Contacts list.

  6. Select the Add a list instance for this list definition box to add a list instance project item to the project. The list instance will be an instance of the new list definition.

  7. Click Finish. You should see the list definition appear in Solution Explorer.

Finally, customize the list definition by adding a new field to it, Patient Name.

Important note Important

The value of the Name attribute in the list definition's Elements.xml must match the folder name of the list definition project item or an error will occur when the project is run. To make sure that the Name attribute matches the folder name, the replacement parameter (or token) $ProjectItemName$ is used. When the SharePoint project is packaged and deployed, this token is converted into the actual folder name of the list definition. If you change the value of the Name attribute to something other than this token, then your application may not run correctly. For more information, see Replaceable Parameters.

To add a field to the list definition

  1. In Solution Explorer, double-click Schema.xml under ListDefinition1 to view it.

    Schema.xml defines all of the fields and other elements of the list definition, such as name and address. Notice under the <Fields> element at the top of the file that the following field definition was added by the content type:

    <FieldRef ID="{5744d18c-305e-4632-8bd1-09d134f4830d}" 
        Name="PatientName" 
        DisplayName="Patient Name" 
        Required="TRUE" />
    
  2. Press F5 to run and deploy the project.

  3. Under the Lists section in the SharePoint QuickLaunch bar, click the CustomField1 - ListInstance1 link.

    This is the list instance for the new list definition. Note that the new Patient Name field does not yet appear as a column in the list instance.

  4. Click List in the List Tools menu tab at the top of the page and then click the Modify View button on the ribbon bar.

  5. In the list of available Column Names, select Patient Name and then click OK. Notice that the Patient Name field now appears in the list instance.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Known issue

Just an FYI; the custom field redeployment problem is referenced in MS KB article 2022443 (http://support.microsoft.com/kb/2022443). There is also a bug filed on it and the VS team is looking into a fix for it.

Amending the fix stated by German
This fix mention by "German" worked for me, except that it did not activate from the post command on the property page. What happened is that the content types installed on the central admin in the "site content types gallery". I had to manually activate the feature in "Central admin" by going to "site settings/site/actions/Manage site features" and the clicking "Activate" for the feature on the page in Central Admin



Then I ran the command "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN\stsadm" -o activatefeature -name "MyFeature" -url http://myserver/sites/testsite/ and the feature installed the content types on the "Subsite" "Site Content types" gallery but not in the top level site. I reviewed the feature1.template.xml file in the Visual Studio 2010 project and found that the default "<property Key="GloballyAvailable" Value="true"> element was set.

I realized that  it "may" be "possible" to set various elements in the feature xml file such as a "web" or "site".  However, it is working and I have other things to do. Hope this adds to the pot of solutions
Redeployment of site column
setting Overwrite="True" in the field caml will get rid of the issue of redeployment of site columns

- Yogesh Pawar
Required="TRUE" does not work
The "Required="TRUE" property  on the fieldRef and Field tags does not work (Should be removed).  Must be contained within a custom content type for this property to work.
Thanks to author
Thanks for good and clear article - all is working !
The field with Id ... defined in feature ... was found in the current site collection or in a subsit
For others running into the problem: The same error message appears when you have two fields with the same GUIDs defined in your elements.xml!

I ran into exactly this problem by doing some copy and pasting of Field elements in the elements.xml - I forgot to change one GUID and got the aforementioned error.
Error on 2nd Deployment: Fix
The problem seems to be with Visual Studio; if you manually activate the feature it works for me.
To do this:

1. Go into your project properties, and on the SharePoint tab, set the Active Deployment Configuration to "No Activation". This will prevent Visual Studio from trying to activate your feature.

2. You could manually activate the feature each time you build your solution, but this can be automated. On the same property page, add a Post-deployment command line to activate the feature such as,

"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN\stsadm" -o activatefeature -name "MyFeature" -url http://myserver/sites/testsite/

Hope it works for you too!
Custom Content Types are not Recognising Custom Forms
Hi, has anyone come across the issue mentioned above. I'm using Visual Studio 2010 RTM and Sharepoint 2010 RTM.
I've observed that the CAM support for adding The <FormsUrl> element to a coustom content type created in VS2010, is not available. I have to paste the markup from one that I dveloped with MOSS 2007. I've also observed, that the Custom form pages, developed using the new Application Page template, are not recognised by the custom content type. Even after a successfull deployment. SharePoint 2010 still uses the default New, Display, and Edit pages, for the new custom content type. But the custom site columns / fields are recognized.

Is there any info anywhere on this behavior, or is it a bug?
2nd Deployment error
Just retract your solution, and restart VS and it's fine.
http://support.microsoft.com/kb/2022443
2nd Deployment Issue for Content Types as well
I'm getting a similar issue on 2nd deployment except with content types. This really makes Visual Studio 2010 not usable for SharePoint development. Back to WSPBuilder it looks like. Ugh!
Content Types and Fields
I've extracted all of the out-of-the-box Content Types and Fields and posted them to my site. It's by no means official documentation, but if you need to use it in your solution at least it'll give you a point in the right direction.

Content Types: http://aarebrot.net/site/index.php/sitedefinitions/69-sharepoint2010contenttypes
Field IDs: http://aarebrot.net/site/index.php/sitedefinitions/68-awesomelistofsharepointidsforsharepoint2010

Edit: I just realized Erik was looking for attributes, not IDs. D'oh. I'll leave the links I guess, in case someone else is looking for them.
Where have all the documentation gone?
It is *very hard* to find updated and comprehensive documentation for instance for the Field attributes.
Seem like at least a “Validate Column” field has been added - are there any other changes from MOSS?

The Walkthroughs should reference the relevant documentation - if such exists.
No bug – just oversimplified walkthrough?
By design Site Columns (AKA Custom Fields, Content Columns ...) which are included in Content Types cannot be deleted.

Thus you have to remove all references to Site Columns (“PatientName” as created in the Custom Field feature “CustomField1”) before the Site Columns can be retracted fully.

The build order in this walkthrough should be: CustomField1, ContentType1.

Before subsequent deployment of the solution in this Walkthrough you should therefore:
1)      Retract the Content Type installed by the Content Type feature (“ContentType1”)
2)      Manually remove the Site Column installed (“Patient Name”)

Note: for some reason at least in our installation pressing the Delete button in Site Settings>Change Site Column has no effect (the last Column in a Site Column Group?) – though the feature can be extracted afterwards as described above.
Name attribute in the list definition's Elements.xml should be $SharePoint.ProjectItem.Name$
See here - http://msdn.microsoft.com/en-us/library/ee231545.aspx
Workaround: Embed fields in list definition
My workaround (if you can call it that) is to embed the custom fields in the list definition schema. Unfortunately that means the custom fields only apply to the list definition and list instances based on it, not the entire site -- but on the other hand it works while we wait for an explanation/solution. If anyone needs an example of how to do this, please contact me through my profile.
Brackets and Sandboxed Solution
I was able to deploy a solution more than once. But I had to make sure that
- the GUIDs of the columns was written with curly brackets ('{' and '}'), otherwise the columns arn't shown in the site columns list.
- the solution was deployed as a sandboxed solution.

PS: I'm using VS 2010 and SP 2010 both RTM.
workaround
this bug is very irritating. What I do for a "workaround" is to change the GUID of the fields, then you can deploy again. 

Does not work
Also seen here. I tried AlwaysForceInstall="TRUE", didn't work for me. Additionally, if I set the VS solution properties to "No Activation", build+deploy and then go to site settings to manually activate the feature, I get a runtime error in the browser.
Still an issue with VS 2010 RTM and SharePoint 2010 RTM
First deployment is fine, second fails.  It doesn't seem to remove/deactivate correctly.  I'll be lodging an official bug...
Same problem
Seems to be a quite common problem. No solution so far :-(
Gettng same error after I try and install the Feature more than once ...
I did set the property "Always Force Install" on the Feature to true, but it still errors. Interesting note, I can navigate to Manage Site Features page and Activate the feature there. All of my custom fields show up after that. Odd.
Resolved error in Activate Features
Ran into the same issue you describe and was able to resolve it by setting "Always Force Install" to true in the feature properties.
Error on 2nd Deployment
I'm using Visual Studio 2010 RTM with SharePoint 2010 RC. Deploying my Custom Field works fine for the first time. But when I try to deploy the solution (without doing any modifications) the second time, I got an error message, saying the the custom field already exists:

Error occurred in deployment step 'Activate Features': The field with Id [GUID] defined in feature [GUID] was found in the current site collection or in a subsite.

Any idea how to get around this error?