locked
how to update "Author"-Created By or "Editor" field programmatically

    Question

  •  Hi,

    I'm adding item into my list from a .aspx page outside the sharepoint. What I want to do is while adding the item, item["Author"] should be the one I choose not Administrator. But in my case, by default Administrator becomes Author and Editor of the list item added from outside.

    Is it possible to edit these fields?

    thanks..

    Friday, December 05, 2008 9:23 AM

Answers

  • One way to achieve this is to create the SPWeb object with the required user context. Get the list and update the list using the SPWeb object which is created under the given user context.
    Following example shows you how to do this:
    //Assuming you already have SPUser object of the required user 
     
    using (SPSite site = new SPSite("http://server",user.UserToken)) 
     using (SPWeb web = site.OpenWeb()) 
     { 
       SPList list = web.Lists["listName"]; 
       SPListItem item = list.Items.Add(); 
       item["Title"] = "value"; 
       item.Update(); 
     } 


    -Sridhar http://sridharu.blogspot.com
    • Marked as answer by sivaslieko Saturday, December 06, 2008 9:22 AM
    Friday, December 05, 2008 12:15 PM
  • You can simply update these fields by using this code

                    using (SPWeb currentWeb = spSite.RootWeb)
                    {
                        SPList documentList = currentWeb.Lists["Shared Documents"];
                       
                        // The actual User Information is within this SPListItem
                        SPListItem userItem = documentList.Items.GetItemById(1);                  
                        userItem["Created By"] = currentWeb.EnsureUser("user").ID;
                        userItem["Modified By"] = userItem["Modified By"];
                        userItem["Created"] = DateTime.Now.AddYears(-3);
                        userItem["Modified"] = DateTime.Parse("01/01/2000");
                        userItem.UpdateOverwriteVersion();
                    }

    Angelo (www.ausy.lu)
    Tuesday, April 28, 2009 9:09 AM

