Docstoc

Creating Custom Lists

Document Sample
Creating Custom Lists Powered By Docstoc
					Hands-On Lab
Creating Custom Lists in a Sandboxed
Solution with SharePoint Online
Lab version:    1.0
Last updated:   11/4/2011
CONTENTS

OVERVIEW .................................................................................................................................................. 3
   System Requirements................................................................................................................................ 3

SETUP ........................................................................................................................................................... 3
       Task 1 – Create SharePoint Site............................................................................................................ 3
       Task 2 – Create SharePoint Online Site ................................................................................................ 3
       Task 3 – Prepare Code Snippets ........................................................................................................... 4

EXERCISE 1: CREATING LISTS WITH VISUAL STUDIO 2010 ................................................................ 4
       Task 1 – Create Site Columns and Content Type ................................................................................. 4
       Task 2 – Create the List Definition ....................................................................................................... 7
       Task 3 – Test Custom List Definition ................................................................................................. 10
       Task 4 – Deploy the Solution to SharePoint Online ........................................................................... 11

EXERCISE 2: IMPLEMENT REFERENTIAL INTEGRITY BETWEEN SHAREPOINT LISTS .............. 12
       Task 1 – Enforce Referential Integrity Between Two Lists ................................................................ 12
       Task 2 – Test Referential Integrity Constraints .................................................................................. 15

SUMMARY ................................................................................................................................................. 16
Overview
Lab Time: 20 minutes
Lab Folder: C:\%Office365TrainingKit%\Labs\2.2\Source\Before
Lab Overview: In this lab, you will learn how to use Visual Studio 2010 to create site columns, a content
type, and custom list definition to create in instance of a custom list while working within a Sandboxed
Solution. You will then implement referential integrity between two lists so that the items in a list
cannot be deleted unless referenced items in its child list are deleted first.

System Requirements
You must have the following items to complete this lab:
       SharePoint 2010

       Visual Studio 2010
       Visual Studio 2010 SharePoint Power Tools
       SharePoint Online site
                ◦   Note: You will need administrator access to an SPO site collection to perform the
                    steps in this lab.



Setup
 Note: These setup instructions apply to all the labs in this unit; you don’t need to repeat these steps if
 you already performed them in the first lab in the unit.

Task 1 – Create SharePoint Site
In this task, you will create a sub site for this lab in your SharePoint on-premise site.
        1. Navigate to your top-level SharePoint site, e.g., http://intranet.contoso.com.
        2. From Site Actions, choose New Site.
        3. Choose the Team Site template.
        4. Enter Lab02 for the name of the site to create.
            a. The site will be created at http://intranet.contoso.com/Lab02.
        5. Click Create to create the site.

Task 2 – Create SharePoint Online Site
In this task, you will create a sub site for this lab in your SharePoint Online site.
        1. Navigate to your top-level SharePoint Online site, e.g., https://contoso.sharepoint.com.
        2. From Site Actions, choose New Site.
        3. Choose the Team Site template.
        4. Enter Lab02 for the name of the site to create.
             a. The site will be created at https://contoso.sharepoint.com/Lab02.
        5. Click Create to create the site.

          Note: You have to be an administrator on your SharePoint Online website to perform the tasks
          in this lab, e.g., creating and publishing a SharePoint Designer workflow to the SharePoint
          Online site.


Task 3 – Prepare Code Snippets
This lab contains code snippets that you will use to complete the solution. You can either copy the
snippets from the lab script itself or install the code snippets so that you can access them directly from
Visual Studio.
        1. Browse to C:\%Office365TrainingKit%\Assets\Code Snippets\XML.
        2. Select all the files in this directory and copy them to your clipboard by pressing [Ctrl]+[a] and
           then [Ctrl]+[c].
        3. Browse to ..\Documents\Visual Studio 2010\Code Snippets\XML\My Xml Snippets
        4. Press [Ctrl]+[v] to paste the code snippet files into this directory.

          Note: To reduce typing, you can right-click where you want to insert source code, select Insert
          Snippet, select My Code Snippets and then select the entry matching the current exercise
          step.



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. http://intranet.contoso.com/Lab02. Select the Deploy as 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.




   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.
7. Select the Item content type from the dropdown list.
8. In the Elements.xml file of the new content type, change the value of the Name attribute of
   the content type to Product.
9. 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="http://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="http://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.

10. 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" />

11. 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="http://schemas.microsoft.com/sharepoint/">
               <Field SourceID="http://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="http://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 instance for 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="http://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" />

With the list template and definition complete, now you need to modify the list instance that
will be created based off this template.
10. In the Solution Explorer window, rename the node ProductList\ListInstance1 to
    ProductList\Products:




11. Next, open the ProductList\Products\Elements.xml file.
        12. 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="http://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.
            Visual Studio will launch the site in Internet Explorer.
        3. You should see the Products list in the Quick Launch.




        4. 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.
       5. 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\ListsAndSc
           hemas.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.



