PnP core component contains the extension methods which help to achieve the SharePoint task in simple lines of code. So to know more about PnP Core component, have a look at:
WebExtensions.GetWeb
(PnP Core Component) - This extension method returns the child web site based on the specified leaf url. For now, the retrieved the web site only returns the server relative url of the website.
Supports: SharePoint 2013, SharePoint 2016, SharePoint Online
Assembly: OfficeDevPnP.Core.dll
Namespace: Microsoft.SharePoint.Client
Method
Web GetWeb( string leafUrl )
Syntax
Single Line Code: Web.GetWeb(“subsiteurl”)
Code Snippet
The following example returns the child site’s server relative url.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.SharePoint.Client;
- using OfficeDevPnP.Core;
-
- namespace PnPSitesCore.Samples
- {
- class Program
- {
- static void Main(string[] args)
- {
- string siteUrl = "https://sharepointonline.sharepoint.com";
-
- AuthenticationManager authManager = new AuthenticationManager();
-
-
- var context = authManager.GetWebLoginClientContext(siteUrl);
-
-
- Web oweb = context.Web.GetWeb("subsite");
- Console.WriteLine(oweb.ServerRelativeUrl);
-
- Console.WriteLine("Press any key to exit.");
- Console.ReadKey();
- }
- }
- }
The above code isalso available from GitHub.
Note
The returned Web object returns only the server relative url property. If we access other properties from the object, that return a "field or property not initialized" error.