This project is read-only.

Overview

SPManager is just a helper class to get SPSite and SPWeb. What for ?

Well if you want to stop thinking about when to dispose or not to dispose SPWeb & SPSite objects, if you are fed up with checking if you forgot a call somewhere or just do dispose one you shouldn't. SPManager will do the job for you.

What does SPManager do?

It gets, exposes & disposes SPWeb & SPSite objects for you. It also provides a safe ForEach function.

How it works ?

It has 4 constructors :

  • one will call site & web from current context
  • one will get the current context site collection rootweb
  • one will get site from context and new web intance (OpenWeb call)
  • one will get new site & web instance (ie for elevated privileges runs or non web applications)

Instanciate it within a using statement. What ever you do, it will dispose or not the objects depending of the call you did.

Who uses SPManager ?

I do ! so why don't you ? :P

Some examples ?

  • The following code snippet illustrate how to get new web instance
SPSecurity.RunWithElevatedPriviledges(() => {
    using (SPManager spm = new SPManager("http://localhost/"))
    {
        SPList list = spm.Web.Lists["list name"];

        //Do something else...
    }
});
  • The following code snippet illustrate how to get current web from context
using (SPManager spm = new SPManager())
{
    SPList list = spm.Web.Lists["list name"];

    //Do something else...
}
  • The following code snippet illustrate how to get site collection root web from context
using (SPManager spm = new SPManager(true))
{
    SPList list = spm.Web.Lists["list name"];

    //Do something else...
}
  • The following code snippet illustrate loop on all current site's webs
using (SPManager spm = new SPManager("http://localhost/"))
{
    spm.ForEach<SPWeb>(w => {
        if (w.IsRootWeb)
            w.Title = w.Title + " (Root)";
    });
}
  • The following code snippet illustrate loop on all current webapplication's sites
using (SPManager spm = new SPManager("http://localhost/"))
{
    spm.ForEach<SPSite>(s => {

        //Do something ...

    });
}

Thanks for your interest

If you like, dislike, have any idea, feel free to give me a feedback in the discussion pane.

Last edited Jul 4, 2012 at 11:14 AM by sledresseur, version 30