SPUserToken systemAccountUserToken = SPContext.Current.Site.SystemAccount.UserToken;
using (SPSite elevatedSite = new SPSite(SPContext.Current.Site.ID, systemAccountUserToken))
{
using (SPWeb cweb = elevatedSite.OpenWeb(SPContext.Current.Site.RootWeb.ID)) {
// SPSecurity.RunWithElevatedPrivileges(delegate() {
//using (SPSite csite = new SPSite(SPContext.Current.Site.ID)) {
// using (SPWeb cweb = csite.OpenWeb(SPContext.Current.Site.RootWeb.ID)) {
// SPWeb cweb = SPContext.Current.Web;
string strURL = cweb.Url;
//web.AllowUnsafeUpdates = true;
//Code to store in SP site
SPDocumentLibrary _MyDocLibrary = (SPDocumentLibrary)cweb.Lists["Documents"];
SPFolderCollection _MyFolders = cweb.Folders;
SPFolder programFolder = _MyDocLibrary.ParentWeb.GetFolder(_MyDocLibrary.RootFolder.Url + "/" + "LED");
if (!programFolder.Exists)
{
_MyFolders.Add(strURL + "/Documents/" + "LED" + "/");
_MyDocLibrary.Update();
}
SPFolder BundlesMFolder = _MyDocLibrary.ParentWeb.GetFolder(_MyDocLibrary.RootFolder.Url + "/" + "LED" + "/" + "Bundles");
if (!BundlesMFolder.Exists)
{
_MyFolders.Add(cweb.Url + "/Documents/" + "LED" + "/" + "Bundles");
_MyDocLibrary.Update();
}
SPFolder bundleFolder = _MyDocLibrary.ParentWeb.GetFolder(_MyDocLibrary.RootFolder.Url + "/LED/Bundles/" + bundleNumber);
// SPFolder bundleFolder = cweb.GetFolder(_MyDocLibrary.RootFolder.Url + "/LED/Bundles/" + bundleNumber);
SPFolder siteFolder = null;
if (!bundleFolder.Exists)
{
cweb.AllowUnsafeUpdates = true;
// _MyFolders.Add(cweb.Url + "/Documents/LED/Bundles/" + bundleNumber + "/");
_MyDocLibrary.RootFolder.SubFolders.Add(cweb.Url + "/Documents/LED/Bundles/" + bundleNumber + "/");
_MyDocLibrary.Update();
}
//SPDocumentLibrary _MyDocLibrary1 = (SPDocumentLibrary)cweb.Lists["Documents"];
//SPFolderCollection _MyFolders1 = cweb.Folders;
if (bundleFolder.Exists)
{
siteFolder = _MyDocLibrary.ParentWeb.GetFolder(_MyDocLibrary.RootFolder.Url + "/LED/Bundles/" + bundleNumber + "/" + GeolocID);
if (!siteFolder.Exists)
{
cweb.AllowUnsafeUpdates = true;
_MyDocLibrary.RootFolder.SubFolders.Add(cweb.Url + "/Documents/LED/Bundles/" + bundleNumber + "/" + GeolocID + "/");
_MyDocLibrary.Update();
// cweb.AllowUnsafeUpdates = false;
}
}
siteFolder = _MyDocLibrary.ParentWeb.GetFolder(_MyDocLibrary.RootFolder.Url + "/LED/Bundles/" + bundleNumber + "/" + GeolocID);// _MyDocLibrary.RootFolder.SubFolders["DocsFolder"];
string generatedDocName = System.IO.Path.GetFileName(NewFileLoc);
Stream fileStream = File.Open(NewFileLoc, FileMode.OpenOrCreate, FileAccess.ReadWrite);
SPFile spfile = siteFolder.Files.Add(generatedDocName, fileStream, true);
if (siteFolder.RequiresCheckout)
{
spfile.CheckIn("Upload Comment", SPCheckinType.MajorCheckIn);
spfile.Publish("Publish Comment");
}
/////////////////////////////
}
}
using (SPSite elevatedSite = new SPSite(SPContext.Current.Site.ID, systemAccountUserToken))
{
using (SPWeb cweb = elevatedSite.OpenWeb(SPContext.Current.Site.RootWeb.ID)) {
// SPSecurity.RunWithElevatedPrivileges(delegate() {
//using (SPSite csite = new SPSite(SPContext.Current.Site.ID)) {
// using (SPWeb cweb = csite.OpenWeb(SPContext.Current.Site.RootWeb.ID)) {
// SPWeb cweb = SPContext.Current.Web;
string strURL = cweb.Url;
//web.AllowUnsafeUpdates = true;
//Code to store in SP site
SPDocumentLibrary _MyDocLibrary = (SPDocumentLibrary)cweb.Lists["Documents"];
SPFolderCollection _MyFolders = cweb.Folders;
SPFolder programFolder = _MyDocLibrary.ParentWeb.GetFolder(_MyDocLibrary.RootFolder.Url + "/" + "LED");
if (!programFolder.Exists)
{
_MyFolders.Add(strURL + "/Documents/" + "LED" + "/");
_MyDocLibrary.Update();
}
SPFolder BundlesMFolder = _MyDocLibrary.ParentWeb.GetFolder(_MyDocLibrary.RootFolder.Url + "/" + "LED" + "/" + "Bundles");
if (!BundlesMFolder.Exists)
{
_MyFolders.Add(cweb.Url + "/Documents/" + "LED" + "/" + "Bundles");
_MyDocLibrary.Update();
}
SPFolder bundleFolder = _MyDocLibrary.ParentWeb.GetFolder(_MyDocLibrary.RootFolder.Url + "/LED/Bundles/" + bundleNumber);
// SPFolder bundleFolder = cweb.GetFolder(_MyDocLibrary.RootFolder.Url + "/LED/Bundles/" + bundleNumber);
SPFolder siteFolder = null;
if (!bundleFolder.Exists)
{
cweb.AllowUnsafeUpdates = true;
// _MyFolders.Add(cweb.Url + "/Documents/LED/Bundles/" + bundleNumber + "/");
_MyDocLibrary.RootFolder.SubFolders.Add(cweb.Url + "/Documents/LED/Bundles/" + bundleNumber + "/");
_MyDocLibrary.Update();
}
//SPDocumentLibrary _MyDocLibrary1 = (SPDocumentLibrary)cweb.Lists["Documents"];
//SPFolderCollection _MyFolders1 = cweb.Folders;
if (bundleFolder.Exists)
{
siteFolder = _MyDocLibrary.ParentWeb.GetFolder(_MyDocLibrary.RootFolder.Url + "/LED/Bundles/" + bundleNumber + "/" + GeolocID);
if (!siteFolder.Exists)
{
cweb.AllowUnsafeUpdates = true;
_MyDocLibrary.RootFolder.SubFolders.Add(cweb.Url + "/Documents/LED/Bundles/" + bundleNumber + "/" + GeolocID + "/");
_MyDocLibrary.Update();
// cweb.AllowUnsafeUpdates = false;
}
}
siteFolder = _MyDocLibrary.ParentWeb.GetFolder(_MyDocLibrary.RootFolder.Url + "/LED/Bundles/" + bundleNumber + "/" + GeolocID);// _MyDocLibrary.RootFolder.SubFolders["DocsFolder"];
string generatedDocName = System.IO.Path.GetFileName(NewFileLoc);
Stream fileStream = File.Open(NewFileLoc, FileMode.OpenOrCreate, FileAccess.ReadWrite);
SPFile spfile = siteFolder.Files.Add(generatedDocName, fileStream, true);
if (siteFolder.RequiresCheckout)
{
spfile.CheckIn("Upload Comment", SPCheckinType.MajorCheckIn);
spfile.Publish("Publish Comment");
}
/////////////////////////////
}
}