Monday, March 28, 2011

Create Custom List Definitions in SharePoint 2010


Create a SharePoint List Definition Project

In this task, you create a list definition SharePoint 2010 project in Microsoft Visual Studio 2010.

To create the SharePoint project

  1. To start Visual Studio 2010, click the Start Menu, click All Programs, click Microsoft Visual Studio 2010, and then click Microsoft Visual Studio 2010.
  2. On the File menu, point to New, and then click Project.
  3. In the New Project dialog window, in the Installed Templates section, click Visual C#, click SharePoint, and then click 2010.
  4. Click List Definition from the project items.
  5. In the Name box, type Bugs and then click OK.
  6. In the SharePoint Customization Wizard, type the local Web site that you want to use for this exercise (such as http://localhost/SampleWebSite).
  7. For the trust level, select Deploy as a farm solution and then click Next.
  8. In the What is the display name of the list definition? Box, type Bugs.
  9. In the What is the type of the list definition? drop-down list, select Custom List.
  10. Select the Add a list instance for this list definition check box and then click Finish.

Customize the SharePoint List Definition

This task guides you through customizing the List Definition project template by changing the list name, the content type it stores, and the fields that are displayed in the new form, edit form, and display form.

To customize the list definition

  1. In Solution Explorer, expand ListInstance1 and open the Elements.xml file.
  2. Within the ListInstance element change the Title attribute to Bugs and change the TemplateType attribute to 10001.
  3. In Solution Explorer, before Schema.xml, open Elements.xml.
  4. Within the ListTemplate element, change the Type attribute to 10001 and add the DisallowContentTypes=”FALSE” attribute.
  5. Insert the following XML into the top of the Elements element in the Elements.xml file as shown in Figure 1. This XML describes the Bug Item content type the list stores.
    <!-- Describes content types. -->
    <ContentType
      ID="0x010089E3E6DB8C9B4B3FBB980447E313CE94"
      Name="Bug Item"
      Group="Custom Content Types"
      Description="Bug item content type."
      Version="0">
      <FieldRefs>
        <FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" />
        <FieldRef ID="{cb55bba1-81a9-47b6-8e6c-6a7da1d25602}" />
        <FieldRef ID="{0248c82f-9136-4b3a-b802-d0b77280b3bc}" />
        <FieldRef ID="{aa4a82dd-5b32-4507-9874-4e1c7bca3279}" />
      </FieldRefs>
    </ContentType>
    <!--  End describes content types. --> 
    
  6. Insert the following XML into the top of the Elements element in the Elements.xml file as shown in Figure 1. This XML describes the fields the Bug Itemcontent type uses.
    <!-- Describes fields in bug list. -->
    <Field Type="Note" DisplayName="Description" Required="FALSE" NumLines="6" RichText="FALSE" Sortable="FALSE" ID="{cb55bba1-81a9-47b6-8e6c-6a7da1d25602}" StaticName="BugDescription" Name="BugDescription" Group="Custom Columns" />
    <Field Type="Text" DisplayName="Project" Required="FALSE" MaxLength="255" ID="{0248c82f-9136-4b3a-b802-d0b77280b3bc}" StaticName="BugProject" Name="BugProject" Group="Custom Columns" />
    <Field Type="Text" DisplayName="Assigned To" Required="FALSE" MaxLength="255" ID="{aa4a82dd-5b32-4507-9874-4e1c7bca3279}" StaticName="BugAssignedTo" Name="BugAssignedTo" Group="Custom Columns" />
    <!-- End describes fields in bug list. -->
    


    Figure 1. Elements.xml file after inserting two code sections

    Elements.xml file after inserting code
  7. In Solution Explorer, open Schema.xml.
  8. Add the EnableContentTypes=”TRUE” attribute to the List element inside the Schema.xml file.
  9. Insert the following XML into the ContentTypes element in the schema.xml file as shown in Figure 2. This XML describes the Bug Item content type this list stores.
    <!-- Start Add. -->
    <ContentTypeRef ID="0x010089E3E6DB8C9B4B3FBB980447E313CE94" />
    <!-- End Add. -->
    
    
  10. Insert the following XML into the Fields element as shown in Figure 2. This XML describes the fields the list stores. These are directly related to the fields in the content type that you added in the previous step.
    <!-- Start Add. -->
    <Field Type="Note" DisplayName="Description" Required="FALSE" NumLines="6" RichText="FALSE" Sortable="FALSE" ID="{cb55bba1-81a9-47b6-8e6c-6a7da1d25602}" StaticName="BugDescription" Name="BugDescription" Group="Custom Columns" />
    <Field Type="Text" DisplayName="Project" Required="FALSE" MaxLength="255" ID="{0248c82f-9136-4b3a-b802-d0b77280b3bc}" StaticName="BugProject" Name="BugProject" Group="Custom Columns" />
    <Field Type="Text" DisplayName="Assigned To" Required="FALSE" MaxLength="255" ID="{aa4a82dd-5b32-4507-9874-4e1c7bca3279}" StaticName="BugAssignedTo" Name="BugAssignedTo" -Group="Custom Columns" />
    <!-- End Add. -->
    


    Figure 2. Schema.xml after inserting the ContentTypeRefs code and Fields code

    Schema.xml after inserting ContentTypeRefs
  11. Insert the following XML into the ViewFields element in the second view, where BaseViewID="1" as shown in Figure 3. This XML describes which fields should be visible in this particular view. BaseView 1 is set as the default view. This is the view the newly created fields should display in.
    <!-- Start Add. -->
    <FieldRef Name="BugDescription"></FieldRef>
    <FieldRef Name="BugProject"></FieldRef>
    <FieldRef Name="BugAssignedTo"></FieldRef>
    <!-- End Add. -->
    


    Figure 3. Schema.xml after Fields XML insert

    Schema.xml after inserting Fields XML
  12. In Solution Explorer, right-click the Bugs node, and then click Deploy.

Test the Solution

In this task, you test the solution by adding a bug to the Bugs list.

To add an item to the Bugs list

  1. Open the website that you specified previously.
  2. On the Home page, click the Bugs list in the left navigation pane.
  3. On the List Tools tab, click Items, and then click Bug Item on the New Item drop-down list as shown in Figure 4.


    Figure 4. Bug Item from the New Item drop-down list

    Bug Item from the New Item listThe Bugs - New Item screen is displayed as shown in Figure 5.


    Figure 5. New Bug Item dialog box

    New Bug Item dialog box
  4. In the Bugs - New Item screen, type Bug01 in the Title box.
  5. Type Who is going to track this bug? in the Description box, type Writing developer articles in the Project box, and then click Save.

Create a Custom List Definition


A field definition defines the columns that appear inside of the list template. For more information about the XML that is used to define a field type.

To create the field definition

  1. Start Microsoft Visual Studio 2010.
  2. On the File menu, point to New, and then click Project.
  3. In Project Types, under Visual Basic or C#, select Empty SharePoint Project.
  4. Type FavoriteColorList as the project name. Click OK.
  5. In the SharePoint Customization Wizard, choose Deploy as a sandboxed solution. Click Finish.
  6. In the Solution Explorer, right-click the FavoriteColorList project and select Add and then New Item.
  7. In the Add New Item dialog box, choose the Empty Element template and type FavoriteColorField as the Name. Click Add.
  8. Insert the following XML inside the Elements element. This XML defines a field for the content type.
    Important note Important
    You must replace FieldId in the ID attribute with a GUID. To generate a GUID, click Tools and then click Create GUID.
    <Field ID="FieldId" Type="Choice" Name="FavoriteColor" DisplayName="Favorite Color" ShowInDisplayForm="TRUE" ShowInNewForm="TRUE" ShowInEditForm="TRUE">
      <CHOICES>
        <CHOICE>Red</CHOICE>
        <CHOICE>Green</CHOICE>
        <CHOICE>Blue</CHOICE>
        <CHOICE>Purple</CHOICE>
        <CHOICE>Magenta</CHOICE>
      </CHOICES>
    </Field>
    
A content type defines a collection of items to use inside of the list. For more information about content types, see Content Types.

To create the content type

  1. In the Solution Explorer, right-click the FavoriteColorList project and select Add and then New Item.
  2. In the Add New Item dialog box, choose the Content Type template and type FavoriteColorContentType as the Name. Click Add.
  3. In the Choose Content Type Settings dialog box, choose Item in the Which base content type should this content type inherit from? drop-down list box. Click Finish.
  4. In the FieldRefs element, you must add a FieldRef element that references the field that you previously defined. The following XML adds a reference to the field.
    Important note Important
    The ID attribute of the FieldRef element must match the ID attribute of the Field element.
    <FieldRef ID="FieldId" Name="FavoriteColor" />
    

To create the list definition from the content type

  1. In the Solution Explorer, right-click the FavoriteColorList project and select Add and then New Item.
  2. In the Add New Item dialog box, choose the List Definition From Content Type template and type FavoriteColorListDefinition as the Name. Click Add.
  3. In the Choose List Definition Settings dialog box, ensure that FavoriteColorContentType is selected in the What content type would you like to use for your list definition? drop-down list box.
  4. Check the Add a list instance for this list definition option. Click Finish.
  5. Next, open the Schema.xml file that was created.
  6. In the ViewFields element, add the same FieldRef element that you added to the content type. The following XML adds a reference to the field.
    Important note Important
    The ID attribute of the FieldRef element must match the ID attribute of the Field element.
    <FieldRef ID="FieldId" Name="FavoriteColor" />
    

To deploy the list definition

  1. Press F5 to deploy the solution.
  2. When the browser is open, click FavoriteColorList - ListInstance1 in the left navigation pane.
  3. After the list opens, observe the Favorite Color field that is now visible. Click Add new item.
  4. Observe the Favorite Color field in the form.

Creating a Page Layout in SharePoint 2010 using Visual Studio 2010



Creating a custom Page Layout in SharePoint 2010 is not too different than creating a custom Page Layout
 in SharePoint 2007. However, the process of creating a SharePoint Solution Package has changed in 
Visual Studio 2010. This article will walk you through the steps.
First, open Visual Studio and create a new C# project, using the "Empty SharePoint Project" template, 
listed under the SharePoint 2010 project templates. For this example, we'll name our project
 "MyCustomPageLayout".
For this example, we'll be deploying our solution as a "Sandboxed solution".
Click "Finish" to wire up your solution to your particular SharePoint site.
As with SharePoint 2007, we'll need to provision our Page Layouts to our SharePoint Site Collection 
Master Page Gallery by using a "Module" element. To do this, right click on the Project and say you want
 to add a new item. Select "Module" from the list of SharePoint 2010 items.
We'll name the Module "masterpage". When you create the new module, you'll see something like this:
By default, the module contains a sample text file you, so you can see how the Module deploys a file to SharePoint. 
However, we don't need it, so we'll delete Sample.txt. Next, we need to wire up our module to point to 
the URL of the Master Page Gallery, so we'll add a "Url" attribute to the Module node. 
Our Elements.xml file should now contain this XML:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="masterpage" Url="_catalogs/masterpage">
  </Module>
</Elements>
Next, we're going to dop our new Page Layout file into the Module. To do thhis, we'll right-click on the "masterpage"
module in the Visual Studio explorer and select "Add new item". 
We're once again presented with the "Add New Item" dialog. If we were to select "Application Page" from the 
SharePoint 2010 menu, we'd get a code-behind page in addition to our ASPX page,
 which we don't want. Since all we want to provision is markup (and not code), 
we're going to use a shortcut. Select the "General" node and select "Text File" from the list of items.
 However, name the file "MyCustomPageLayout.aspx".
Add your Page Layout markup into your MyCustomPageLayout.aspx page. You can do it how I do it, 
which is to create a new Page Layout in SharePoint designer, then copying the code to Visual Studio.
 However, DO NOT FORGET to remove the attributes in the Page Directive at the top of the page that 
SharePoint Designer adds, that look like this:
meta:webpartpageexpansion="full" meta:progid="SharePoint.WebPartPage.Document"
If you don't forget to remove those items from the Page directive, SharePoint will think
 your pages have been customized, even though they were deployed via a Feature.
Next, we'll need to update the Module's Elements.xml file to tell SharePoint what to do with the new Page Layout.
The first thing we'll need to do is to update the "Url" property of the File element, by removing the "masterpage/" 
prefix fromt the Url. (Since we already specified in the Module element that we're deploying the Page Layout
 to the "masterpages" library, if we left the "masterpages/" part of the Url on the file, SharePoint would try 
to provision our Page Layout into a library called "masterpages" inside the existing "masterpages" library! 
We don't want that!) Your new File element should look like this:
<File Path="masterpage\MyCustomPageLayout.aspx" 
Url="MyCustomPageLayout.aspx" />
Now we need to specify some properties for the Page Layout. 
These properties are identical to the kind of properties you would specify for a Page Layout in SharePoint 2007:
  • The Title of the file in SharePoint
  • In our case, the file itself has a URL of MyCustomPageLayout.aspx, but we can also give the page 
  • a Title that will show up when the Page Layout is being selected by the content author, such as "My Custom Page Layout".
  • The Content Type
  • All Page Layouts must inherit from the "Page Layout" content type (which we add using a Resource file value.)
  • A Publishing Preview Image
  • The Publishing Preview Image is the image that shows up when a user picks that Page Layout from the Page Layout picker while they're creating a new page. In this example, we'll just point to the Article Page's preview image.
  • The Publishing Associated Content Type
  • This is the Content Type that the Page Layout is representing, such as an "Article Page", "Welcome Page", etc. The value is a string representing a Lookup value that's a combination of the name of the Content Type and its unique identifier.
    Our new File node will look like this:


    <File Path="masterpage\MyCustomPageLayout.aspx" Url="MyCustomPageLayout.aspx" Type="GhostableInLibrary">
          <Property Name="Title" Value="My Custom Page Layout" />
          <Property Name="ContentType" 

    Value="$Resources:cmscore,contenttype_pagelayout_name;" />
          <Property Name="PublishingPreviewImage" Value="~SiteCollection/_catalogs/masterpage/$Resources:core,Culture;/Preview Images/CustomPageLayout.png, ~SiteCollection/_catalogs/masterpage/$Resources:core,Culture;/Preview Images/CustomPageLayout.png" />
          <Property Name="PublishingAssociatedContentType" Value=";#$Resources:cmscore,contenttype_articlepage_name;;
    #0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130E

    C3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D;#"
    />
    </File>
    Please note, there's no line break inside the PublishingAssociatedContentType value string; 
    I needed to add a line break so you could see the whole value here.
    That's it! We've built our Module.
    Now, we're going to add a module for the Page Layout preview image.
     Right click on the project and add an additional Module element. 
    Delete the "Sample.txt" file from it, and add your new preview image. 
    (In our case, we'll call it CustomPageLayout.png".) Modify your module file so it looks like this:


    <Module Name="Previews Images" 
    Url="_catalogs/masterpage/$Resources:core,Culture;/Preview Images">
      <File Path="Preview Images\CustomPageLayout.png" Url="CustomPageLayout.png" Type="GhostableInLibrary">
          <Property Name="Title" Value="Custom Page Layout Preview Image" />
      </File>
    </Module>
    You might have noticed that a new Feature got created for you when you created a new Module.
     Let's rename the Feature itself MyCustomPageLayout in the project tree, like this:
    We'll open up our MyCustomPageLayout feature, and give it a helpful name and description.
     Notice that you can graphically see that your Module is a part of this Feature, 
    and you can even see which Module files will be provisioned when the Feature is activated.
    The last thing we need to do is to make sure our Solution Package is getting configured 
    correctly, to include our new Feature.
    By default, the "Empty SharePoint Project" will create a DLL for you. 
    If we just went ahead and deployed 
    our Solution Package with the settings as-is, we would be deploying a DLL to the GAC.
     However, since our solution doesn't have any code in it, it would be a blank DLL. 
    That's a bit messy, so let's avoid that. Click on the Project in your Solution Explorer, 
    and look in the Properties Window. 
    Change the "Include Assembly in Package" property to "False".
    Important note: This value is in the Properties Window. 
    I was originally looking for it in the Properties page 
    of the Project (say that three times fast), and I couldn't find it there.
    To deploy our new Solution Package to our Sandboxed Site, we'll go to the 
    "Build" menu in Visual Studio,
     and select "Deploy MyCustomPageLayout".
    This will assemble the Solution Package, deploy the Solution Package to the Sandbox, 
    then activate the Sandboxed feature.
    If we browse to our Master Page Library, we'll now see our new Page Layout.
    Note that, unlike SharePoint 2007, our Page Layout will appear checked out by default,
     if we haven't modified any of the approval or check-out settings 
    of the out of the Master Page Gallery. 
    We'll need to check it in and approve it for it to be available to us.