Share via


SPList.GetView Method

Returns a view of the list based on the specified GUID.

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

Syntax

'Declaration
<ClientCallableExceptionConstraintAttribute(FixedId := "a", ErrorType := GetType(ArgumentException),  _
    Condition := "There is no view in the view collection with the given view Guid.")> _
<ClientCallableExceptionConstraintAttribute(FixedId := "b", ErrorType := GetType(UnauthorizedAccessException),  _
    Condition := "The user does not have permission to retrieve the specified view.")> _
<ClientCallableMethodAttribute(CacheReturnValue := True)> _
Public Function GetView ( _
    viewGuid As Guid _
) As SPView
'Usage
Dim instance As SPList
Dim viewGuid As Guid
Dim returnValue As SPView

returnValue = instance.GetView(viewGuid)
[ClientCallableExceptionConstraintAttribute(FixedId = "a", ErrorType = typeof(ArgumentException), 
    Condition = "There is no view in the view collection with the given view Guid.")]
[ClientCallableExceptionConstraintAttribute(FixedId = "b", ErrorType = typeof(UnauthorizedAccessException), 
    Condition = "The user does not have permission to retrieve the specified view.")]
[ClientCallableMethodAttribute(CacheReturnValue = true)]
public SPView GetView(
    Guid viewGuid
)

Parameters

  • viewGuid
    Type: System.Guid

    The GUID that identifies the view.

Return Value

Type: Microsoft.SharePoint.SPView
The view.

Remarks

If the value of the viewGuid parameter is Empty, this method returns the default view available to the current user.

Examples

The following code example uses the GetView method to return a specified view for a list and passes this view as a parameter in the GetItems method in order to return and display items.

This example uses the ViewFields property to get the displayed fields in the list, which are then used as indexes for each list item.

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

The example assumes the existence of an .aspx page that contains a label control named Label1.

Dim siteCollection As SPSite = SPContext.Current.Site
Try
    Dim srcList As SPList = siteCollection.AllWebs("Site_Name").Lists("List_Name")
    Dim srcViewGuid As Guid = srcList.Views("View_Name").ID
    Dim srcView As SPView = srcList.GetView(srcViewGuid)
    Dim viewFields As SPViewFieldCollection = srcView.ViewFields
    Dim srcItems As SPListItemCollection = srcList.GetItems(srcView)

    Dim srcItem As SPListItem
    For Each srcItem In  srcItems

        Dim i As Integer
        For i = 0 To viewFields.Count - 1
            Label1.Text += SPEncode.HtmlEncode(srcItem(viewFields(i)).ToString()) + " :: "
        Next i

        Label1.Text += "<BR>"
    Next srcItem

Finally
    siteCollection.Dispose() 
End Try
SPSite oSiteCollection = SPContext.Current.Site;
SPList oList = oSiteCollection.AllWebs["Site_Name"].Lists["List_Name"];
Guid guidViewID = oList.Views["View_Name"].ID;
SPView oViewSrc = oList.GetView(guidViewID);
SPViewFieldCollection collViewFields = oViewSrc.ViewFields;
SPListItemCollection collItemsSrc = oList.GetItems(oViewSrc);

foreach (SPListItem oItemSrc in collItemsSrc)
{
    for (int intIndex=0; intIndex<collViewFields.Count; intIndex++)
    {
        Label1.Text +=               SPEncode.HtmlEncode(oItemSrc[collViewFields[intIndex]].ToString()) 
         + " :: ";
    }
    Label1.Text += "<BR>";
}

See Also

Reference

SPList Class

SPList Members

Microsoft.SharePoint Namespace