Using linq with Sharepoint and deleting objects

How can I refer to subWeb in the following query?

using (SPSite spSite = Utility.GetElevatedSite(_rootUrl))
{
    from SPWeb web in spSite.AllWebs
    where web.ServerRelativeUrl.ToLower() == path
    from SPWeb subWeb in web.Webs                            
    select subWeb
}

Do I even need to worry about subWeb recycling if iam has already cracked spSite in a Using statement?

Edit:

Is it good, for example, to cause garbage collection in this scenario?

+3
source share
2 answers

Sorry, you do it. The problem starts with the SPSite.AllWebs property. The SPWeb.Web property is also unsafe.

Read this very detailed link about situations where you need to worry about deleting SharePoint objects.

(I suggest adding this to my SharePoint cheat sheet).

, SharePoint LINQ.

, .

Edit: SPDisposeCheck - , .NET . .

http://code.msdn.microsoft.com/SPDisposeCheck

http://blogs.msdn.com/sharepoint/archive/2008/11/12/announcing-spdisposecheck-tool-for-sharepoint-developers.aspx

+6

- "": SPWeb , SPSite, , SPSite . , SPWeb, , , , SPWeb.

LINQ #. , - , # . AsSafeEnumerable(), :

using (SPSite spSite = Utility.GetElevatedSite(_rootUrl))
{
    var sw = from SPWeb web in spSite.AllWebs.AsSafeEnumerable()
             where web.ServerRelativeUrl.ToLower() == path
             from SPWeb subWeb in web.Webs.AsSafeEnumerable()      
             select subWeb;
    foreach(SPWeb aSubWeb in sw)
    {
        // Do something
    }
}

, sw, IEnumerable<SPWeb>. , SPWeb , . , SPWeb SP * (SPList ..) foreach. sw using, SPWebCollections SPSite.

, , , - (!), . , , spSite.AllWebs[path] from/where.

, - , , GC .

, GetElevatedSite. RunWithElevatedPrivileges SPSite, , , SPSite . , SPSite - .

+2

Source: https://habr.com/ru/post/1704948/


All Articles