0

I have tried so many ways and I searched so many articles, I didn't get any solution for attaching file in a SharePoint list using sand box solution.

I got solution using farm solutions. How can we achieve it using Sand Box solution?

protected void Button1_Click(object sender, EventArgs e) {

        try
        {
            using (SPSite oSPsite = new SPSite(SPContext.Current.Web.Url))
            {
                using (SPWeb oSPWeb = oSPsite.OpenWeb())
                {
                    oSPWeb.AllowUnsafeUpdates = true;
                    SPList list = oSPWeb.Lists["FileAttach"];
                    //Add a new item in the List
                    SPListItem itemToAdd = list.Items.Add();
                    SPAttachmentCollection attachmentCollection = itemToAdd.Attachments;

                     Stream attachmentStream;
                    Byte[] attachmentContent;
                    // Get the file from the file upload control
                    if (this.flprojectieadformat.HasFile)
                    {
                        attachmentStream = this.flprojectieadformat.PostedFile.InputStream;
                        attachmentContent = new Byte[attachmentStream.Length];
                        attachmentStream.Read(attachmentContent, 0, (int)attachmentStream.Length);
                        attachmentStream.Close();
                        attachmentStream.Dispose();
                        attachmentCollection.Add(this.flprojectieadformat.FileName, attachmentContent);
                    }
                    itemToAdd["Title"] = TextBox1.Text;
                    itemToAdd.Update();

                }
            }
        }
            catch(Exception ex)
        {
            }


    }
  • What is the error you got in sandboxed solution? Show some code also – Atish Dipongkor - MVP Sep 30 '15 at 10:54
  • I got solution in farm ,but i didn't any solution for sandbox. – chakri Sep 30 '15 at 11:08
  • It should be the same for both. Write the same code and let me know – Atish Dipongkor - MVP Sep 30 '15 at 11:10
  • It is not showing any error and attachment not added – chakri Sep 30 '15 at 11:51
  • Then show your code. – Atish Dipongkor - MVP Sep 30 '15 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.