Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer
    
Yep, forgot about that one. :) –  Steve Lineberry Apr 17 '12 at 12:00
    
can you check this blog and let me know additional comments? levalencia.wordpress.com/2012/04/17/… –  Esteban V Apr 17 '12 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. –  Per Jakobsen Apr 17 '12 at 17:18

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.

share|improve this answer
    
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. –  Esteban V Apr 17 '12 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. –  Steve Lineberry Apr 17 '12 at 12:01
    
can you check this blog and let me know additional comments? levalencia.wordpress.com/2012/04/17/… –  Esteban V Apr 17 '12 at 13:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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