I am using following code to upload a file in library and setting column values. This library has Major versioning enabled. Problem I am facing is on each file upload, it creates two versions, first version is for file upload and second version is for updating column values. Can we do it in one go i.e. upload and setting column values only create one version?
I tried spfile.ListItemAllFields.SystemUpdate(); instead of spfile.ListItemAllFields.Update(); but then it didn't update the Editor field and saved it as System Account
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(siteURL))
{
using (SPWeb web = site.OpenWeb(mySubSite))
{
Byte[] bytes = Convert.FromBase64String(Attachment);
SPFolder myLibrary = web.Folders[myLibrary];
web.AllowUnsafeUpdates = true;
SPFile spfile = myLibrary.Files.Add(FileName, bytes, true);
spfile.ListItemAllFields["Editor"] = web.EnsureUser(UploadedBy).ID;
spfile.ListItemAllFields["Product"] = Product;
spfile.ListItemAllFields.Update();
web.AllowUnsafeUpdates = false;
myLibrary.Update();
}
}
});