Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

SPWeb.GetFile Method (String)

Windows SharePoint Services 3

Returns the file object located at the specified URL.

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

public SPFile GetFile(
	string strUrl


Type: System.String
A string that contains the URL for the file.

Return Value

Type: Microsoft.SharePoint.SPFile
An SPFile object that represents the file.

If we create a feature stapler for adding some web part or some other information to the default.aspx ( default Home page) at the time of creating a new site based upon a template, then we will not get the information of the default.aspx page in the Feature Activated Event. You can see the issue in the code below, for example, when using the SPFeatureReceiver event.

SPFile file = web.GetFile(“default.aspx”);
    if(file.Exists) // here, this condition will return always false
      // Add AllUsersWebPart

Because this approach tries to pull the details about the default.aspx page while creating the site, you do not get the information about the default.aspx page at that time. The SharePoint object model retrieves the information from the content database, but it is not able to retrieve that information while you are creating the contents for that site in the content DB.

To work around this problem, you can provision the same default.aspx page using the feature stapler. This allows you to retrieve information about the default.aspx page in the SPFeatureReceiver event.

For more information about provisioning, see How to Provision a File.

Community Additions

© 2015 Microsoft