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.


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


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.



Henry Chong said...

thanks for this great blogpost! I was about halfway there but missed a few things like the BaseViewID changes...brilliant

Gavin said...

Nice post, quick thing to watch, I was getting "failed to read view file XXXX.aspx" errors until I moved the URL attribute after the SetUpPath in the View element.

AlexM said...

Hi! Thanks for your blog post. I had another problem getting my views to work which had me struggling around for 3 hours:
I changed everything to be as you described, but no matter what I changed, my views were not published.
I finally found out that it wasn't myself causing these problems but the DAMN FREAKI'N vssphost4.exe process. When I just killed it using task manager before deploying my solution via visual studio 2010 everything worked fine.... damn crappy software... :)

Anonymous said...

DAMN FREAKI'N vssphost4.exe
Thanks for letting me know :)
problem solved

Anonymous said...

Very Good Article. After following your blog my problem got resolved. Thanks for writing it.

Anonymous said...

Thanks for the post. Again it came down to the
"DAMN FREAKI'N vssphost4.exe"
Killed that and it all worked.