2

I already read the document about disposal about 10 times in the last 3 years. But my question is different.

Why wouldnt I always use SPContext.Site and .Web

instead of always creating an object instance? using (SPSite site =

I suppose it has to do with the kind of operations you are going to do with the objects? As far as I know creating an instance its more resource intensive, so the more I use the Context object the better?

2 Answers 2

3

Also create new SPSite and SPWeb inside of RunWithElevatedPrivileges. SPSite object permissions are determined when they are created, so SPContext.Current.Site will already have the permissions of the current user even if you get the reference within RWEP.

3
  • Yep, forgot about that one. :) Commented Apr 17, 2012 at 12:00
  • can you check this blog and let me know additional comments? levalencia.wordpress.com/2012/04/17/… Commented Apr 17, 2012 at 13:24
  • Add the following to bullet 4 in your blog: When you need to access data from another Site collection/site than the current. Commented Apr 17, 2012 at 17:18
4

I create a new SPSite or SPWeb instance when I don't have a SPContext (console app, powershell, unit test, etc) or I need to get data from a different SPSite and SPWeb than the one I'm currently accessing.

3
  • good point. I am actually seeing the code in the project I am and the developer uses new SpSite and New SpObject all over the place, like 20 times per page. Commented Apr 17, 2012 at 11:53
  • 2
    Wow, yea, if you turn on the developer dashboard, you should be able to see all of the sprequest objects and how much time they are taking up on page load. Commented Apr 17, 2012 at 12:01
  • can you check this blog and let me know additional comments? levalencia.wordpress.com/2012/04/17/… Commented Apr 17, 2012 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.