SharePoint: Create List With Lookup, Choice, Date And Person Fields (Schema)

Creating a SharePoint list in Visual Studio is pretty easy these days. We can manage all the list columns in the list definition schema. I will give a walk-through on how to create a list in Visual Studio with various field types. I’ll be using SharePoint hosted app for this.

Prerequisites: SharePoint app is configured and working in the system.

Solution:

  1. Open Visual Studio, New Project, then Apps and select App for SharePoint.

  2. Provide appropriate name to the solution and press OK.

    App for SharePoint

  3. Select your site for debug & deployment, then select SharePoint-hosted and click Finish.

    Select SharePoint hosted

  4. Once project is created, add few lists to the project. Right click on the project, Add, New Item, List, then provide list name. After that click Add and select “Create A Customizable…”, then click Finish.

    Create A Customizable

    click Finish

  5. Above step 4 will create a custom list ‘Doctors’ with list definition. Repeat step #4 to create another list ‘Registrations’.

    Registrations folder

  6. Double click on ‘Doctors’ list to open it in design mode and add additional columns as given in the snapshot below (Change ‘Title’ to ‘Doc Code’).

    Doc Code

  7. You can modify default ‘All Items’ view or create a new List View from ‘Views’ tab (Not mandatory for this article).

    All Items

  8. In the Solution Explorer, if you expand ‘Doctors’ list, you can see a list instance (actual list) and Schema (list definition). Also there are 2 element files; one for list definition and the other for list instance.

    Doctors list

  9. By default list template ‘Type’ is ‘100’ for new list definitions. It is a good practice to assign a new and unique (1000+) value to avoid any conflict. To do this, open ‘List Definition’ element.xml file and change the ‘Type’ value to 1001.

    Type

  10. Since list template type has been changed, same has to be updated in ‘List Instance’ element file. Open the element.xml file and change ‘TemplateType’ to ‘1001’.

    TemplateType

  11. Now we’ll update the list schema (definition) to manage various properties for different data types; choice, multi choice, person, date, etc.

  12. Double click on Schema.xml to open list definition schema. If you collapse all the child tags under <MetaData>, you can see main 4 sections; Content Types, Fields, Views & Forms. In this article, we’ll cover <Fields> schema.

    MetaData

  13. Expand <Fields> to see all the fields we created in step #6. Here we can provide more details for fields which was not possible using designer view.

    designer view

  14. Person Or Group field: ‘Doctor’ is a person or group field which is internally called as ‘User’. I have added some more attributes to this field which are used frequently. Set these values in your schema. [Do not copy paste below as ID’s will be different].
    1. <Field Name="Doctor" ID="{de67c70f-58fc-4a8d-ae13-b19a8536cde3}" DisplayName="Doctor" Type="User" Required="TRUE" ShowField="Name" Presence="TRUE" UserSelectionMode="PeopleOnly" List="UserInfo" />  
  15. Choice field: ‘Shift’ is a choice field. I have added some more attributes and choice options to this field which are used frequently. Update your schema accordingly.
    1. <Field Name="Shift" ID="{b4d2d2dc-26cd-4f6c-ac45-7482cc4fbf59}" DisplayName="Shift" Type="Choice" Format="Dropdown">  
    2.     <CHOICES>  
    3.       <CHOICE>Early Morning</CHOICE>  
    4.       <CHOICE>Regular</CHOICE>  
    5.       <CHOICE>Mid-day</CHOICE>  
    6.       <CHOICE>Night</CHOICE>  
    7.     </CHOICES>  
    8.     <Default>Regular</Default>  
    9.   </Field>  
  16. Multi-choice field: ‘Scheduled Days’ is a multi-choice field. I have added some more attributes and choice options to this field which are used frequently. Update your schema accordingly.
    1. <Field Name="ScheduledDays" ID="{c2e12f19-4e93-44b5-976a-6bc884ef7414}" DisplayName="Scheduled Days" Type="MultiChoice" Format="RadioButtons">  
    2.     <CHOICES>  
    3.       <CHOICE>Sunday</CHOICE>  
    4.       <CHOICE>Monday</CHOICE>  
    5.       <CHOICE>Tuesday</CHOICE>  
    6.       <CHOICE>Wednesday</CHOICE>  
    7.       <CHOICE>Thursday</CHOICE>  
    8.       <CHOICE>Friday</CHOICE>  
    9.       <CHOICE>Saturday</CHOICE>  
    10.     </CHOICES>  
  17. DateTime field: ‘Date of Joining’ is a date time field, here I have added ‘Format’ to date only. Update your schema accordingly.
    1. <Field Name="DateofJoining" ID="{15f54e5a-be23-482a-9bd0-dd0fc79c6f34}" DisplayName="Date of Joining" Type="DateTime" Format="DateOnly" />  
  18. After you are done with above change, save and close ‘Doctors’ list schema.

  19. Double click on ‘Registrations’ list to open it in design mode and add additional columns as given in the following snapshot (Change ‘Title’ to ‘Reg Number’):

    reg number

  20. Repeat step #9 & #10 for ‘Registrations’ list and change Template Type to ‘1002’.

    xml code

    code

  21. Now open ‘Registrations’ list Schema.xml to complete field’s definitions.

    Schema

  22. Lookup field: ‘Assigned Doctor’ is a lookup, lookup source and column must be defined for lookup fields, change your schema as given below:
    1. <Field Name="AssignedDoctor" ID="{e7cea260-3b15-4938-adc4-2eab7b4a60cb}" DisplayName="Assigned Doctor" Type="Lookup" List="Lists/Doctors" ShowField="Title" />  
  23. Choice field: ‘Patient Type’ is a choice with fill-in enabled. Update your schema as given below.
    1. <Field Name="PatientType" ID="{c8e51a4f-d0e5-42a6-9514-a7894b5b51a6}" DisplayName="Patient Type" Type="Choice" FillInChoice="TRUE">  
    2.         <CHOICES>  
    3.           <CHOICE>Internal</CHOICE>  
    4.           <CHOICE>Outdoor</CHOICE>  
    5.         </CHOICES>  
    6. </Field>  
  24. Both lists are ready, build & deploy the app. You should be able to see these lists as defined. You can open these lists directly via a URL or to display these lists on default page, please refer my previous article: SharePoint Hosted App: Display Link Of Lists On A Default Page.

    Deepak Solanki

    Doctor

    ragistration

Thanks!