Monday, September 07, 2009

Programmatically creating a recursive view with WSS web services

I had a project where I wanted to do a few things:
  • Create a view using the Windows SharePoint Services' web services
  • Make this view recursive
  • Set the display style to the alternating line style
What I discovered is that there is not feature parity between the WSS object model and the web service. Also, while it is possible to create the view and make it recursive, there is no support in the WSS 3.0 web services for setting the style on a view.

To create a recursive view:

Add Web Reference

First, add a web reference to the Views web service. You can do this by adding an ASMX web reference in your project to http://yourSharePointSiteUrl/resources/_vti_bin/views.asmx.

Create the Client
SharePointViewService.Views viewClient = new MyProject.SharePointViewService.Views();
viewClient.Credentials = System.Net.CredentialCache.DefaultCredentials;
Create the View via the Web Service
When working with the WSS web services, the basic model is to send the XML fragments to the method call and then parse the result for exceptions.
XmlNode result = viewClient.AddView(listName, viewName, viewFieldsNode, queryNode, rowLimitNode, type, false); // the last parameter: isDefaultView
Here are examples of the parameter values for the above:

listName: "My List"
viewName: "My View"
viewFieldsNode (outer XML):
<ViewFields><FieldRef Name="LinkTitle" />
<FieldRef Name="Item_x0020_Number" />
<FieldRef Name="Description" />
<FieldRef Name="Release" />
<FieldRef Name="Module" />
</ViewFields>
queryNode (outer XML):
<Query>
<OrderBy>
<FieldRef Name="Item_x0020_Number" />
</OrderBy>
<GroupBy Collapse="TRUE" GroupLimit="100">
<FieldRef Name="Release" />
<FieldRef Name="Module" />
</GroupBy>
</Query>
rowLimitNode (outer XML):
<RowLimit Paged="TRUE">100</RowLimit>
type: "HTML"

If exceptions occur, you will have meaningful data in the return value from the AddView method call.

Update the View to Set the Recursive Property
You cannot create the view as a recursive view--the key is that you must follow up the add call with an update call to set this property.
XmlDocument vp = new XmlDocument();
vp.AppendChild(vp.CreateElement("View"));
vp.DocumentElement.SetAttribute("Scope", "Recursive");
viewClient.UpdateView(addView.ListName, viewResult.Name, vp, default(XmlNode), default(XmlNode), default(XmlNode), default(XmlNode), default(XmlNode));
I sincerely hope this helps someone, as the documentation for the WSS web services, particularly the Views web service, is significantly lacking.

2 comments:

  1. Like you, I've found the documentation for the views web service to be sparse at best and I've been fighting an issue for a while now.

    You mention that there is no web service support for setting view styles with WSS 3.0.

    By this do you mean CSS styles, or the Microsoft.SharePoint.SPViewCollection.SPViewType view style?

    I'm working with adding views through the asmx web service and can add a view using SPViewType.Calendar but the view doesn't work unless I go in the List Settings in the actual SharePoint list and change some settings of the view. I can't find these settings reference anywhere in the views web service.

    This may be what you were speaking of in any case do you have any idea about this issue?

    ReplyDelete
  2. msufreeman: I am referring to the latter, the Microsoft.SharePoint.SPViewCollection.SPViewType view style. The web service does not support many things, and I suspect your action is not included as mine was not. A MS engineer suggested I write my own web service that calls the SharePoint object model API and host it on the SharePoint server. Here's hoping this story is better in MOSS 2010.

    ReplyDelete