URLがSPSiteの有効なSPWebのかどうかを確認してください
質問
URLを持ち、特定のURLのサブサイトとして存在するかどうかを確認したいです。
- URL=
www.go.com
- = www.go.comがurl=
www.go.com/abx
を持っているかどうかをチェックしたいか、
私は+ 1000冊のサブウェブを持っています、私はそれらすべてをループしなければならない、ショートカットはありませんか?
解決
あなたはスーパーモードとニュージーが提案されたもので行くことができます、あなたはまた以下のコードを使うことを選ぶかもしれません:
using (SPSite site = new SPSite("http://www.go.com/"))
{
using (SPWeb oWeb= site.OpenWeb())
{
SPWeb IsWebExists = oWeb.Webs.FirstOrDefault(x => x.Name == "abx");
if (IsWebExists == null)
{
//Your Code
}
}
}
. 他のヒント
下のコード:
using (SPSite site = new SPSite("http://www.go.com/"))
{
using (SPWeb web = site.OpenWeb("abx"))
{
if (web.Exists)
Console.WriteLine("exists");
else
Console.WriteLine("not exists");
}
}
.
site.OpenWeb
の内部では、Webのサイト相対URLを提供します。
次の
を試すことができますstring serverUrl = "http://myserver";
string siteUrl = "/sites/SiteCollection";
string subSiteUrl = "This Subsite Does Not Exist";
using (SPSite site = new SPSite(serverUrl + siteUrl))
{
using (SPWeb web = site.OpenWeb(subSiteUrl))
{
if (web.Exists)
{
// do work with the web...
Console.WriteLine(site.Url);
Console.WriteLine(web.Url);
}
}
}
. 所属していません sharepoint.stackexchange