How can i retrieve the web from its title programatically.
For example : I have url like http://abc.sharepoint.com/Site1/SubSite1
Now by using "SubSite1"(web Title) I need its web url
How can i retrieve the web from its title programatically.
For example : I have url like http://abc.sharepoint.com/Site1/SubSite1
Now by using "SubSite1"(web Title) I need its web url
in a site collection may be having more than one same titles of subsites so here is a code to get web based on title matches in a site collection
using (SPSite spsite = new SPSite(SPContext.Current.Web.Site.Url))
{
var allwebs = spsite.AllWebs.Cast<SPWeb>().Where(a => a.Title == "web title here");
foreach (SPWeb item in allwebs)
{
//you can get web url from here which web title matches
string url = item.Url;
}
}
Hope it helps
http://abc.sharepoint.com/Site1/SubSite1 seems your environment is SharePoint Online. In that case how this solution can work? @DRS
– Atish Kumar Dipongkor
Nov 26 '15 at 07:13
The SharePoint platform differentiates the Site Title (web Title in your terms) and the Url Address. When you navigate to Site Settings --> "Title, Description, and Logo", you can see both choices.
Your question lacks a bit of clarity. Are you attempting to get a reference Site instance object as in SPWeb for SSOM or Web for CSOM? If so, you need to iterate over a list of all Sites in the Site Collection and pick one that has a matching title or URL fragment. Here is a sample using SSOM (server side object model) which can be used for SP2013 On-Premise.
using (var site = new SPSite("http://abc.sharepoint.com/Site1/SubSite1"))
{
var resultSite = site
.AllWebs
.WebsInfo
.FirstOrDefault(wi => wi.Title == "SubSite1 Title" || wi.ServerRelativeUrl == "/Site1/SubSite1");
If (resultSite == null)
return;
using (var web = site.OpenWeb(resultSite.Id))
{
/// do your work here using SSOM
}
// Retrieving all matches
var resultSites = site
.AllWebs
.WebsInfo
.Where(wi => wi.Title == "SubSite1 Title" || wi.ServerRelativeUrl == "/Site1/SubSite1")
.ToList();
foreach (var spWebInfo in resultSites)
{
using (var web = site.OpenWeb(spWebInfo.Id))
{
/// do your work here using SSOM
}
}
}
If you were to clarify your request for "retrieving web from Title programmatically", then i am sure we can guide you further.
Based on the comment about the URL format, this does look like a SharePoint Online Tenant that you are asking about. The approach is similar but your code needs to be using CSOM or JSOM.
what will you do if there is more than one web title?
– Hitesh Chandegara Nov 26 '15 at 07:23URL http://abc.sharepoint.com/Site1/SubSite1 seems to me that Environment is SharePoint Online.
For SPO, CSOM solution is needed.
Following is the REST API solution.
/_api/web/webs?$select=Url&$filter=Title eq 'SubSite1'
Make a GET request to the above URL, It will give you the Web URL.