Get Current SPWeb From Web Part
Asked Answered
C

1

5

I'm working on a WebPart, to be used in SharePoint, that will display a listing of documents within a document library. To do so, I believe I need to reference the SPWeb object for which the web part is hosted on. Unfortunately, I don't see how to access the current SPSite or SPWeb from within a WebPart. How would I access these from within a WebPart?

Condensable answered 15/9, 2010 at 11:51 Comment(0)
Q
14

Use the static SPContext.Current property to get access to your SharePoint environment. From SPContext you can get access to both the current SPWeb and SPSite.

SPWeb myWeb = SPContext.Current.Web;
SPSite mySite = SPContext.Current.Site;
Quadrinomial answered 15/9, 2010 at 12:52 Comment(1)
I didn't even notice the SPContext property. Thank you very much!Condensable

© 2022 - 2024 — McMap. All rights reserved.