Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. SharePoint
  4. SPContext.Current is null

SPContext.Current is null

Scheduled Pinned Locked Moved SharePoint
sharepointhelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hardus Lombaard
    wrote on last edited by
    #1

    I am new to Sharepoint development and I searched the web for an answer, but couldn't find anything that helped me. I am trying to upload text to a document library, but I get an error stating that SPContext.Current is null. What am I forgetting?

    SPSite siteCollection = SPContext.Current.Site;
    SPWeb curSite = SPContext.Current.Web;
    SPDocumentLibrary library = (SPDocumentLibrary)curSite.Lists["Shared Documents"];
    string documentName = txtFileName.Text;
    string libraryRelativePath = library.RootFolder.ServerRelativeUrl;
    string libraryPath = siteCollection.MakeFullUrl(libraryRelativePath);
    string documentPath = libraryPath + "/" + documentName;

    Stream documentStream = new MemoryStream();
    StreamWriter writer = new StreamWriter(documentStream);
    writer.Write(txtDocumentBody.Text);
    writer.Flush();

    curSite.Files.Add(documentPath, documentStream, true);

    R 1 Reply Last reply
    0
    • H Hardus Lombaard

      I am new to Sharepoint development and I searched the web for an answer, but couldn't find anything that helped me. I am trying to upload text to a document library, but I get an error stating that SPContext.Current is null. What am I forgetting?

      SPSite siteCollection = SPContext.Current.Site;
      SPWeb curSite = SPContext.Current.Web;
      SPDocumentLibrary library = (SPDocumentLibrary)curSite.Lists["Shared Documents"];
      string documentName = txtFileName.Text;
      string libraryRelativePath = library.RootFolder.ServerRelativeUrl;
      string libraryPath = siteCollection.MakeFullUrl(libraryRelativePath);
      string documentPath = libraryPath + "/" + documentName;

      Stream documentStream = new MemoryStream();
      StreamWriter writer = new StreamWriter(documentStream);
      writer.Write(txtDocumentBody.Text);
      writer.Flush();

      curSite.Files.Add(documentPath, documentStream, true);

      R Offline
      R Offline
      Rajesh Sitaraman
      wrote on last edited by
      #2

      Where are you using this code? If you are running it any other place like console or win forms you should use SPWeb OpenWeb method. --- Rajesh MVP

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups