SPContext.Current is null
-
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);
-
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);
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