2

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

Atish Kumar Dipongkor
  • 13,371
  • 5
  • 32
  • 64
DRS
  • 722
  • 2
  • 17
  • 34

3 Answers3

1

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

Hitesh Chandegara
  • 2,638
  • 6
  • 29
  • 49
  • I noticed your answer as i was writing mine. I would suggest either creating the using block within your foreach, or use WebsInfo property to avoid thrashing SPWeb objects. The SPWebCollection indexer does generate a new instance of the SPWeb object, thus you need to ensure its disposal. – Roman Nov 26 '15 at 07:06
  • URL 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
  • @AtishDipongkor the sample is for server-side object model only and can only work for On prem instances. The code just needs to be refactored for managed CSOM equivalent or JSOM, depending on the requestor's needs. – Roman Nov 26 '15 at 07:30
  • @Roman we can use .WebsInfo property instead of Casting, it is better than casting – Hitesh Chandegara Nov 26 '15 at 07:36
  • Yeah .... It has equivalent... But we need to know OP's need at first. OP did not bother to replay in my comment. – Atish Kumar Dipongkor Nov 26 '15 at 07:37
0

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.

Roman
  • 313
  • 2
  • 14
  • roman u used .FirstOrDefault Property, that means we can get only one result if there is multiple same web title in a site collection,

    what will you do if there is more than one web title?

    – Hitesh Chandegara Nov 26 '15 at 07:23
  • @HiteshChandegara Yes, you are correct, i am only taking the first match to the boolean expression. I could use Where() with ToList() and then iterate over the list ... just like you are doing... this is just a sample. If requestor wants, i can modify my answer. – Roman Nov 26 '15 at 07:26
0

URL 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.

Atish Kumar Dipongkor
  • 13,371
  • 5
  • 32
  • 64