Share via


Exercise 1: Creating Lists with Visual Studio 2010

Task 1 – Create Site Columns and Content Type

In this task, you will create site columns and a content type which you will later use to create a SharePoint list definition.

  1. Launch Visual Studio 2010 and create a new empty SharePoint project by selecting File >> New >> Project.
  2. In the New Project dialog, select SharePoint >> 2010 in the Installed Templates section and choose Empty SharePoint Project.
  3. Name the project ListsAndSchemas and save it in C:\%Office365TrainingKit%\Labs\2.2\Source\Before.
  4. In the SharePoint Customization Wizard dialog, in the What local site do you want to use for debugging? textbox, enter the URL of the local SharePoint 2010 site you created for this session, e.g. https://intranet.contoso.com/Lab02. Select the Deployas a sandboxed solution radio button.
  5. Right-click the ListsAndSchemas project in the Solution Explorer window and select Add >> New Item.
  6. Choose Content Type from the list of SharePoint >> 2010 item templates and give it the name of Product.
  7. The wizard dialog will then prompt you to choose a content type that the new content type will inherit from. When you inherit from the Item content type, you can use any of the columns from the Item content type in your new content type.

  8. Select the Item content type from the dropdown list.
  9. In the Elements.xml file of the new content type, change the value of the Name attribute of the content type to Product.
  10. To create two new site columns to add to the content type, add the following CAML just before the opening <ContentType> element.

    (Code snippet 2.2.1)

    XML

    <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" />
    Note:
    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.

    Make sure the ID="" attribute is in CAPS. IntelliSense tries to use the invalid Id="" format.
  11. To add the site columns defined above to the new content type, add the following <FieldRef> elements ContentType/FieldRefs.

    This adds the two site columns to the Products content type, and also adds the existing Title site column from the Item content type to the content type.

    (Code snippet 2.2.2)

    XML

    <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" />
  12. The resulting content type should look like the following CAML. Ensure that the correct GUIDs are used for the ProductDescription and the ProductID fields, referencing the columns created in the previous step.

    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" /> <!-- Parent ContentType: Item (0x01) --> <ContentType ID="0x0100cec90d0b9fd341a8b3b65e5177f1155a" Name="Product" Group="Custom Content Types" Description="My Content Type" Inherits="TRUE" 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>

Task 2 – Create the List Definition

In this task, you will create a list definition based on the Products content type and use it to create a list instance.

  1. Right-click the ListsAndSchemas project in the Solution Explorer window and select Add >> New Item.
  2. Choose List Definition From Content Type and give it a name of ProductList.
  3. In the What is the display name… text box, set the display name of the list to ProductList.
  4. In the What is the type of the list definition? drop down list, choose Product (ListsAndSchemasProduct). Leave the Add a list instancefor this list definition checkbox checked.

  5. To modify the new list definition to use the Products content type, expand ListsAndSchemas >> ProductList in Solution Explorer and open Elements.xml.
  6. In the ListTemplate element, change the Type attribute from 10000 to 10001 to give it a unique identifier.
    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>
  7. In the Solution Explorer window, expand ListsAndSchemas >> ProductList and open Schema.xml.

    This file contains all the details about the list schema, such as the fields and content types it contains, as well as any defined list views.

  8. Add the following <FieldRef> elements to the List/MetaData/Views/View BaseViewID=”0”/ViewFields element to include in the fields in the first list view.

    (Code snippet 2.2.3)

    XML

    <FieldRef ID="{36819A9B-E748-47D5-9949-A65DD195BF80}" Name="ProductDescription" DisplayName="Product Description" /> <FieldRef ID="{5CD2C0C1-67AC-4F9E-BF21-463CFEE9B382}" Name="ProductID" DisplayName="ProductID" />
  9. Add the following <FieldRef> elements to the List/MetaData/Views/View BaseViewID=”1”/ViewFields element to include in the fields in the second list view.

    (Code snippet 2.2.3)

    XML

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

  12. Next, open the ProductList\Products\Elements.xml file.
  13. Change the <ListInstance> element to match the following, changing the Title, TemplateType 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>

Task 3 – Test Custom List Definition

In this task, you will verify that the list instance created by your custom list definition was created correctly in your SharePoint on-premise site.

  1. Save all your changes.
  2. Press [CTRL]+[F5] to build and deploy the solution.
  3. Visual Studio will launch the site in Internet Explorer.
  4. You should see the Products list in the Quick Launch.

  5. 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.

  6. Close Internet Explorer to stop debugging.

Task 4 – Deploy the Solution to SharePoint Online

In this task, you will deploy the solution package to SharePoint online to confirm that it works as expected.

  1. In the Solution Explorer window, right-click on the ListsAndSchemas project and select Package to package the project.
  2. Open Internet Explorer and navigate to your top-level SharePoint Online site, e.g., https://contoso.sharepoint.com.
  3. Click Site Actions and then select Site Settings.

  4. Click the Solutions link in the Galleries section to view the site collection’s solution gallery.

  5. Click on the Solutions tab in the ribbon and click the Upload Solution button.

  6. Browse to the solution package at C:\ %Office365TrainingKit%\Labs\2.2\Source\Before\ListsAndSchemas\bin\Debug\ListsAndSchemas.wsp and click Open.
  7. In the Solution Gallery – Activate Solution, click the Activate button.
  8. Browse to the Lab02 sub site.
  9. Click Site Actions >> Site Settings and choose Manage Site Features.
  10. Click the Activate button next to the ListsAndSchemas Feature1 feature to activate the feature and deploy Products list instance to the Lab02 sub site.
  11. 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.

  12. Verify that the Products list appears in the Quick Launch.
  13. Open the Products list and verify that it was created as expected.
  14. Close Internet Explorer to stop debugging.