Share via


Exercise 1: Creating Lists with Visual Studio 2010

In this exercise you will create a new SharePoint list using the SharePoint development tools included in Visual Studio 2010.

  1. If you haven’t already done so, run the batch file named SetupLab04.bat, found in the c:\Student\Labs\04_Lists\ folder, to create the new site collection that will be used to test and debug the code you will be writing in this lab. This batch file creates a new site collection at the URL of https://intranet.contoso.com/sites/Lab04.
  2. Launch the Internet Explorer and navigate to the top-level site at https://intranet.contoso.com/sites/Lab04. Take a moment to inspect the site and make sure it behaves as expected. Note that the setup script creates a new site collection with a Blank site as its top-level site.
  3. Launch Visual Studio 2010 and create a new empty SharePoint project by select File » New » Project and use the following information to create a new empty project:

    1. Project Types: Installed Templates » SharePoint » 2010
    2. Templates: Empty Project
    3. Name: ListsAndSchemas

    In the SharePoint Customization Wizard, enter the URL of the site created in the previous step (https://intranet.contoso.com/sites/Lab04/) for the debugging site and select the Deployas a farm solution box.

  4. The first step is to create the site columns and content types that will be applied to the new list that you will create. This is done by adding a new project item to the project. Right-click the ListsAndSchemas project in the Solution Explorer tool window and select Add » New Item. Pick Content Type from the list of SharePoint » 2010 templates and give it a name of Product. The wizard dialog will then prompt you for the content type the new content type will inherit from. Select the Item content type from the dropdown list.
  5. Before creating the content type, the first step is to create two new site columns. Add the following CAML just before the opening <ContentType> element. Note that you don’t need to use the same GUIDs as shown here. Just make sure you track the ones you use because you’ll need to reference them in future steps.
    Note:
    Take care to assure the ID="" attribute is in CAPS. IntelliSense tries to use the invalid Id="" format.

    XML

    <?xml version="1.0" encoding="utf-8"?> <Elements xmlns="https://schemas.microsoft.com/sharepoint/"> <Field SourceID="https://schemas.microsoft.com/sharepoint/v3" ID="{36819A9B-E748-47D5-9949-A65DD195BF80}" Name="ProductDescription" DisplayName="Product Description" Group="My Custom Columns" Type="Text" DisplaceOnUpgrade="TRUE" /> <Field SourceID="https://schemas.microsoft.com/sharepoint/v3" ID="{5CD2C0C1-67AC-4F9E-BF21-463CFEE9B382}" Name="ProductID" DisplayName="Product ID" Group="My Custom Columns" Type="Number" DisplaceOnUpgrade="TRUE" /> <ContentType …> <!—Rest of the content type </Content Type> </Elements>
  6. With the site columns created, you can add the columns to the content type. Add a <FieldRefs> element containing <FieldRef> elements for each column (i.e the ones we just added in step 6) to the <ContentType> nodes. Also add an extra <FieldRef> element for the Title field. This adds three columns to the content type. The resulting content type should look like the following CAML. Ensure the correct GUIDs are used for the ProductDescription and the ProductID fields, referencing the columns created in the previous step.

    XAML

    <?xml version="1.0" encoding="utf-8"?> <Elements xmlns="https://schemas.microsoft.com/sharepoint/"> <Field SourceID="https://schemas.microsoft.com/sharepoint/v3" /> <Field SourceID="https://schemas.microsoft.com/sharepoint/v3" /> <ContentType ID="0x0100fb1ad12faa9b4834ad4d590f0f030151" Name="Product" Group="My Custom Content Types" Version="0"> <FieldRefs> <FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Name="Title" DisplayName="Product Name" /> <FieldRef ID="{36819A9B-E748-47D5-9949-A65DD195BF80}" Name="ProductDescription" /> <FieldRef ID="{5CD2C0C1-67AC-4F9E-BF21-463CFEE9B382}" Name="ProductID" /> </FieldRefs> </ContentType> </Elements>
  7. Now the list template and instance can be created that will leverage this content type. Right-click the ListsAndSchemas project in the Solution Explorer tool window, select Add » New Item, pick the List Definition template from the SharePoint » 2010 list and specify a name of ProductList. In the following dialog, set the display name of the list to ProductList and the What is the type of the list definition to Custom List, leaving Add a list instance for this list definition checked.
  8. Now it’s time to modify the list definition (aka: template) and schema. The list definition, located in the Solution Explorer, ListsAndSchemas Project, ProductList\Elements.xml file contains the list template. (Note: do not confuse this Elements.xml file with the Elements.xml file contained in the ListInstance1 container)Change the Type attribute from 10000 to 10001 to give it a unique ID.(Note: All items added by you to SharePoint MUST have a Type ID greater than 10000 in order to avoid conflicting with any that are built into the product or official Microsoft provided additions to the product)

    XML

    <?xml version="1.0" encoding="utf-8"?> <Elements xmlns="https://schemas.microsoft.com/sharepoint/"> <ListTemplate Name="ProductList" Type="10001" BaseType="0" OnQuickLaunch="TRUE" SecurityBits="11" Sequence="410" DisplayName="ProductList" Description="My List Definition" Image="/_layouts/images/itgen.gif"/> </Elements>
  9. Next, open the schema.xml file in the Solution Explorer,ListsAndSchemas Project, ProductList section of the project. This file contains all the details about the list such as what fields will appear, any content types, and the views in the list.
  10. First, add the attribute EnableContentTypes="TRUE" to the <List> node.
  11. Second, add the content type created previously to the <ContentTypes> section near the top of the schema.xml file like so:

    XML

    <?xml version="1.0" encoding="utf-8"?> <List xmlns:ows="Microsoft SharePoint" Title="ProductList" EnableContentTypes="TRUE" FolderCreation="FALSE" Direction="$Resources:Direction;" Url="Lists/ListsAndSchemas-ProductList" BaseType="0" xmlns="https://schemas.microsoft.com/sharepoint/"> <MetaData> <ContentTypes> <ContentTypeRef ID="0x01"> <Folder TargetName="Item" /> </ContentTypeRef> <ContentTypeRef ID="0x0120" /> <ContentTypeRef ID="0x0100fb1ad12faa9b4834ad4d590f0f030151" /> </ContentTypes>
    Note:
    Note: Make sure you use the same content type ID that was generated when you created the Product content type previously. (i.e. the one in the Product Section, Elements.xml file)
  12. Now it's time to add the fields that will be included in the list by adding them to the <Fields> section in the Schema.xml file. Add the three fields that are part of the content type like so:

    XML

    <?xml version="1.0" encoding="utf-8"?> <List xmlns:ows="Microsoft SharePoint" Title="ProductList" EnableContentTypes="TRUE" FolderCreation="FALSE" Direction="$Resources:Direction;" Url="Lists/ListsAndSchemas-ProductList" BaseType="0" xmlns="https://schemas.microsoft.com/sharepoint/"> <MetaData> <ContentTypes>…</ContentTypes> <Fields> <Field ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Name="Title" DisplayName="Product Name" Type="Text" /> <Field ID="{36819A9B-E748-47D5-9949-A65DD195BF80}" Name="ProductDescription" DisplayName="Product Description" Type="Text" /> <Field ID="{5CD2C0C1-67AC-4F9E-BF21-463CFEE9B382}" Name="ProductID" DisplayName="ProductID" Type="Number" /> </Fields>
  13. With the content type and fields added to the list, the last part is to add the fields to two default list views. Search for the <ViewFields> elements and add the fields. There are two default views in the schema.xml file so you’ll find two of them. The first one should look like this:

    XML

    <ViewFields> <FieldRef Name="LinkTitleNoMenu"></FieldRef> <FieldRef ID="{36819A9B-E748-47D5-9949-A65DD195BF80}" Name="ProductDescription" DisplayName="Product Description" /> <FieldRef ID="{5CD2C0C1-67AC-4F9E-BF21-463CFEE9B382}" Name="ProductID" DisplayName="ProductID" /> </ViewFields>
  14. And the second one should look like this:

    XML

    <ViewFields> <FieldRef Name="Attachments"></FieldRef> <FieldRef Name="LinkTitle"></FieldRef> <FieldRef ID="{36819A9B-E748-47D5-9949-A65DD195BF80}" Name="ProductDescription" DisplayName="Product Description" /> <FieldRef ID="{5CD2C0C1-67AC-4F9E-BF21-463CFEE9B382}" Name="ProductID" DisplayName="ProductID" /> </ViewFields>
  15. With the list template and definition complete, now you need to modify the list instance that will be created based off this template. First, in the Solution Explorer, rename the node ProductList\ListInstance1 to ProductList\Products:

    Figure 1

    The list definition in Solution explore. C# shown, VB.NET similar in ProductList area.

  16. Next, open the ProductList\Products\Elements.xml file. Change the <ListInstance> element to match the following, changing the Title, TempateType and Url attributes:

    XML

    <?xml version="1.0" encoding="utf-8"?> <Elements xmlns="https://schemas.microsoft.com/sharepoint/"> <ListInstance Title="Products" OnQuickLaunch="TRUE" TemplateType="10001" Url="Lists/Products" Description="My List Instance"> </ListInstance> </Elements>
  17. Test your work by saving all changes and pressing [CTRL]+[F5] to build and deploy the solution. Eventually Visual Studio will launch the site in the browser and you should see the Products list in the Quick Launch.(Note: as this is based on the RTM build of SharePoint 2010 there may still be a few issues; namely you may see Warnings during the build/deploy process relating to xml files and their schemas. You may ignore these).
  18. You can look in the Site Column Gallery and Site Content Type Gallery to see the other assets created by the Visual Studio 2010 project.
    Note:
    In this exercise you used the new SharePoint Tools in Visual Studio 2010 to create new site columns, a content type, list template and an instance of that list.