SPContext.Current.Web.Site.OpenWeb().Lists[“List”]; 之间有什么区别和 SPContext.Current.Web.Lists[“列表”]?
我必须重构同事的一些 sharepoint 2010 代码。每次他需要访问列表时,他都会这样做:
SPContext.Current.Web.Site.OpenWeb().Lists["List"];
我曾经这样做过:
SPContext.Current.Web.Lists["List"];
这两者之间有什么区别,更有效的方法是什么?
I have to refactor some sharepoint 2010 code from my collegue. Everytime he needs to access a list he does this:
SPContext.Current.Web.Site.OpenWeb().Lists["List"];
I used to do this:
SPContext.Current.Web.Lists["List"];
What's the difference between these two and whats the more efficient way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
第二种是更有效的方法。
在第一种方法中,您通过
OpenWeb()
调用创建一个新的SPWeb
对象,这是一个昂贵的调用。请注意,当您使用完该对象后,您还必须手动显式地处置该对象。阅读此处:
http://msdn.microsoft.com/en-我们/library/aa973248(v=office.12).aspx
The second is much more efficient way.
In the first method, you are creating a new
SPWeb
object through theOpenWeb()
call which is an expensive call. Note only that, you must also explicitly dispose this object manually when you are done using that.Read here:
http://msdn.microsoft.com/en-us/library/aa973248(v=office.12).aspx
同意 Madhur
使用第二种方法,因为它不会造成任何内存泄漏
顺便说一句:在 SP2010 中,有一种获取 SPList 的新方法
SPContext.Current.Web.Lists.TryGetList("ListName");
用那个
Agree with Madhur
Use the 2st approach as it will not do any memory leakage
By the way : In SP2010 there is a new method to get SPList
SPContext.Current.Web.Lists.TryGetList("ListName");
use that
替代性能的另一点是语句
SPContext.Current.Web.Site.OpenWeb().Lists["List"]
将从以下位置访问列表List
当前网站集合,而SPContext.Current.Web.Lists["List"];
行将从当前网站访问列表,但是不是来自当前网站集。考虑这种情况...
考虑网站集
http://[web-app]/sites/sa
中存在一个列表Employee
。sa 网站集中有子网站 en-us。
然后,如果使用此行
SPContext.Current.Web.Lists["List"];
那么它将尝试在 sa/en-us 内的网络中查找列表,这反过来会引发错误。同时使用语句
SPContext.Current.Web.Site.OpenWeb().Lists["List"];
将在 sa 网站集中查找列表并成功运行。Another point in-lieu of performance is that the statement
SPContext.Current.Web.Site.OpenWeb().Lists["List"]
will access the listList
from the current site collection while the lineSPContext.Current.Web.Lists["List"];
will access the list from the current web, but not from the current site collection.Consider this scenario...
Consider there is a list
Employee
exists at the site collectionhttp://[web-app]/sites/sa
.And there is subsite en-us at sa site collection.
Then if use this line
SPContext.Current.Web.Lists["List"];
then it will try to find the list in the web inside the sa/en-us which in turn throw a error.Whilw using the statement
SPContext.Current.Web.Site.OpenWeb().Lists["List"];
will find the list in sa site collection and run successfully.马杜尔对于昂贵代码的看法是正确的。我首先认为他明确地处理它是错误的,但他在这一点上也是正确的。
根据最佳实践文档:
SPContext 对象由 SharePoint 框架管理,不应在代码中显式处置。对于 SPContext.Site、SPContext.Current.Site、SPContext.Web 和 SPContext.Current.Web 返回的 SPSite 和 SPWeb 对象也是如此。
但是,您在 SPContext 对象上使用 OpenWeb() 方法如果您查看反编译的程序集,则会返回一个新的 SPWeb 对象。因此,需要明确地处理它。
Madhur is right about the expensive code. I first thought that he was wrong about disposing it explicitly, but he is right on that as well.
According to the Best Practices documentation:
SPContext objects are managed by the SharePoint framework and should not be explicitly disposed in your code. This is true also for the SPSite and SPWeb objects returned by SPContext.Site, SPContext.Current.Site, SPContext.Web, and SPContext.Current.Web.
However you are using OpenWeb() method on a SPContext object which returns a new SPWeb object if you look at the decompiled assembly. Hence, it needs to be disposed explicitly.