U2U Blog

for developers and other creative minds

TextBox

Developing a List Definition with the Visual Studio 2008 extensions for WSS

Developing a list definition for a custom list using the Visual Studio 2008 extensions for WSS is not that hard because there is an existing project template for it. But having the custom fields displayed in the several forms (edit form, new form, display form, etc) can be a challenge. This post offers you a walkthrough on how you can proceed.

Lets say you want to develop a list definition for a custom list having fields like CustomerCode, Name, City, Country and StartDate. The custom list must also contain a customer ID but as the list item will inherit from the standard list item, the ID column will automatically be created. The Name column will be the standard Title column. When a new customer is entered, you also want to check if the customer code is not already used for another customer, so you will need to add an event receiver to the custom list.

Open Visual Studio 2008 and choose to create an empty SharePoint project. Add a new list item of type SharePoint List Definition. In the dialog that appears, select Custom List and check both the check boxes.

image

This is the structure that is created by the Visual Studio extensions for WSS. Notice that the necessary forms like the AllItems.aspx, DispForm.aspx, etc are all there. They will need no modification. The schema.xml file contains the definition of the list template and it is in here that you will have to do the work. The instance.xml file will create a list instance based on this custom list definition upon activation of the feature. The ItemEventReceiver.cs file will contain the necessary code for checking the existence of the customer code. The ItemEventReceiver.xml file on its turn will install the event receiver upon activation of the feature.

image

As we want to develop an item event receiver, we can remove the ListEventReceiver.cs and the ListEventReceiver.xml file from the project.

First we are going to define the schema of the Customer list. Open the schema.xml file and locate the <Fields> node.

image

Enter a field element for each column in the custom list. If you copy/paste, don't forget to change the GUID of the field ID because the ID must be unique.

<Field ID="{7809EF2B-C225-4459-BC81-50F9CBF1A244}" DisplayName="Code"
       Type="Text" Required="TRUE" MaxLength="15" StaticName="Code"
       Name="Code" RowOrdinal="0"
       SourceID="http://schemas.microsoft.com/sharepoint/v3" />

This Field element should be added for each column needed in the custom list. Also pay attention to attributes like

- Name: this is the internal name of the field. This field doesn't allow spaces. In the case of Company Name you can use CompanyName or Company_x0020_Name.

- DisplayName: this is the user friendly name of the field and allows spaces.

- StaticName: give it the same value as the Name attribute. 

- Type: this indicates the data type of the field. Possible values are the SharePoint data types like Text, Note, DateTime, Currency, Number,... 

- Required: indicates wether a value is required or not.

At the top of the schema definition you find a ContentTypes element containing one or more ContentTypeRef elements. Remove these elements and add a new ContentType element. This is the definition of the Customer content type.

The ID attribute shows the inheritance: this content type inherits from the Item content type which has ID 0x01. The ID should then be followed by 00 and a new GUID.

It is necessary to define a content type to make the fields appear in the New, Edit and Display form.

<ContentTypes>
      <ContentType
        ID="0x0100247971ABF81E4ac9B96C7C6287D18772"
        Name="Customer Item"
        Group="U2U Content Types"
        Description="Customer item content type."
        Version="0">

        <FieldRefs>

               <!-- Here comes the reference to the fields -->
        </FieldRefs>

      </ContentType>

</ContentTypes>

The FieldRefs element of the content type contains the field references to the fields you defined in the Fields element. The first field is the reference to the Title field which is part of the Item content type.

<FieldRefs>
  <FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Name="Title" Required="TRUE" DisplayName="Company Name" />

  <FieldRef
    ID="{7809EF2B-C225-4459-BC81-50F9CBF1A244}"
    Name="Code"
    Required="TRUE"
    ShowInNewForm="TRUE"
    ShowInEditForm="TRUE"/>

  <FieldRef
    ID="{F5BC3F73-88E0-436e-8C2C-C9819FFE2FE3}"
    Name="City"
    Required="FALSE"
    ShowInNewForm="TRUE"
    ShowInEditForm="TRUE"/>

  <FieldRef
    ID="{D5877137-AB9C-4474-93BD-F99B734436D8}"
    Name="Country"
    Required="FALSE"
    ShowInNewForm="TRUE"
    ShowInEditForm="TRUE"/>

  <FieldRef
    ID="{242BF2C4-6459-45f2-B61C-EF7280A701C6}"
    Name="StartDate"
    Required="FALSE"
    ShowInNewForm="TRUE"
    ShowInEditForm="TRUE"/>
