kamagra how much to take

Casting To SPDocumentLibrary

A friend just asked me the way to cast a SPList To a SPDocumentLibrary so that he could access the document library specific attributes for document library objects. This is one way of doing it:

  1. public static SPWeb GetWeb(string url)
  2. {
  3. using (SPSite site = new SPSite(url))
  4. {
  5. using (SPWeb web = site.OpenWeb())
  6. {
  7. return web;
  8. }
  9. }
  10. }
  11.  
  12. public static SPDocumentLibrary CastDocumentLibrary(string url, string libraryName)
  13. {
  14. return GetWeb(url).Lists[libraryName] as SPDocumentLibrary;
  15. }

Obviously, the casting can take on many forms, but the end result is still an SPDocumentLibrary object.

Share

3 Comments

  1. Viktor Dolezel says:

    Isn’t GetWeb() returning a reference to a disposed object?

  2. Adam Buenz says:

    The expression in the return statement is evaluated before the object is disposed.

  3. Keith Dahlby says:

    I believe Viktor is correct. GetWeb disassembled looks like this:

    public static SPWeb GetWeb(string url)
    {
    SPWeb temp;
    using (SPSite site = new SPSite(url))
    {
    using (SPWeb web = site.OpenWeb())
    {
    temp = web;
    }
    }
    return temp;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>