All replies

  • One way to achieve this is to create the SPWeb object with the required user context. Get the list and update the list using the SPWeb object which is created under the given user context.
    Following example shows you how to do this:
    //Assuming you already have SPUser object of the required user 
     
    using (SPSite site = new SPSite("http://server",user.UserToken)) 
     using (SPWeb web = site.OpenWeb()) 
     { 
       SPList list = web.Lists["listName"]; 
       SPListItem item = list.Items.Add(); 
       item["Title"] = "value"; 
       item.Update(); 
     } 


    -Sridhar http://sridharu.blogspot.com
    • Marked as answer by sivaslieko Saturday, December 06, 2008 9:22 AM
    Friday, December 05, 2008 12:15 PM
  • perfect :))
    Saturday, December 06, 2008 9:23 AM
  • Can we achieve that with some web service calls?
    Friday, April 10, 2009 10:37 PM
  • You have to create a custom web service with the code described above...

    Serge Luca; blog: www.redwood.be
    Saturday, April 11, 2009 4:01 PM
  • The "Author" column does not map to the "Created By" column you see in the SharePoint UI. So if you are trying to modify the "Created By" column then your code should be item["Created By"]. This column cannot be modified by code. The ReadOnly property of the SPField is true. Even if you change the ReadOnly property to false before trying to update this column, the SharePoint runtime will still throw an error stating you are trying to modify a read only column. The same applies to  the "Modified By"  column. If you want to update meta data of a SharePoint item other than these columns using a web service, then you can use the Lists.asmx web service and the UpdateListItems method. It takes the list name and a XmlNode containing xml like below:

    For document libraries:


    "<Batch OnError='Continue'><Method ID='1' Cmd='Update'><Field Name='ID'/><Field Name='FileRef'>http://basesmcdev2/sites/tester1/tester2/A0805051340563171168.txt</Field><Field Name='Title'>new title</Field></Method></Batch>"

    ;

    For lists:

    "<Batch OnError='Continue'><Method ID='1' Cmd='Update'><Field Name='ID'>22</Field ><Field Name='Title'>new title</Field></Method></Batch>"

    ;

    http://msdn.microsoft.com/en-us/library/ms440289.aspx


    http://www.certdev.com
    Saturday, April 11, 2009 6:31 PM
  • Thanks, but write stuff in Java and have to use web services (Lists.asmx).. Seems like I am out of luck..
    Saturday, April 11, 2009 6:36 PM
  • yes, but this won't work if you want the "created by" field to be any specific user...
    The only alternative I see is the code described above, wrapped in a custom web service.


    Serge Luca; blog: www.redwood.be
    Saturday, April 11, 2009 6:53 PM
  • Steve.Curran said...
    "So if you are trying to modify the "Created By" column then your code should be item["Created By"]. This column cannot be modified by code. The ReadOnly property of the SPField is true. Even if you change the ReadOnly property to false before trying to update this column, the SharePoint runtime will still throw an error stating you are trying to modify a read only column. The same applies to  the "Modified By"  column."

    Thats not entirely correct.  If you elevate permissions on the item, you can force an update of the Created By and Modified By fields.  Just be sure to use the internal names instead, Author and Editor, respectively.

    I know it works because I just had to do it for my project.
    Monday, April 20, 2009 4:47 PM
  • There is a simpler way to do this without needing the user token:

     

    SPList list = web.Lists["myList"];
    list.Fields[
    "Author"].ReadOnlyField = false;
    SPListItem item = list.Items.Add();
    item[
    "Author"] = "value";
    item.SystemUpdate(
    false);
    list.Fields["Author"].ReadOnlyField = true;


    Visit my blog http://jasear.wordpress.com
    Monday, April 20, 2009 4:56 PM
  • that's what Sridharu proposed a while ago...

    Serge Luca; blog: www.redwood.be
    Monday, April 20, 2009 4:56 PM
  • No, Sridharu proposed something fundamentally different. Please view his code.


    Visit my blog http://jasear.wordpress.com
    Monday, April 20, 2009 5:06 PM
  • What if you just want to update the item?

    Yeah I know changing the Created By field seems to be fundamentally flawed, but the client is attempting to use this field for a dual purpose.

    Monday, April 20, 2009 7:05 PM
  • I was working on a huge migration project where we needed to be able to update the author's of documents while it was being added to document libraries by the system account.

    The method i described above worked for us.


    Visit my blog http://jasear.wordpress.com
    Monday, April 20, 2009 10:33 PM
  • SOAP works the same way?
    Tuesday, April 21, 2009 3:31 AM
  • The "Modified By"/"Editor and "Created By"/"Author" field cannot be edited.  The only way to add an item as a specific user is to impersonate the user in code by opening the SPSite object under a specific user.  (This would allow you to not have to know the user's password)


    Akona Systems
    Wednesday, April 22, 2009 4:03 PM
  • That is not correct. It can be edited as described above.
    Visit my blog http://jasear.wordpress.com
    Wednesday, April 22, 2009 4:43 PM
  • I was reacting to tigertoy"s post, no offense ;-)
    Serge Luca; blog: www.redwood.be
    Wednesday, April 22, 2009 5:17 PM
  • There is a simpler way to do this without needing the user token:

     

    SPList list = web.Lists["myList"];
    list.Fields[
    "Author"].ReadOnlyField = false;
    SPListItem item = list.Items.Add();
    item[
    "Author"] = "value";
    item.SystemUpdate(
    false);
    list.Fields["Author"].ReadOnlyField = true;


    Visit my blog http://jasear.wordpress.com

    dear jasear:

    i tried your code to updating item["Author"]
    but it wasn't work.. :-(
    I wrote a console application.
    the value of item["Author"] after item.Update(),become admin's account.

    does anyone has suggesstion??
    Tuesday, April 28, 2009 3:14 AM
  • You can simply update these fields by using this code

                    using (SPWeb currentWeb = spSite.RootWeb)
                    {
                        SPList documentList = currentWeb.Lists["Shared Documents"];
                       
                        // The actual User Information is within this SPListItem
                        SPListItem userItem = documentList.Items.GetItemById(1);                  
                        userItem["Created By"] = currentWeb.EnsureUser("user").ID;
                        userItem["Modified By"] = userItem["Modified By"];
                        userItem["Created"] = DateTime.Now.AddYears(-3);
                        userItem["Modified"] = DateTime.Parse("01/01/2000");
                        userItem.UpdateOverwriteVersion();
                    }

    Angelo (www.ausy.lu)
    Tuesday, April 28, 2009 9:09 AM