Exercise 2: Implement Referential
Integrity between SharePoint Lists
In this exercise you will add referential integrity between lists that you will create in this exercise.
Referential integrity is a new capability added in SharePoint Foundation 2010.
Task 1 – Enforce Referential Integrity Between Two Lists
        1. Open Internet Explorer and navigate to the SharePoint Online site you created for this lab,
           e.g., https://contoso.sharepoint.com/Lab02.
        2. Create a new list named Companies by selecting Site Actions >> More Options…, and then
           select the Custom List template.
        3. Name the list Companies and click Create.
        The Companies list will serve as the parent list in the join between the two lists.




        4. Edit the list to add columns by clicking the List tab in the ribbon, selecting Settings and then
           List Settings:




        5. In the Columns section, click the Create column link to create the two additional columns
           using the values below (leaving all other fields at their default values):
                a. Create a column named Stock Ticker based on column type of Single line of text.
                b. Create a column named Headquarters based on column type of Single line of text.
        6. Go back to the list by clicking Companies in the breadcrumb at the top of the page and add
           the following three list items:

Title                                 Stock Ticker                        Headquarters

Microsoft                             MSFT                                Redmond, WA

Adventure Works Travel                AWT                                 London, United Kingdom

Contoso, Inc.                         CONT                                Seattle, WA
        7. Navigate back to the main page of the Lab02 site.
            Now you need to create a new list named SKUs that will contain products and be treated as
            the child in the relationship.
        8. Refer to the previous instructions to create a new list SKUs (using the Custom List template).
        9. In the SKUs List, go into the List Settings page, and add a lookup column that will act as the
           join column and enforce referential integrity between Companies and SKUs.
             a. Name the new column Manufacturer based on a column type of Lookup.
             b. In the Get information from drop down, choose Companies.
             c. In the Add a column to show each of these additional fields section, select the
                Headquarters field.
                  This setting will cause SharePoint to project the Companies.Headquarters field into the
                  views within the SKUs list containing the value corresponding to the list item selected in
                  the Manufacturer field. This is yet another new and exciting ability in SharePoint 2010
                  that is new in SharePoint Online.
             d. Check the Enforce relationship behavior checkbox and select Restrict delete.

                   Note that these last two selections will prohibit users from deleting items in the
                   Companies list if that item is referenced by items in the SKUs list.

             e. Click OK.
             f.   When prompted, accept the dialog prompting you to create an index on this column.
                  Linked columns in joins must be indexed.

                   Note: If you cancel the dialog, the column will not be created and added to the list.

         10. Create two additional columns for this list.
             a. Create a column named Description based on a column type of Single line of text.
             b. Create a column named Cost based on a column type of Currency.
         11. Navigate back to the SKU’s list and add the following items to that list using the following
             information.

Title                       Manufacturer                Description                     Cost

SharePoint Foundation       Microsoft                   SharePoint Foundation           $0.00
2010                                                    2010

Office                      Microsoft                   Office 2010 Client              $100.00
                                                        Applications

Destination Guide Seattle   Adventure Works Travel      Travel guide for Seattle,       $25.00
                                                        Washington

Destination Guide St.       Adventure Works Travel      Travel guide for St. Johns,     $25.00
Johns                                                   FL

Do-it-Yourself Advanced     Contoso, Inc.               Create your own fireworks       $15.00
Fireworks Set                                           indoor with this advanced
                                                        fireworks set. Real
                                                        gunpowder! Age 4-7.

Model Flying Fortress       Contoso, Inc.               Build a replica of a historic   $10.00
Airplane                                                American bomber
       12. When finished, you should see a list of products. Notice how fields from the Companies list
           were pulled down into this list.




Task 2 – Test Referential Integrity Constraints
In this task, you will test the Restrict Delete and Cascade Delete referential integrity constraint between
the Companies and SKUs lists.
       1. Test the referential integrity between the Companies and SKUs lists. Try and delete the
          Adventure Works Travel item form the Companies list. You will be sent to an error page.




       2. To test out the cascading delete capability, change the referential integrity constraint
          behavior on the Manufacturer column in the SKUs list to Cascade delete.




       3. Try again to delete Adventure Works Travel from the Companies list.
        Be sure to read the warning message that informs you exactly what will happen if you click OK.
        Now click OK. If you browse to the SKUs list, you’ll see two items are now gone.




       4. When the parent and child items were deleted, they were all deleted in a single atomic unit.
          Look inside the Recycle Bin (available via the Quick Launch menu on the left side of the
          screen). You’ll notice a special icon indicating there are multiple items in this deletion set.




Summary
In this lab, you used Visual Studio 2010 to create custom site columns, a content type, and list definition
to create an instance of a custom list. You also learned how to configure referential integrity between
two SharePoint lists and explore the constraints that can be applied to the relationship between the
lists.

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:8
posted:11/4/2011
language:English
pages:16