main syntax:
1.folder creating
2.items add to parent folder only.
protected void btnsubmit_Click(object sender, EventArgs e)
{
#region maincode
string strIssueID = txttitle.Text.Trim() + "_Announcement";
// strIssueID = hdnIssueID.Value;
// Insert Issue details to List
{
SPWeb objWeb = SPContext.Current.Web;
//SPList DocList = objWeb.Lists.TryGetList(this.parentwebpart.FeedbackDocList);
SPList DocList = objWeb.Lists.TryGetList("ComAnnouncement");
// Britannia.SP.Core.ULSLogger.LogTrace("Got List : " + DocList.Title, "Organoleptics - InsertDocToLibrary");
if (DocList != null)
{
// Britannia.SP.Core.ULSLogger.LogTrace("List Not Null", "Organoleptics - InsertDocToLibrary");
// Create folder in Document Library if not exists
if (DocList.ParentWeb.GetFolder(strIssueID).Exists)
{
}
else
{
SPListItem newfolder = DocList.Items.Add(DocList.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, strIssueID);
newfolder["Modified By"] = SPContext.Current.Web.CurrentUser;
newfolder["Title"] = txttitle.ToString();
newfolder["Body"] = txtbody.ToString();
newfolder["Expires"] = calexpire.SelectedDate;
newfolder["AnnouncementTitle"] = FileUpload1.PostedFile.FileName;
newfolder.Update();
objWeb.AllowUnsafeUpdates = true;
if (DocList.ParentWeb.GetFolder(objWeb.Url + "/" + DocList.Title + "/" + strIssueID).Exists)
{
SPFolder IssueFolder = DocList.ParentWeb.GetFolder(objWeb.Url + "/" + DocList.Title + "/" + strIssueID);
SPFolderCollection folders = objWeb.GetFolder(DocList.RootFolder.ServerRelativeUrl).SubFolders;
foreach (SPFolder spfColl in folders)
{
//Since we have to create the SubFolder under MainFolder so we do this check.
if (spfColl.Name.Equals(newfolder.DisplayName))
{
//obul comment 27/12/03
spfColl.Files.Add(this.FileUpload1.PostedFile.FileName, this.FileUpload1.PostedFile.InputStream);
spfColl.Update();
}
}
}
}
}
}
}
Method1:
protected void btnsubmit_Click(object sender, EventArgs e)
{
SPSite site = SPContext.Current.Site;
SPWeb web = site.OpenWeb();
SPList list = web.Lists["ComAnnouncement"];
web.AllowUnsafeUpdates = true;
SPListItemCollection coll = list.Items;
SPListItem newItem = coll.Add();
Stream fStream = FileUpload1.PostedFile.InputStream;
byte[] _byteArray = new byte[fStream.Length];
fStream.Read(_byteArray, 0, (int)fStream.Length);
fStream.Close();
web.AllowUnsafeUpdates = true;
string _fileUrl = list.RootFolder.Url + "/" + FileUpload1.PostedFile.FileName;
bool IsOverwriteFile = true;
SPFile file = list.RootFolder.Files.Add(_fileUrl, _byteArray, IsOverwriteFile);
newItem["AnnouncementTitle"] = txttitle.ToString();
newItem["Body"] = txtbody.ToString();
newItem["Expires"] = calexpire.SelectedDate;
//SPListItem newItem2 = coll.Add();
newItem = file.Item;
//SPListItem item = file.Item;
newItem["Title"] = FileUpload1.PostedFile.FileName;
newItem.Update();
//newItem2.Update();
file.Update();
//newItem["Title"] = txttitle.ToString();
txttitle.Text = string.Empty;
txtbody.Text = string.Empty;
string seldt = calexpire.SelectedDate.ToString();
}
Method2:
Stream fStream = FileUpload1.PostedFile.InputStream;
byte[] _byteArray = new byte[fStream.Length];
fStream.Read(_byteArray, 0, (int)fStream.Length);
fStream.Close();
web.AllowUnsafeUpdates = true;
string _fileUrl = list.RootFolder.Url + "/" + Path.GetFileNameWithoutExtension(FileUpload1.PostedFile.FileName) + DateTime.Now.ToString("MMddyyyyHHmmss") + Path.GetExtension(FileUpload1.PostedFile.FileName);
bool IsOverwriteFile = false;
SPFile file = list.RootFolder.Files.Add(_fileUrl, _byteArray, IsOverwriteFile);
newItem = file.Item;
Method3:
Stream fStream = FileUpload1.PostedFile.InputStream;
byte[] _byteArray = new byte[fStream.Length];
fStream.Read(_byteArray, 0, (int)fStream.Length);
fStream.Close();
web.AllowUnsafeUpdates = true;
string _fileUrl = list.RootFolder.Url + "/" + FileUpload1.PostedFile.FileName;
bool IsOverwriteFile = true;
SPFile file = list.RootFolder.Files.Add(_fileUrl, _byteArray, IsOverwriteFile);
newItem = file.Item;
file.Update();