모든 레벨에서 spweb를 얻으십시오
-
28-09-2020 - |
문제
다른 수준의 사이트가있는 루트 사이트가 있습니다 (깊이는 알 수 없음).가장 깊은 수준까지 반복되는 모든 SPWEB 객체를 어떻게 찾을 수 있습니까?
아래 코드가 나에게 원하는 결과를줍니다
SPWebApplication mySPWebApp = SPWebApplication.Lookup(new Uri("http://test-site"));
{
foreach (SPSite siteCollection in mySPWebApp.Sites)
{
foreach (SPWeb oweb in siteCollection.AllWebs)
{
//My Code
}
}
}
. 해결책
yes, spwebapplication.sits 웹 응용 프로그램 및 spsite.allwebs 레벨에 관계없이 사이트 모음에서 모든 사이트를 얻을 수 있습니다.
모범 사례는 AllWebs 속성을 통해 리턴 된 컬렉션에서 검색된 개별 웹 사이트의 명시 적으로 폐기하는 것입니다.
foreach (SPWeb oweb in siteCollection.AllWebs)
{
try
{
//Your code
}
finally
{
oweb.Dispose();
}
}
.
사이트 모음을 버리고 있어야합니다.
다른 팁
@ naim의 답변을 빌드하려면 사이트 모음과 웹을 모두 폐기하고 Dispose 또는 Dismply를 사용하여 Try / Catch를 사용해야합니다.
사이트 컬렉션 예제
모범 사례 : 일회용 Windows SharePoint 사용서비스 개체void SPSiteCollectionForEachNoLeak()
{
using (SPSite siteCollectionOuter = new SPSite("http://moss"))
{
SPWebApplication webApp = siteCollectionOuter.WebApplication;
SPSiteCollection siteCollections = webApp.Sites;
foreach (SPSite siteCollectionInner in siteCollections)
{
try
{
// ...
}
finally
{
if(siteCollectionInner != null)
siteCollectionInner.Dispose();
}
}
} // SPSite object siteCollectionOuter.Dispose() automatically called.
}
.
웹 폐기 예제
모범 사례 : 일회용 Windows SharePoint 사용서비스 개체void SPWebCollectionAddNoLeak(string strWebUrl)
{
using (SPSite siteCollection = new SPSite("http://moss"))
{
using (SPWeb outerWeb = siteCollection.OpenWeb())
{
SPWebCollection webCollection = siteCollection.AllWebs; // No AllWebs leak just getting reference.
using (SPWeb innerWeb = webCollection.Add(strWebUrl))
{
//...
}
} // SPWeb object outerWeb.Dispose() automatically called.
} // SPSite object siteCollection.Dispose() automatically called.
}
. 제휴하지 않습니다 sharepoint.stackexchange