I had a requirement recently to show a view of a list from a child site on the page of a parent site. You can of course do this easily with a content query web part and then just manipulate the XSL to get the output to look the way you want but what if you want to be able to use the out of the box List View Web Part so that you get the toolbar and all the other stuff that the web part provides? Many people aren’t even aware that this is possible but it is – unfortunately it’s kind of a pain to do via the browser – you have to add the web part to a page, export it using a custom tool or code (not sure if SPD allows you to export but you can’t do it via the browser), edit the resultant XML to set the WebId attribute, and then import the web part to the desired location.

To get around all these steps I built a simple command called gl-addlistviewwebpart. I already had a command, gl-setwebpartstate, that could accomplish the same thing but using that command would require that you know the WebId and ListId which would most likely not be available during a scripted install.

The majority of the code is a derivative of the code from the gl-setwebpartstate command. I simply instantiate a ListViewWebPart object, set a few properties and then add the web part using an instance of the SPLimitedWebPartManager class:

 1/// <summary>
 2/// Adds a List View Web Part to the specified page.
 3/// </summary>
 4/// <param name="pageUrl">The page URL.</param>
 5/// <param name="listUrl">The list URL.</param>
 6/// <param name="title">The title.</param>
 7/// <param name="zoneID">The zone ID.</param>
 8/// <param name="zoneIndex">Index within the zone.</param>
 9/// <param name="publish">if set to <c>true</c> [publish].</param>
10public static void Add(string pageUrl, string listUrl, string title, string zoneID, int zoneIndex, bool publish)
11{
12    using (SPSite site = new SPSite(pageUrl))
13    using (SPWeb web = site.OpenWeb())
14        // The url contains a filename so AllWebs[] will not work unless we want to try and parse which we don't
15    {
16        SPFile file = web.GetFile(pageUrl);
17 
18        // file.Item will throw "The object specified does not belong to a list." if the url passed
19        // does not correspond to a file in a list.
20 
21        if (file.InDocumentLibrary)
22        {
23            if (!Utilities.IsCheckedOut(file.Item) || !Utilities.IsCheckedOutByCurrentUser(file.Item))
24                file.CheckOut();
25            // If it's checked out by another user then this will throw an informative exception so let it do so.
26        }
27 
28        string displayTitle = string.Empty;
29        ListViewWebPart lvw = null;
30        SPLimitedWebPartManager manager = null;
31        try
32        {
33            manager = web.GetLimitedWebPartManager(pageUrl, PersonalizationScope.Shared);
34            lvw = new ListViewWebPart();
35            SPList list = Utilities.GetListFromViewUrl(listUrl);
36            if (list == null)
37                throw new ArgumentException("List not found.");
38 
39            lvw.ListName = list.ID.ToString("B").ToUpperInvariant();
40            lvw.TitleUrl = list.DefaultViewUrl;
41            lvw.WebId = list.ParentWeb.ID;
42 
43            if (!string.IsNullOrEmpty(title))
44                lvw.Title = title;
45 
46 
47            displayTitle = lvw.DisplayTitle;
48 
49            manager.AddWebPart(lvw, zoneID, zoneIndex);
50        }
51        finally
52        {
53            if (manager != null)
54            {
55                manager.Web.Dispose();
56                manager.Dispose();
57            }
58            if (lvw != null)
59                lvw.Dispose();
60 
61            
62            if (file.InDocumentLibrary && Utilities.IsCheckedOut(file.Item))
63                file.CheckIn("Checking in changes to page due to new web part being added: " + displayTitle);
64 
65            if (publish && file.InDocumentLibrary)
66            {
67                file.Publish("Publishing changes to page due to new web part being added: " + displayTitle);
68                if (file.Item.ModerationInformation != null)
69                {
70                    file.Approve("Approving changes to page due to new web part being added: " + displayTitle);
71                }
72            }
73        }
74    }
75}

The help for the command is shown below:

C:\>stsadm -help gl-addlistviewwebpart

stsadm -o gl-addlistviewwebpart


Adds a list view web part to a page.

Parameters:
        -url <web part page URL>
        -listurl <list url>
        -zone <zone ID>
        -zoneindex <zone index>
        [-title <web part title>]
        [-publish]

The following table summarizes the command and its various parameters:

Command NameAvailabilityBuild Date
gl-addlistviewwebpartWSS v3, MOSS 2007Released: 8/6/2008
Parameter NameShort FormRequiredDescriptionExample Usage
urlYesThe URL to the page to add the web part to.-url http://portal/pages/default.aspx
listurlluYesThe URL of the list to point to.-listurl http://portal/subsite/Lists/Events, -lu http://portal/subsite/Lists/Events
zonezYesThe web part zone to add the web part to.-zone RightColumnZone, -z RightColumnZone
zoneindexziYesThe index within the zone to add the web part.-zoneindex 1, -zi 1
titletNoThe title to give the web part. If not specified then the list title is used.-title Events, -t Events
publishpNoPublishes the page, if supported. The page will be automatically checked-in to share draft – passing in this parameter will also publish the page.-publish, -p

The following is an example of how to add a list view web part to a page where the list is contained within a sub site:

stsadm -o addlistviewwebpart -url http://portal/pages/default.aspx -listurl http://portal/subsite/lists/events -zone RighColumnZone -zoneindex 1 -title "Company Events" -publish