Поделиться через


SPViewStyle Class

Represents a style for a view.

Inheritance Hierarchy

System.Object
  Microsoft.SharePoint.SPViewStyle

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel := True)> _
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel := True)> _
Public Class SPViewStyle

Dim instance As SPViewStyle
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
public class SPViewStyle

Remarks

View styles are defined in \\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\GLOBAL\XML\VWSTYLES.XML.

Use the ViewStyles property of the SPWeb class to return the collection of view styles for a site. Use an indexer to return a single view style from the collection. For example, if the collection is assigned to a variable named collViewStyles, use collViewStyles[index] in C#, or collViewStyles(index) in Visual Basic, where index is the index number of the view style in the collection.

Examples

The following code example iterates through the collection of view styles for a site and displays the names of styles that have view fields and the names of the fields.

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

Dim siteCollection As SPSite = SPControl.GetContextSite(Context)
Dim site As SPWeb = siteCollection.AllWebs("Site_Name")
Dim vwStyles As SPViewStyleCollection = site.ViewStyles
Dim vwStyle As SPViewStyle

For Each vwStyle In  vwStyles

    Dim vwFieldCollection As SPViewFieldCollection = vwStyle.ViewFields

    If Not (vwFieldCollection Is Nothing) Then

        Dim vwFieldNames 
            As System.Collections.Specialized.StringCollection = 
            vwFieldCollection.ToStringCollection()
        Dim i As Integer

        For i = 0 To vwFieldNames.Count - 1

            Response.Write(SPEncode.HtmlEncode(vwStyle.Title) & 
                " :: " & SPEncode.HtmlEncode(vwFieldNames(i)) & "<BR>")

        Next i

    End If

Next vwStyle
SPSite oSiteCollection = SPContext.Current.Site;
using(SPWeb oWebsite = oSiteCollection.AllWebs["Website_Name"])
{
    SPViewStyleCollection collViewStyles = oWebsite.ViewStyles;

    foreach (SPViewStyle oViewStyle in collViewStyles)
    {
        SPViewFieldCollection collViewFields = oViewStyle.ViewFields;

        if (collViewFields != null)
        {
            System.Collections.Specialized.StringCollection collFieldNames = collViewFields.ToStringCollection();

            for (int i=0; i<collFieldNames.Count; i++)
            {
                Response.Write(SPEncode.HtmlEncode(oViewStyle.Title) + 
                " :: " + SPEncode.HtmlEncode(collFieldNames[i]) + 
                "<BR>");
            }
        }
    }
}

Note

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Best Practices: Using Disposable Windows SharePoint Services Objects.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

SPViewStyle Members

Microsoft.SharePoint Namespace