0

I am having a bit of a weird issue. I have created a Visual Webpart for SP2010 using C# and VS2010. I finally got all the bugs worked out and it is working properly. If someone wants to attach something during the list item creation then I first create the list item then read the List Item ID and attach the item. Everything works great the List Item is created and the document attached. I can then open that attachment just fine.

I want the webpart to be displayed when a user clicks the "Add New Item" link. Easy enough I went to the "Form Web Parts" in the "Customize List" section of the List tools ribbon. Opened the Default Display Form, minimized the default sharepoint form (I did not want to delete it as of yet) and added my Custom webpart in.

Cool now my webpart shows up when a user hits the add new item link. They fill out the form, add an attachment and hits Submit. List entry made complete with attachment but if I try and open that attachment I am getting an error message as shown Error from Excel document![][1]

Similar errors occur regardless of the file type.

I am using a FileUpload Asp control to browse for the file and here is the code for attaching.

Stream fs = FileUpload1.PostedFile.InputStream;
string name = Path.GetFileName(FileUpload1.PostedFile.FileName);
byte[] contents = new byte[fs.Length];
fs.Read(contents, 0, (int)fs.Length);
fs.Close();
try
{
    string addAttach = safeList.AddAttachment(safeListName, id, name, contents);
}

I searched around but did not have much luck finding an example that was specific like this. Any suggestions would be appreciated.

More of the code sorry the limit on the comment box was preventing a proper layout

safety.Lists list = new safety.Lists();
            const string listURL = "http://mysite/_vti_bin/lists.asmx";

            const string safeListName = "{guid}";
            //const string safeListView = "{guid}";
            safeList.Credentials = new NetworkCredential(dUser, pwd, dom);
            safeList.Url = listURL;
            if (lstRequired.SelectedItem.ToString() == "Yes")
            {
                forms = "1";
            }
            XmlDocument doc = new XmlDocument();
            XmlElement batch = doc.CreateElement("Batch");
            batch.SetAttribute("OnError", "Continue");
            batch.SetAttribute("ListVersion", "1");
            batch.InnerXml = "<Method ID='1' Cmd='New'>" +
                "<Field Name='Title'>" + txtName.Text + "</Field>" +
                "<Field Name='Job_x0020_Name'>" + txtNumber.Text + "</Field>" +
                "<Field Name='Document_x0020_Type'>" + LstType.SelectedItem.ToString() + "</Field>" +
                "<Field Name='Did_x0020_you_x0020_attach_x0020'>" + forms + "</Field>" +
                "<Field Name='Contract_x0020__x0023_'>" + txtContract.Text + "</Field>" +
                "<Field Name='Due_x0020_Date'>" + txtDue.Text + "</Field>" +
                "<Field Name='SSHO_x0020_Person'>" + lstSSHO.SelectedItem.ToString() + "</Field>" +
                "<Field Name='Any_x0020_additional_x0020_Plans'>" + txtAdd.Text + "</Field>" +
                "<Field Name='Subcontractors_x0020_Names_x0020'>" + txtSubContract.Text + "</Field>" +
                "<Field Name='Notes'>" + txtNotes.Text + "</Field>" +
                "<Field Name='Status'>" + lstStatus.SelectedItem.ToString() + "</Field>" +
                "<Field Name='Safety_x0020_Person_x0020_Respon'>" + lstAssigned.SelectedValue.ToString() + "</Field>" +
                "</Method>";

            string srcURL = FileUpload1.PostedFile.FileName;

            if (srcURL == "")
            {
                 XmlNode info = safeList.UpdateListItems(safeListName, batch);
                 showDialog("Request successfully submitted!");
            }
            else if (srcURL != "")
            {
                string id = "";
                XmlDocument xml = new XmlDocument();
                XmlNode info = safeList.UpdateListItems(safeListName, batch);
                xml.LoadXml(info.InnerXml);
                XmlNodeList ids = xml.GetElementsByTagName("z:row");
                foreach (XmlElement value in ids)
                {
                    id = value.GetAttribute("ows_ID");
                }

                //txtSubContract.Text = info.InnerXml;
                //txtAdd.Text = id;
                //File.WriteAllText("C:\\Temp\\safety.xml", info.InnerXml);
                Stream fs = FileUpload1.PostedFile.InputStream;
                string name = Path.GetFileName(FileUpload1.PostedFile.FileName);
                byte[] contents = new byte[fs.Length];
                fs.Read(contents, 0, (int)fs.Length);
                fs.Close();
                try
                {
                    string addAttach = safeList.AddAttachment(safeListName, id, name, contents);
                }

                catch (System.Web.Services.Protocols.SoapException ex)
                {

                }

            }

            if (lstRequired.SelectedValue.ToString() == "No")
            {
                showDialog("Please attach all required documents as soon as possible in order to process your request in a timely manner\\n\\n\\nRequest successfully submitted!");
            }
            else
            {
                showDialog("Request successfully submitted!");

this is everything that is effecting the add attachment (well actually quite a bit that doesn't affect it)

  • I will add that the attachment functionality still works perfectly in my form that is not using the add new item link so I would suspect some backend SharePoint code having to do with that is causing this – CarlSeiber Jan 22 '15 at 21:22
  • THank you Amal, let me Try this code and see what happens – CarlSeiber Jan 22 '15 at 21:48
  • Uh oh I may be missing a .dll to use this. Intellisense is not giving me an option for GetItemById – CarlSeiber Jan 22 '15 at 21:51
  • What is the type of safeList? I am assuming it is of SPList type. msdn.microsoft.com/en-us/library/… – Amal Hashim Jan 22 '15 at 21:51
  • test.Lists safeList = new test.Lists(); – CarlSeiber Jan 22 '15 at 21:55

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.