Get SPWeb Object from the URL

PowerShell Snap-in for SharePoint 2010 offers Get-SPWeb cmdlet to get SPWeb Object directly from the given web URL. But when writing .Net code in C# (or in PowerShell with MOSS 2007) SharePoint object model doesn’t offer this mechanism.

However there is a Trick to get SPWeb Object from given URL: Get the SPWeb by calling SPSite.OpenWeb() Method.

Get SPWeb from Given URL: C# Code:

Using(SPSite site=new spsite(URL))
{
	using(SpWeb web=site.openweb())
	{
		//Do something 
	}
}

Same way, With MOSS 2007 PowerShell:

$webURL="https://sharepoint.compnay.com/sites/sales/us" #Subsite
$site = New-Object Microsoft.SharePoint.SPSite($WebURL) 
$web= $site.OpenWeb()

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

Your email address will not be published. Required fields are marked *