</FieldRefs>

 

The content type also contains a XmlDocuments element which is a collection of XmlDocument elements. An XmlDocument element can contain custom information. In this case it defines the form templates to use when displaying the content type.

<XmlDocuments>
    <XmlDocument NamespaceURI="
http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
       <FormTemplates xmlns="
http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
            <Display>ListForm</Display>
            <Edit>ListForm</Edit>
            <New>ListForm</New>
        </FormTemplates>
    </XmlDocument>
</XmlDocuments>

 

Then you have a number of View definitions. To have your fields displayed in the different views, for example the All Items view when viewing the list content, you have to add your custom fields to the ViewFields element of each view.

<ViewFields>
     <FieldRef Name="LinkTitleNoMenu">
     </FieldRef>
     <FieldRef Name="Code" />
     <FieldRef Name="City" />
     <FieldRef Name="Country" />
     <FieldRef Name="StartDate" />
</ViewFields>

 

Now that the list definition is set and done, we can start with the code for the event receiver. When a new customer is entered, you want to check whether the customer code is already used for another customer or not. Open the ItemEventReceiver.cs file and uncomment the ItemAdding event handler:

public override void ItemAdding(SPItemEventProperties properties)
{
    // Check here if the code already exists
    DisableEventFiring();

    string code = null;
    if (properties.AfterProperties["Code"] != null)
    {
        code = properties.AfterProperties["Code"].ToString();
    }

    SPList list = properties.OpenWeb().Lists[properties.ListId];

    if (!ValidateData(list, code))
    {
        properties.Cancel = true;
        properties.ErrorMessage = "This customer code is already in use.";
    }

    EnableEventFiring();
}

 

The ValidateData method is a private method that executes a CAML Query to check whether the customer code already exists or not.

private bool ValidateData(SPList list, string email)
{
   if (email != null)
   {
      SPQuery qry = new SPQuery();
      qry.Query = "<Where><Eq><FieldRef Name='Code' /><Value Type='Text'>"
            + email + "</Value></Eq></Where>";
      SPListItemCollection results = list.GetItems(qry);
      if (results.Count > 0)
          return false;
   }
   return true;
}

 

Time to deploy the list definition. The ListDefinition.xml file contains the definition of the list template. Open the file to modify some metadata. You can change the display name of the list template and set the type to a custom number.

<ListTemplate Name="CustomerListTemplate"
      DisplayName="Customer List Template"
      Description=""
      BaseType="0"
      Type="700"
      OnQuickLaunch="TRUE"
      SecurityBits="11"
      Sequence="410"
      Image="/_layouts/images/itgen.gif" />

 

The instance.xml file defines the list instance that will be created based on your custom list template. Also here you can change f.e. the Title attribute.

<ListInstance FeatureId="bc53ee2c-46b3-41ea-8deb-e75013c92eee"
      Title="Customer List Template instance"
      Url="Lists/CustomerListTemplate">
</ListInstance>

 

Open the project Properties and set the Start browser with URL on the Debug tab to your SharePoint site where you want to deploy your custom list template. Press F5 to start the deploy.

Open an internet browser and navigate to your SharePoint site. The list instance is already created for you. Add a new customer and fill out some data.

image

Add another customer with the same code and you will get an error message indicating that this customer code is already used.

You can download the code here.

