locked
Update Created By and Modified By field in a document library using Client Object Model RRS feed

  • Question

  • Hi

    I tried to update the Created By (Author) and Modified By (Editor) fields of a document in a document library using Client Object model.  but i couldnt as they are read only. How to achieve this.

    • Moved by Clayton Cobb Tuesday, February 8, 2011 5:08 PM Programming question (From:SharePoint 2010 - Using SharePoint Designer, Infopath, and other customization)
    Tuesday, February 8, 2011 5:03 PM

Answers

  • Here you are.

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using Microsoft.SharePoint.Client;
    
    namespace ClientObjectModelConsoleApplication
    {
      class Program
      {
        static void Main(string[] args)
        {
          ClientContext clientContext = new ClientContext("http://hercules");
          clientContext.Credentials = new NetworkCredential("Administrator", "mypassword");      
          List list = clientContext.Web.Lists.GetByTitle("Shared Documents");
          clientContext.Load(list);      
          clientContext.ExecuteQuery();      
          CamlQuery camlQuery = new CamlQuery();      
          camlQuery.ViewXml = "<View/>";
          ListItemCollection listItems = list.GetItems(camlQuery);
          clientContext.Load(listItems, items => items.Include(item => item.DisplayName, item => item["Author"], item => item["Editor"],
            item => item["Created"], item => item["Modified"]));
          clientContext.ExecuteQuery();            
          
          FieldUserValue user = FieldUserValue.FromUser("HERCULES\\dmitry.kaloshin");
    
          foreach (ListItem listItem in listItems)
          {
            listItem["Editor"] = user;
            listItem["Author"] = user;
            listItem["Created"] = DateTime.Now.AddYears(-1);
            listItem["Modified"] = DateTime.Now;
            listItem.Update();
          }
          clientContext.ExecuteQuery();
        }
      }
    }
    

     

    Dmitry

     


    Lightning Tools Check out our SharePoint tools and web parts

     


    • Marked as answer by RK Senthil Wednesday, February 9, 2011 1:46 PM
    Wednesday, February 9, 2011 11:56 AM

All replies

  • Hi,

    Please try this console application:

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint;
    
    namespace TestProject
    {
      class Program
      {
        static void Main(string[] args)
        {
          using (SPSite oSiteCollection = new SPSite("http://hercules/"))
          {        
            using (SPWeb oWebsiteRoot = oSiteCollection.OpenWeb())
            {
              SPList oList = oWebsiteRoot.Lists["Shared Documents"];
              
              foreach (SPListItem item in oList.Items)
              {            
                item[SPBuiltInFieldId.Author] = oWebsiteRoot.EnsureUser("HERCULES\\dmitry.kaloshin").ID;
                item[SPBuiltInFieldId.Editor] = oWebsiteRoot.EnsureUser("HERCULES\\dmitry.kaloshin").ID;
                item[SPBuiltInFieldId.Created] = DateTime.Now.AddYears(-1);
                item[SPBuiltInFieldId.Modified] = DateTime.Now;
                item.UpdateOverwriteVersion();
              }          
            }
          }
        }
      }
    }
    

     

    Dmitry

     


    Lightning Tools Check out our SharePoint tools and web parts

     


    Tuesday, February 8, 2011 9:26 PM
  • In SP2007, those fields required an SPFieldUserValue and not a string. So please check what client object model expects.
    Tuesday, February 8, 2011 10:29 PM
  • Hi diffident,

    But we are discussing SharePoint 2010 in this thread, aren't we?

     

    Dmitry

     


    Lightning Tools Check out our SharePoint tools and web parts

     


    Tuesday, February 8, 2011 10:47 PM
  • Yes, that's why I said that he may have to check something similar for client object model which is 2010.
    Tuesday, February 8, 2011 10:49 PM
  • Thanks diffident for clarification.

     

    Dmitry

     


    Lightning Tools Check out our SharePoint tools and web parts

     


    Tuesday, February 8, 2011 10:53 PM
  • Thanks for your reply,

    But i have to do this using client object model.....how to do the same..

    Wednesday, February 9, 2011 8:54 AM
  • Here you are.

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using Microsoft.SharePoint.Client;
    
    namespace ClientObjectModelConsoleApplication
    {
      class Program
      {
        static void Main(string[] args)
        {
          ClientContext clientContext = new ClientContext("http://hercules");
          clientContext.Credentials = new NetworkCredential("Administrator", "mypassword");      
          List list = clientContext.Web.Lists.GetByTitle("Shared Documents");
          clientContext.Load(list);      
          clientContext.ExecuteQuery();      
          CamlQuery camlQuery = new CamlQuery();      
          camlQuery.ViewXml = "<View/>";
          ListItemCollection listItems = list.GetItems(camlQuery);
          clientContext.Load(listItems, items => items.Include(item => item.DisplayName, item => item["Author"], item => item["Editor"],
            item => item["Created"], item => item["Modified"]));
          clientContext.ExecuteQuery();            
          
          FieldUserValue user = FieldUserValue.FromUser("HERCULES\\dmitry.kaloshin");
    
          foreach (ListItem listItem in listItems)
          {
            listItem["Editor"] = user;
            listItem["Author"] = user;
            listItem["Created"] = DateTime.Now.AddYears(-1);
            listItem["Modified"] = DateTime.Now;
            listItem.Update();
          }
          clientContext.ExecuteQuery();
        }
      }
    }
    

     

    Dmitry

     


    Lightning Tools Check out our SharePoint tools and web parts

     


    • Marked as answer by RK Senthil Wednesday, February 9, 2011 1:46 PM
    Wednesday, February 9, 2011 11:56 AM
  • Thanks for your quick help

    Wednesday, February 9, 2011 1:46 PM
  • when i am trying to do it and it updates my Title and Abstract fields and all other fields

    Author, Editor and both dates are just being totally ignored - no errors but no update either

    any suggestions on what can go wrong here?

    • Edited by Olga.D Friday, February 11, 2011 9:56 PM
    Friday, February 11, 2011 4:31 PM
  • Hi Olga,

    Do you have this issue when running the console application I posted above?

     

    Dmitry

     


    Lightning Tools Check out our SharePoint tools and web parts

     


    Friday, February 11, 2011 9:51 PM