error uploading document

Hi all,

iam using below code to update the doc lib item data withattachment.But document is not updating.Iam using file upload control in my webpart.

protected void btnUpdate_Click(object sender, EventArgs e)
        {

            string DocumentID = Page.Request.QueryString["DocID"].ToString();
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite oSite = new SPSite(SPContext.Current.Site.Url))
                {
                    using (SPWeb oWeb = oSite.OpenWeb())
                    {

                        SPDocumentLibrary mylist = oWeb.Lists["UploadDocument"] as SPDocumentLibrary;
                        SPQuery query = new SPQuery();
                        query.Query = "<Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + DocumentID + "</Value></Eq></Where>";
                        oWeb.AllowUnsafeUpdates = true;
                        SPListItemCollection ilistItems = mylist.GetItems(query);
                        if (ilistItems.Count > 0)
                        {
                            foreach (SPListItem oSPListItem in ilistItems)
                            {

                               //facing issue below code
                                string fileName = System.IO.Path.GetFileName(fileAttachment.PostedFile.FileName);
                                string fileExtension = fileAttachment.FileName.Substring(fileAttachment.FileName.IndexOf("."));
                                byte[] fileBytes = fileAttachment.FileBytes;
                                string athr = string.Empty;
                                string destUrl = mylist.RootFolder.Url + "/" + fileName;
                                SPFile destFile = mylist.RootFolder.Files.Add(destUrl, fileBytes, true);
                                oSPListItem["Name"] = fileName;





                                  // below code is working fine
                                oSPListItem["abc"] = drpSite.SelectedItem.Text;
                                oSPListItem["xyz"] = drpSubsite.SelectedItem.Text;
                               oSPListItem["ass"] = txtNumericInput.Text;
                                //oSPListItem["tech"] = txtTech.Text;

                                SPFieldUserValueCollection values = new SPFieldUserValueCollection();
                                if (PeopleEditor3.ResolvedEntities.Count > 0)
                                {
                                    for (int i = 0; i < PeopleEditor3.ResolvedEntities.Count; i++)
                                    {
                                        PickerEntity user = (PickerEntity)PeopleEditor3.ResolvedEntities[i];

                                        SPUser webUser = oWeb.EnsureUser(user.Key);
                                        SPFieldUserValue userValue = new SPFieldUserValue(oWeb, webUser.ID, webUser.Name);


                                        SPUser myUser = userValue.User;
                                        string myUser1 = myUser.Name;


                                        oSPListItem.Fields["User"].ReadOnlyField = false;

                                        oSPListItem["User"] = myUser1;

                                    }
                                }


                                oSPListItem["accd"] = txtBU.Text;

                                oSPListItem.Update();
                            }
                        }



                        oWeb.AllowUnsafeUpdates = false;

                    }
                }
            });




        }



below error is coming

The specified name is already in use.

The document or folder name was not changed.  To change the name to a different value, close this dialog and edit the properties of the document or folder.please help.

regards,

praveen

                                                                                                                      
July 30th, 2015 12:50pm

Seems to me that two threads are getting mixed.

First just play with SPFile and update it then with the help of Same list ID, update list items.

Refer: http://platinumdogs.me/2010/03/29/sharepoint-updating-office-based-document-library-items-automatic-document-property-promotion/

Free Windows Admin Tool Kit Click here and download it now
July 30th, 2015 1:58pm

Hi rupesh,

Thanks for u r reply.

I tried below code but not working.

string fileName = System.IO.Path.GetFileName(fileAttachment.PostedFile.FileName);
                                string fileExtension = fileAttachment.FileName.Substring(fileAttachment.FileName.IndexOf("."));
                                byte[] fileBytes = fileAttachment.FileBytes;
                                string athr = string.Empty;
                                string destUrl = mylist.RootFolder.Url + "/" + fileName;
                                SPFile destFile = mylist.RootFolder.Files.Add(destUrl, fileBytes, true);
                                destFile.SaveBinary(fileBytes);

Regards,

Praveen

July 31st, 2015 2:05am

Try this,

public void UploadDocument( string siteTitle, string libraryName, string fileName )
{
  var web = GetWebByTitle(siteTitle);
  var fInfo = new FileInfo(fileName);
  var targetLocation = string.Format("{0}/{1}/{2}", web.ServerRelativeUrl, 
     libraryName, fInfo.Name);
  
  using (var fs = new FileStream(fileName, FileMode.Open))
  {
    File.SaveBinaryDirect(clientContext, targetLocation, fs, true);
  }
}

Usage,

var result = sh.UploadDocument("MySite", "myDocumentLibrary",
                              @"c:\temp\sample.png");

Free Windows Admin Tool Kit Click here and download it now
July 31st, 2015 2:59pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics