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:
- Open Visual Studio, New Project, then Apps and select App for SharePoint.
- Provide appropriate name to the solution and press OK.
- Select your site for debug & deployment, then select SharePoint-hosted and click Finish.
- 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.
- Above step 4 will create a custom list ‘Doctors’ with list definition. Repeat step #4 to create another list ‘Registrations’.
- 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’).
- You can modify default ‘All Items’ view or create a new List View from ‘Views’ tab (Not mandatory for this article).
- 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.
- 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.
- 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’.
- Now we’ll update the list schema (definition) to manage various properties for different data types; choice, multi choice, person, date, etc.
- 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.
- 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.
- 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].
- <Field Name="Doctor" ID="{de67c70f-58fc-4a8d-ae13-b19a8536cde3}" DisplayName="Doctor" Type="User" Required="TRUE" ShowField="Name" Presence="TRUE" UserSelectionMode="PeopleOnly" List="UserInfo" />
- 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.
- <Field Name="Shift" ID="{b4d2d2dc-26cd-4f6c-ac45-7482cc4fbf59}" DisplayName="Shift" Type="Choice" Format="Dropdown">
- <CHOICES>
- <CHOICE>Early Morning</CHOICE>
- <CHOICE>Regular</CHOICE>
- <CHOICE>Mid-day</CHOICE>
- <CHOICE>Night</CHOICE>
- </CHOICES>
- <Default>Regular</Default>
- </Field>
- 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.
- <Field Name="ScheduledDays" ID="{c2e12f19-4e93-44b5-976a-6bc884ef7414}" DisplayName="Scheduled Days" Type="MultiChoice" Format="RadioButtons">
- <CHOICES>
- <CHOICE>Sunday</CHOICE>
- <CHOICE>Monday</CHOICE>
- <CHOICE>Tuesday</CHOICE>
- <CHOICE>Wednesday</CHOICE>
- <CHOICE>Thursday</CHOICE>
- <CHOICE>Friday</CHOICE>
- <CHOICE>Saturday</CHOICE>
- </CHOICES>
- DateTime field: ‘Date of Joining’ is a date time field, here I have added ‘Format’ to date only. Update your schema accordingly.
- <Field Name="DateofJoining" ID="{15f54e5a-be23-482a-9bd0-dd0fc79c6f34}" DisplayName="Date of Joining" Type="DateTime" Format="DateOnly" />
- After you are done with above change, save and close ‘Doctors’ list schema.
- 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’):
- Repeat step #9 & #10 for ‘Registrations’ list and change Template Type to ‘1002’.
- Now open ‘Registrations’ list Schema.xml to complete field’s definitions.
- Lookup field: ‘Assigned Doctor’ is a lookup, lookup source and column must be defined for lookup fields, change your schema as given below:
- <Field Name="AssignedDoctor" ID="{e7cea260-3b15-4938-adc4-2eab7b4a60cb}" DisplayName="Assigned Doctor" Type="Lookup" List="Lists/Doctors" ShowField="Title" />
- Choice field: ‘Patient Type’ is a choice with fill-in enabled. Update your schema as given below.
- <Field Name="PatientType" ID="{c8e51a4f-d0e5-42a6-9514-a7894b5b51a6}" DisplayName="Patient Type" Type="Choice" FillInChoice="TRUE">
- <CHOICES>
- <CHOICE>Internal</CHOICE>
- <CHOICE>Outdoor</CHOICE>
- </CHOICES>
- </Field>
- 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.
Thanks!