Adding file attachment in custom list programmatically
Code:
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite mySite = new SPSite(“Site Url”); //Mentioned site url
SPWeb spweb = mySite.OpenWeb();
SPList list = spweb.Lists[“HR Policies”];
int ID =
Convert.ToInt32(this.Page.Request.QueryString["ItemID"].ToString());
SPListItem
spNewItem = myList.Items.Add();
spWeb.AllowUnsafeUpdates
= true;
string fileName1 = System.IO.Path.GetFileName(“file Name”);//Mention
File name
if
(fileName1 != "")
{
Stream fStream = firstinputFile.PostedFile.InputStream;
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0,
(int)fStream.Length);
fStream.Close();
fStream.Dispose();
SPAttachmentCollection attachments = myNewItem.Attachments;
fileName1 = Path.GetFileName(firstinputFile.PostedFile.FileName);
attachments.Add(fileName1, contents);
spNewItem.Update();
}
});
No comments:
Post a Comment