Hi!
I come across another strange request from the same client…
I had something to do with displaying all items from all document libraries in the site.
Trying to think caught me off guard, and I decided to go ahead and work with the CQWP (Content Query Web Part).
I found it too complicated and uncustomizable (the client has fantasies about web development).
Scratching my head a bit got me thinking about a nice web part I put together once:
Get a list, use the GetDataTable() mehod, use WriteXml() and parse the whole thing with XSL…
OK!
What about *all* the lists in the web site?
First, the user must select the list template to base the query on (localized):
SPListTemplateCollection listTemplates = SPContext.Current.Site.RootWeb.ListTemplates;
SortedList list = new SortedList();
List<int> templatesToIgnore = new List<int>() { 110, 0x75, 0x76, 0x2776 };
ListItem item = null;
listTypeDropDown = new DropDownList();
#region populate the list template drop down
foreach (SPListTemplate template in listTemplates)
{
int type = (int)template.Type;
if (templatesToIgnore.Contains(type))
{
continue;
}
item = new ListItem(template.Name, Convert.ToString(type, CultureInfo.InvariantCulture));
list[item.Text] = item;
item.Attributes.Add("BaseType", template.BaseType.ToString("D"));
ListItem item2 = new ListItem(SPUtility.GetLocalizedString("$Resources:core,posts_schema_blg_title;",
null, (uint)CultureInfo.CurrentUICulture.LCID), SPListTemplateType.Posts.ToString("d"));
list[item2.Text] = item2;
item2.Attributes.Add("BaseType", "0");
ListItem[] array = new ListItem[list.Count];
list.Values.CopyTo(array, 0);
listTypeDropDown.Items.Clear();
listTypeDropDown.Items.AddRange(array);
//if (string.IsNullOrEmpty(baseType))
//{
// baseType = this.contentByQueryWebPart.ServerTemplate;
//}
}
Second, get all the lists based on that type:
/// <summary>
/// Retrieves all lists based of the specified template type in the provided web
/// </summary>
/// <param name="parentWeb"></param>
/// <param name="listTemplateType"></param>
/// <returns></returns>
/// <exception cref="ArgumentException" />
public static List<SPList> GetListsByType(SPWeb parentWeb, int listTemplateType)
{
List<SPList> lists = new List<SPList>();
if (parentWeb == null)
{
throw new ArgumentException("parentWeb cannot be null");
}
// look for lists based of the requrested template type
foreach (SPList list in parentWeb.Lists)
{
if ((int)list.BaseTemplate == listTemplateType)
{
lists.Add(list);
}
}
return lists;
}
Cheers!