none
SpSite Object RRS feed

  • Question

  • What is teh diff betn getting spsite Object through site URL and using spContext Object?
    which one is better and recomonded?why?
    Monday, August 10, 2009 10:49 AM

Answers

  • Hi,

    If you have a context (for example, SharePoint web application), then you should use the SPSite from that. In a console application, you should go with SPSite(http://yoursite/).

    I suggest you to read this article:
    Best Practices: Using Disposable Windows SharePoint Services Objects
    http://msdn.microsoft.com/en-us/library/aa973248.aspx

    Hope that helps.

    Peter
    Monday, August 10, 2009 10:58 AM
  • As Peter says, if you're writing code in a page that will be accessed through the web site, such as in a page you put in the "LAYOUTS" directory, or a web part that you write, you can use the SPContext object. Basically, if someone had to log in to SharePoint to view your code, you'll use the SPContext object. Otherwise, as was mentioned, if you're writing code that will run outside the context of the web application, such as a console application, you'll have to create a new SPSite object.
    Visit my blog at blog.beckybertram.com or follow me on Twitter: @beckybertram
    Monday, August 10, 2009 11:47 AM

All replies

  • Hi,

    If you have a context (for example, SharePoint web application), then you should use the SPSite from that. In a console application, you should go with SPSite(http://yoursite/).

    I suggest you to read this article:
    Best Practices: Using Disposable Windows SharePoint Services Objects
    http://msdn.microsoft.com/en-us/library/aa973248.aspx

    Hope that helps.

    Peter
    Monday, August 10, 2009 10:58 AM
  • As Peter says, if you're writing code in a page that will be accessed through the web site, such as in a page you put in the "LAYOUTS" directory, or a web part that you write, you can use the SPContext object. Basically, if someone had to log in to SharePoint to view your code, you'll use the SPContext object. Otherwise, as was mentioned, if you're writing code that will run outside the context of the web application, such as a console application, you'll have to create a new SPSite object.
    Visit my blog at blog.beckybertram.com or follow me on Twitter: @beckybertram
    Monday, August 10, 2009 11:47 AM
  • Becky and Peter's responses cover this well, to summarise:

    Use SPContext for
    * WebParts
    * Application Pages, Feature Recievers and Item Event Receivers (use local properties if available, such as this.Site with App Pages or properties.Web.Site with Item Event Receivers).

    Use new SPSite for
    * Windows Forms Applications
    * Console Applications
    * Services

    If the code is running under the context of SharePoint use SPContext or local properties.

    Dave
    My SharePoint Blog - http://www.davehunter.co.uk/blog
    Tuesday, August 11, 2009 2:43 PM
  • Hmm I didn't see this mentioned.

    Creating a SPSite object gives you more control.  You can create a SPSite object in any users security context.  When you use SPContext, you only have the security context for the current logged in user.  And like the other experts mentioned, sometimes you're restricted where you can use what.  Anytime you don't have a HttpContext you wont have a SPContext.  But I like Dave's summary.
    Tuesday, August 11, 2009 5:18 PM