How to upload document and update properties in one go using C# in SharePoint

Frank Martin 451 Reputation points
2020-07-28T07:45:18.457+00:00

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();
      }
   }
});
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,594 questions
{count} votes

Accepted answer
  1. Jerryzy 10,566 Reputation points
    2020-07-29T05:53:47.507+00:00

    Yesterday, I answered the same question in StackExchange:

    How to upload document and update properties in one go using C#

    Actually, this is by design.

    Using Update() method, Version1 is to create the uploaded file item and Version 2 is to update the metadata column.

    Using SystemUpdate() method, it won't update Editor field and generate the new version history, it will set the Editor field with Current Logged User.

    A workaround is to open the SPSite with a UserToken which is the same as the value which needs to be set with Editor like this:

    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
    SPUserToken token;
    using (SPSite site = new SPSite("http://sp/sites/dev/"))
    {
    token = site.RootWeb.EnsureUser(@"i:0#.w|contoso\jerry").UserToken;
    }
    using (SPSite site = new SPSite("http://sp/sites/dev/", token))
    {
    using (SPWeb web = site.OpenWeb())
    {
    Byte[] bytes = File.ReadAllBytes(@"C:\passwords.txt");
    SPFolder myLibrary = web.Folders["DocVersion"];
    web.AllowUnsafeUpdates = true;
    SPFile spfile = myLibrary.Files.Add("pwd.txt", bytes, true);
    spfile.ListItemAllFields["Title"] = "TestTitle";
    spfile.ListItemAllFields.SystemUpdate();
    web.AllowUnsafeUpdates = false;
    }
    }
    });

    Then it will only generate one version like this:

    14146-image-1.png

    This workaround needs to make sure the User in UserToken have enough permission for the site and library.

    Reference:

    Update vs. SystemUpdate for SharePoint List Item

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Justin Liu 51 Reputation points MVP
    2020-07-29T01:17:37.83+00:00

    FYI
    https://video2.skills-academy.com/en-us/previous-versions/office/sharepoint-server/ms479448(v=office.15)

    The SystemUpdate method has a bool param which you can set to false that will not affect the modified by.

    0 comments No comments