Wednesday, April 13, 2011

Creating views in a list definition using SharePoint Designer 2010

I have been creating some SharePoint 2010 list definitions and list instances in Visual Studio 2010 recently.

I have taken an approach of creating my content types and site columns through the SharePoint UI. Then using the excellent CKSDEV tools, I import the content types and site columns into my Visual Studio project. I reorganize them into folders to keep things tidy.

Then I create a new list definition and list instance from the Add new item menu in Visual studio.

After creating these it is important to remember to edit the elements file of both, changing the descriptive information and the Type to a matching unique number. Usually this number is > 10000.

Now I can deploy my list definition, content type, site columns and list instance through a feature in Visual studio. Visual Studio is kind enough to recognize and resolve conflicts deleting the old instance of the list.

So my solution deploys and when the feature is activated, the list instance is created.

What I wanted to do next was declaratively create some views in the List Definition schema. The XML can be a bit confusing and tedious to write by hand.

So I looked for an alternative. I opened my list in SharePoint Designer 2010 and configured the views the way I wanted. Then I can copy and paste the View XML schema info from SharePoint Designer into the schema XML of my list definition.

image

However, the view would still not deploy correctly.

I found that I had to make a number of changes to the XML to make it work.

These included:

  • Remove the Name property
  • Remove the Level property
  • Add the SetupPath property
  • Add the WebPartZoneID
  • Change the BaseViewID property to the next sequential number for the views in the list
  • Change the URL property to remove the path
  • Add the XslLink element

image

Maybe it will end up being easier to handcraft the XML, but if you want to build your views in SPD and then bring them into Visual Studio, this might help.

If I have missed something or you have a better way of doing this, I’d love to hear about it.

UPDATE: I realised that I can get this information from the Schema tab of the SharePoint Manager instead of SharePoint Designer.

image