Partilhar via


SPWebPartCollection.Add Method (ListViewWebPart)

NOTE: This API is now obsolete.

Obsolete. Adds the specified List View Web Part to the collection.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: No

Syntax

'Declaration
<ObsoleteAttribute("Use SPLimitedWebPartManager instead.")> _
Public Function Add ( _
    listWebPart As ListViewWebPart _
) As Guid
'Usage
Dim instance As SPWebPartCollection
Dim listWebPart As ListViewWebPart
Dim returnValue As Guid

returnValue = instance.Add(listWebPart)
[ObsoleteAttribute("Use SPLimitedWebPartManager instead.")]
public Guid Add(
    ListViewWebPart listWebPart
)

Parameters

Return Value

Type: System.Guid
A GUID that identifies the Web Part.

Examples

The following code example adds a List View Web Part to the collection of Web Parts that are displayed on the home page of the current site.

The ListName and ViewGuid properties both require a GUID that is represented as a string, that uses all-uppercase characters, and that is enclosed within curly braces ({ }).

This example requires using directives (Imports in Visual Basic) for both the Microsoft.SharePoint and Microsoft.SharePoint.WebPartPages namespaces.

Dim site As SPWeb = SPContext.Current.Web
Dim list As SPList = site.Lists("List_Name")

Dim listViewWebPart As New ListViewWebPart()

listViewWebPart.ZoneID = "Left"
listViewWebPart.ListName = list.ID.ToString("B").ToUpper()
listViewWebPart.ViewGuid = list.Views(0).ID.ToString("B").ToUpper()

Dim webPartCollection As SPWebPartCollection = 
    site.GetWebPartCollection("default.aspx", Storage.Shared)

Dim guid As System.Guid = webPartCollection.Add(listViewWebPart)
SPWeb oWebsite = SPContext.Current.Web;
SPList oList = oWebsite.Lists["List_Name"];

ListViewWebPart oListViewWebPart = new ListViewWebPart();

oListViewWebPart.ZoneID = "Left";
oListViewWebPart.ListName = oList.ID.ToString("B").ToUpper();
oListViewWebPart.ViewGuid = oList.Views[0].ID.ToString("B").ToUpper();

SPWebPartCollection collWebParts = 
   oWebsite.GetWebPartCollection("default.aspx", Storage.Shared);

System.Guid guid = collWebParts.Add(oListViewWebPart);

See Also

Reference

SPWebPartCollection Class

SPWebPartCollection Members

Add Overload

Microsoft.SharePoint Namespace