WebFile Object

SharePoint Designer Developer Reference

Represents a file in a Web site.

Remarks

The WebFile object is a member of the WebFiles collection. The WebFiles collection represents all of the files in a specified WebFolder object. Within the WebFiles collection, individual WebFile objects are indexed beginning with zero. The WebFile object is similar to a file in a directory-based hierarchy. You have the ability to create multiple Web objects on a Web server. Any WebFolder object can represent a Web site, but every WebFolder object does not necessarily represent a Web site.

Use WebFiles(index), where index is the ordinal number of a Web page, to return a single WebFile object. The following example returns the file name of the first Web page in the WebFiles collection.

Visual Basic for Applications
ActiveWeb.RootFolder.Files(0).Name

Use the File object to return information about a file on a Web site. The following example returns the Name, Title, and Url properties of each File object on the active Web site.

Note

To run this procedure, you must have a least one Web site open.

Visual Basic for Applications
Private Sub GetWebFileInfo()
    Dim myWeb As Web
    Dim myFiles As WebFiles
    Dim myFile As WebFile
    Dim myFileName As String
    Dim myTitle As String
    Dim myUrl As String
    Set myWeb = ActiveWeb
    Set myFiles = myWeb.RootFolder.Files
    With myWeb
        For Each myFile In myFiles
            myFileName = myFile.Name
            myTitle = myFile.Title
            myUrl = myFile.Url
        Next
    End With
End Sub

Use the IsOpen property to determine whether a file is currently open in Page view. The following example returns the IsOpen property for a specified File object. Notice that the Edit method is used to open the file in this example. For more information on using these methods, see the Edit method.

Note

You must have a Web site open to run this procedure.

Visual Basic for Applications
Private Sub CheckForOpenFile()
    Dim myWeb As Web
    Dim myFiles As WebFiles
    Dim myFile As WebFile
    Dim myFileToOpen As String
    Dim myMessage As String
    Dim myFileName As String
    Set myWeb = ActiveWeb
    Set myFiles = myWeb.RootFolder.Files
    myFileToOpen = "index.htm"
    myMessage = "This file is currently open."
    With myWeb
        For Each myFile In myFiles
            myFileName = myFile.Name
            If myFileName = myFileToOpen Then
                If myFile.IsOpen = True Then
                    MsgBox (myMessage)
                    Exit Sub
                Else
                    myFile.Edit fpPageViewNormal
                    Exit Sub
                End If
           End If
        Next
    End With
End Sub

Use the Checkin, Checkout, and UndoCheckout methods to manage file resources through source control on a Web site. The following statement checks out the first file in the active Web site.

Note

You must have a source control project set up for this statement to work.

Visual Basic for Applications
myFileCheckedOut = ActiveWeb.RootFolder.Files(1).Checkout

Similar to file management features in Microsoft Visual SourceSafe, Office SharePoint Designer also provides an UndoCheckout method that you can use to return a file to its original state. The following statement returns the file to its original state.

Visual Basic for Applications
myFileCheckedOut = ActiveWeb.RootFolder.Files(1).UndoCheckout

You can use the CheckedoutBy property before attempting to check out a file to determine whether the file is currently checked out and by whom. The following statement returns the logon alias of the person who checked out a file or is null if the file isn't currently checked out.

Visual Basic for Applications
myWhoCheckedOutFile = ActiveWeb.RootFolder.Files(0).CheckedoutBy

Use the Properties property to return information about a Web site, such as the type of Web server (vti_webservertype) or whether the Web site has a search bot (vti_hassearchbot). The Properties property returns a collection of key-value pairs used to maintain the meta information. The following statement returns True for the variable mySearchBot if the Web site has a search bot.

Visual Basic for Applications
mySearchBot = ActiveWeb.Properties.Item("vti_hassearchbot")

Use the MetaTags property to return information about the meta tags contained in the HTML coding of a file. The MetaTags property returns a collection of meta tags for a File object, such as the generator of the file. The following example returns the file name and meta tags for each file in a Web site.

Note

To run this procedure, you must have a least one Web site open.

Visual Basic for Applications
Private Sub GetMetaTags()
    Dim myWeb As Web
    Dim myMetaTag As Variant
    Dim myFiles As WebFiles
    Dim myFile As WebFile
    Dim myMetaTags As MetaTags
    Dim myFileName As String
    Dim myMetaTagName As String
    Set myWeb = ActiveWeb
    Set myFiles = myWeb.RootFolder.Files
    With myWeb
        For Each myFile In myFiles
            Set myMetaTags = myFile.MetaTags
            For Each myMetaTag In myMetaTags
                myFileName = myFile.Name
                myMetaTagName = myMetaTag
            Next
        Next
    End With
End Sub

Use the SharedBorders property to return the shared borders on the current Web page or to set new shared borders. The following statement returns the top shared border of the first file in the Files collection of the active Web site.

Visual Basic for Applications
myTopBorder _
    = ActiveWeb.RootFolder.Files(0).SharedBorders(fpBorderTop)

You can also set shared borders on a Web page, as shown in the following statement.

Visual Basic for Applications
ActiveWeb.RootFolder.Files(0).SharedBorders(fpBorderTop) = True

Use the ThemeProperties property to return information about whether the theme uses vivid colors or active graphics. The following example returns the properties of an applied theme and adds vivid colors to the current theme properties if vivid colors haven't been applied to the specified object.

Visual Basic for Applications
Private Sub CheckThemeProperties()
    Dim myFile As WebFile
    Set myFile = ActiveWeb.RootFolder.Files(0)
    If myFile.ThemeProperties(fpThemeActiveGraphics) Then
        myFile.ApplyTheme myFile.ThemeProperties(fpThemeName), myFile.ThemeProperties(fpThemePropertiesAll) + fpThemeVividColors
    Else
        myFile.ApplyTheme myFile.ThemeProperties(fpThemeName), myFile.ThemeProperties(fpThemePropertiesAll) + fpThemeActiveGraphics + fpThemeVividColors
    End If
End Sub

Using File methods

Use the Copy, Delete, Edit, Move, or Open methods to manage your Web pages. A subtle distinction exists between the Edit and Open methods. With the Edit method, you can open and modify a Office SharePoint Designer–compatible file into a PageWindow object. With the Open method, you can open both Office SharePoint Designer–compatible files and any other type of file such as image or text files into the editor that is associated with the file. When you use the Open method to open a file type that is not compatible with Office SharePoint Designer, Office SharePoint Designer does not return a file object. The following example opens a file, deletes a file, and moves a file.

Note

To run this example, you must have a Web site named "C:\My Documents\My Webs Sites\Coho Winery".

Visual Basic for Applications
Private Sub OpenFile()
    Dim myWeb As Web
    Dim myFile As WebFile
    Set myWeb = Webs.Open("C:\My Documents\My Webs Sites\Coho Winery")
    myWeb.Activate
    Set myFile = myWeb.RootFolder.Files("index.htm")
    myFile.Open
End Sub
Private Sub DeleteFile()
    Dim myWeb As Web
    Dim myFile As WebFile
    Set myWeb = ActiveWeb
    Set myFile = myWeb.RootFolder.Files(0)
    myFile.Delete
End Sub
Sub MoveFile()
    Dim myWeb As Web
    Dim myFile As WebFile
    Set myWeb = ActiveWeb
    Set myFile = myWeb.RootFolder.Files(0)
    myFile.Move "New Filename", True, True
End Sub

See Also