Sep 29 2009

Query Demo Web Part

Category: SharePointAlex Angas @ 10:36 pm

A question recently came up on Stack Overflow about querying data from multiple SharePoint lists and binding the results to a GridView control. I realised there were some types of SharePoint queries I hadn’t explored, and that it would be interesting to compare them and see how they work in a web part.

Hence, I created the SPQueryDemo web part! It tests and displays results from the following types of queries:

  • For loop
  • GetListItems from Lists web service
  • SPQuery for SPList objects
  • SPSiteDataQuery for cross-site list queries
  • CrossListQueryInfo for cached cross-site list queries
  • PortalSiteMapProvider for SharePoint Server publishing sites only

Download it from CodePlex.

I’ve also used this project to try out using user controls for output rather than the traditional dynamic creation of controls in a SharePoint web part. This has been attempted with an MVP-style pattern which I’d really appreciate any feedback on.

In the future I’d like to add more functionality, particularly for different query options and types of queries. There is also an annoying bug when changing the query type where the page needs to be refreshed before changes take affect. If anyone would like to help or just check out the code, please download the source and hack away! You can also contact me via this blog post or via Twitter.

Tags: , , , ,


Aug 05 2009

Showing the bulleted links list view

Category: SharePointAlex Angas @ 6:45 pm

A user recently took quite a shine to this lovely bulleted view of links in the list view web part:

Bulleted link list view

They wanted all of their existing links list to be changed over to use this view. What makes this case more unusual is that this is actually the view displayed as “<Summary view>” in the web UI:

Links toolpart

After much fighting with the SPViewCollection on the list, I discovered this post on Ketul Patel’s blog. He showed that to get a reference to this ‘hidden’ view, it is necessary to use the mouthy method SPList.GetUncustomizedViewFromBaseViewId(). The list view web part can then be changed to use the markup for the summary view. (Note that the web part needs to be re-added to the page so that the changes take effect correctly.) Here is a method that does this all this:

[csharp]
public static ListViewWebPart ReplaceWithBaseView(
ListViewWebPart listViewWebPart, SPList list,
int baseViewId, SPLimitedWebPartManager limitedWebPartManager)
{
string zoneID = listViewWebPart.ZoneID;
int zoneIndex = listViewWebPart.ZoneIndex;

SPView view = list.GetUncustomizedViewByBaseViewId(baseViewId);

ListViewWebPart replacementWebPart = new ListViewWebPart
{
Title = listViewWebPart.Title,
ListName = listViewWebPart.ListName,
Hidden = false,
FilterString = listViewWebPart.FilterString,
SuppressWebPartChrome = listViewWebPart.SuppressWebPartChrome,
ViewContentTypeId = listViewWebPart.ViewContentTypeId,
ListViewXml = view.HtmlSchemaXml,
ExportMode = listViewWebPart.ExportMode
};

limitedWebPartManager.DeleteWebPart(listViewWebPart);
limitedWebPartManager.AddWebPart(replacementWebPart, zoneID, zoneIndex);

return replacementWebPart;
}
[/csharp]

In order to call the above method, it’s simply a matter of getting a reference to the web part page and the web part. The magic number for “<Summary view>” is 0. Here is the calling code:

[csharp]
SPList usefulLinksList = web.Lists["Useful Links"];
using (SPLimitedWebPartManager webPartManager =
web.GetLimitedWebPartManager("default.aspx", PersonalizationScope.Shared))
{
try
{
foreach (WebPart webPart in webPartManager.WebParts)
{
if (webPart.Title == "Useful Links")
{
ListViewWebPart listViewWebPart = (ListViewWebPart)webPart;
ReplaceWithBaseView(listViewWebPart, usefulLinksList, 0, webPartManager);
break;
}
}
}
finally
{
webPartManager.Web.Dispose();
}
}
[/csharp]

Tags: , ,