If you need to check if the site / subsite URL contains a specific value , I wrote this code in my side that working properly :
using (SPSite site = new SPSite("http://Qassasrootsite/"))
{
using (SPWeb web = site.OpenWeb())
{
//Get the sites / sub site
string sitelist = "";
foreach (SPWeb spWeb in site.AllWebs)
{
if (spWeb.Url.ToString().Contains("Qassas"))
{
sitelist += "," + spWeb.Url.ToString();
}
}
}
}
The above solution will work as your above request (Contains) , but it will retrieve all the URL's that contains your provided value (not depend on where it find the provided value in the URL).
For example, you need to find the URL with qassas
keyword
http://rootsite/qassas should be retrieved
http://rootsite/qassas/wiki should be retrieved
Meanwhile, if you need to check the last part in URL to check if it's equal the provided value or not try to split your URL as the following
using (SPSite site = new SPSite("http://Qassasrootsite/"))
{
using (SPWeb web = site.OpenWeb())
{
//Get the sites / sub site
string sitelist = "";
foreach (SPWeb spWeb in site.AllWebs)
{
var siteurl = spWeb.Url.ToString().Split('/');
// get the last item in site uel array that contains the site name
if (siteurl[siteurl.Length - 1].ToString() == "Qassas")
{
sitelist += "," + spWeb.Url.ToString();
}
}
}
}
For example, you need to find the URL with qassas
keyword
http://rootsite/qassas should be retrieved
http://rootsite/qassas/wiki should not be retrieved
Now you have the two code sample, you can select that match your requirements :)