Comments (21) -

  • epistolografia

    6/16/2015 10:45:00 AM | Reply

    I have really learned newer and more effective things by your site. One other thing I would like to say is that newer laptop operating systems are likely to allow a lot more memory to get used, but they in addition demand more memory simply to work. If one's computer could not handle more memory as well as the newest application requires that storage increase, it can be the time to buy a new Laptop. Thanks

  • zmarnotrawilbys

    6/24/2015 1:21:47 PM | Reply

    I have been browsing online more than three hours today, yet I never found any interesting article like yours. It is pretty worth enough for me. In my view, if all web owners and bloggers made good content as you did, the internet will be a lot more useful than ever before.

  • zodiakowa

    6/26/2015 5:40:23 PM | Reply

    Many thanks for your post. I want to write my opinion that the price of car insurance varies from one policy to another, due to the fact there are so many different facets which play a role in the overall cost. Such as, the make and model of the auto will have a huge bearing on the price. A reliable outdated family automobile will have a more affordable premium than just a flashy performance car.

  • www hestia olsztyn pl

    7/3/2015 4:14:15 PM | Reply

    I have been examinating out a few of your stories and i can claim clever stuff. I will surely bookmark your website.

  • http www tanie ubezpieczenie eu

    7/3/2015 5:02:55 PM | Reply

    It's the best time to make some plans for the future and it is time to be happy. I've read this post and if I could I desire to suggest you some interesting things or suggestions. Perhaps you can write next articles referring to this article. I wish to read more things about it!

  • www ubezpieczenia holms eu

    7/3/2015 5:05:15 PM | Reply

    Yet another issue is that video games are normally serious as the name indicated with the principal focus on understanding rather than amusement. Although, we have an entertainment facet to keep your young ones engaged, each and every game is often designed to work on a specific skill set or curriculum, such as mathematics or technology. Thanks for your publication.

  • metody pisania pracy magisterskiej

    7/3/2015 5:05:25 PM | Reply

    hello!,I love your writing so a lot! share we communicate extra approximately your article on AOL? I need a specialist on this space to unravel my problem. May be that's you! Taking a look ahead to peer you.

  • korekta prac dyplomowych

    7/6/2015 1:29:05 AM | Reply

    I truly appreciate this post. I’ve been looking all over for this! Thank goodness I found it on Bing. You have made my day! Thanks again

  • hestiaolsztyn.pl/

    7/6/2015 1:29:05 AM | Reply

    F*ckin’ awesome issues here. I am very glad to look your article. Thank you a lot and i am having a look ahead to touch you. Will you please drop me a e-mail?

  • www tanie ubezpieczenia eu

    7/6/2015 5:07:16 AM | Reply

    Hey there! This is my 1st comment here so I just wanted to give a quick shout out and tell you I really enjoy reading through your articles. Can you recommend any other blogs/websites/forums that deal with the same topics? Thanks for your time!

  • http://www.holms.eu

    7/6/2015 5:07:17 AM | Reply

    Greetings from Idaho! I'm bored to death at work so I decided to check out your site on my iphone during lunch break. I love the knowledge you provide here and can't wait to take a look when I get home. I'm surprised at how quick your blog loaded on my mobile .. I'm not even using WIFI, just 3G .. Anyhow, good site!

  • tanie-ubezpieczenie.eu/

    7/7/2015 5:14:10 PM | Reply

    Whoa! This blog looks just like my old one! It's on a totally different subject but it has pretty much the same page layout and design. Excellent choice of colors!

  • ergo hestia olsztyn pl

    7/7/2015 5:14:14 PM | Reply

    of course like your website however you have to test the spelling on quite a few of your posts. Many of them are rife with spelling issues and I find it very troublesome to inform the reality however I’ll certainly come again again.

  • ubezpieczenia holms.eu

    7/10/2015 3:23:35 AM | Reply

    I do consider all the ideas you have introduced in your post. They're very convincing and can definitely work. Still, the posts are very quick for starters. Could you please extend them a bit from next time? Thank you for the post.

  • www ergo hestia olsztyn pl

    7/10/2015 8:14:54 AM | Reply

    I have come across that these days, more and more people are now being attracted to cams and the subject of pictures. However, to be a photographer, you should first spend so much time deciding the exact model of photographic camera to buy and also moving via store to store just so you might buy the most inexpensive camera of the brand you have decided to pick. But it will not end at this time there. You also have to take into consideration whether you should buy a digital dslr camera extended warranty. Thx for the good suggestions I accumulated from your blog.

  • tanie.ubezpieczenie.eu

    7/10/2015 8:14:58 AM | Reply

    Needed to put you this very little remark so as to thank you very much yet again for the magnificent suggestions you have contributed on this page. It's really remarkably open-handed of people like you to grant publicly what exactly some people would have offered for sale for an e book to end up making some cash for their own end, notably now that you could possibly have done it if you ever wanted. Those pointers in addition acted to be the easy way to be sure that some people have similar desire really like my very own to understand many more in respect of this problem. I'm sure there are thousands of more fun opportunities in the future for individuals who browse through your blog.

  • address

    11/1/2015 4:46:45 AM | Reply

    I simply want to tell you that I am newbie to blogging and site-building and really loved your web site. Very likely I’m going to bookmark your website . You amazingly come with beneficial well written articles. Thanks for revealing your web page.

  • Kr2IcyoLi5

    12/7/2015 10:00:33 PM | Reply

    721782 908396Thanks  for helping out,  excellent   information. 141732

Loading

TextBox