Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Server Technologies
SDK Documentation
SPSite Class
SPSite Methods
OpenWeb Method
 OpenWeb Method ()
SPSite.OpenWeb Method () (Microsoft.SharePoint)
Returns the site that is associated with the URL that is used in an SPSite constructor.

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

Visual Basic (Declaration)
Public Function OpenWeb As SPWeb
Visual Basic (Usage)
Dim instance As SPSite
Dim returnValue As SPWeb

returnValue = instance.OpenWeb
C#
public SPWeb OpenWeb ()

Return Value

An SPWeb object that represents the site.

When used in conjunction with an SPSite constructor, the OpenWeb method returns the lowest-level site specified by the URL that is passed as parameter for the constructor. For more information about using the OpenWeb method with an SPSite constructor, see the OpenWeb method overload.

The following code example returns the Web site that is located at http://Server_Name/sites/Site_Name/Subsite_Name.

Visual Basic
Dim strUrl As String = "http://Server_Name/sites/Site_Name/Subsite_Name/default.aspx"
Using oSiteCollection As New SPSite(strUrl)
    Using oWebsite As SPWeb = oSiteCollection.OpenWeb()
        ...
    End Using
End Using
C#
string strUrl = 
   "http://Server_Name/sites/Site_Name/Subsite_Name/default.aspx";
using (SPSite oSiteCollection = new SPSite(strUrl))
{
    using(SPWeb oWebsite = oSiteCollection.OpenWeb())
    {
        ...
    }
}
NoteNote:

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.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Memory leak issues...      rnowik   |   Edit   |  

I only recently realised the following...

To avoid memory leaks, you should explicitly call the Close / Dispose method when you are finished with your SPWeb and SPSite objects. Alternatively use using structures in your code to ensure that the Dispose method is called. E.g.

using (SPSite mySite = new SPSite(url))
{
using (SPWeb myWeb = mySite.OpenWeb())
{
// Do something here ...
}
}

Edit: found this article that gives more info http://msdn2.microsoft.com/en-us/library/ms778813.aspx

Tags What's this?: Add a tag
Flag as ContentBug
Memory leak issues?      0x00BC   |   Edit   |  

You should use using() or a finally clause on everything you instantiate that has a Close or Dispose method.

This usually doesn't indicate memory leak, it indicates that unmanaged resources are still open.

It's the job of class designers to release unmanaged resources in both the Dispose/Close and Finalize methods. Generally, if you Close/Dispose something explicitly you can suppress the finalizer from being called by the GC:

Private m_finalized As Boolean = False
Protected Overrides Sub Finalize()
Dispose()
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
If Not m_finalized Then
If m_web IsNot Nothing Then
m_web.Dispose()
End If
        m_finalized = True
GC.SuppressFinalize(Me)
End If
End Sub
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker