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