Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Nicola Nauwynck 11 posts 76 karma points
    Oct 11, 2014 @ 12:13
    Nicola Nauwynck
    1

    How to set the creator or writer of a content item in a controller action

    Hello,

    I have started using Umbraco one month ago and I learned so much thanks to Umbraco.tv and Umbraco courses.

    In my project I created a custom dashboard. This dashboard makes it possible for writers to write a message about what they are going to write.

    When creating a content item I would like to save who made the content item. Currently I do this by saving the userId in a custom property because WriterId and CreatorId don't seem to work.

            [System.Web.Http.HttpPost]
        public HttpResponseMessage CreateMessage(Message message)
        {
            var contentService = Services.ContentService;
    
            int userId = UmbracoUser.Id;
            var newMessage = Services.ContentService.CreateContent(message.Content, 1117, "message");
            newMessage.SetValue("content", message.Content);
            newMessage.SetValue("userId", userId);
            //newMessage.WriterId = userId;         does not work,always saves a 0
            //newMessage.CreatorId = userId;        does not work,always saves a 0
            contentService.Save(newMessage);
            contentService.PublishWithStatus(newMessage);
    
            return new HttpResponseMessage(System.Net.HttpStatusCode.OK);
        }
    

    Any suggestions on how I could set the creator of a content item without using a custom property?

    Regards, Nicola Nauwynck

  • Richard Soeteman 4046 posts 12899 karma points MVP 2x
    Oct 12, 2014 @ 08:21
    Richard Soeteman
    0

    Hi Nicola,

    It's not possible, same for create and update date. Umbraco will always set the values.

    Best,

    Richard

  • Nicola Nauwynck 11 posts 76 karma points
    Oct 12, 2014 @ 09:19
    Nicola Nauwynck
    0

    Hmmm

    The next and final step for setting up Umbraco as my CMS for my magazine website is importing all the articles from my custom made CMS.

    So creating a content item from a controller will always set the creator to user with id 0 even when logged in with a different user and the creation date to the current date.

    So that means I will have to make custom properties for userId and creationTime and make a custom dashboard so content writers can edit posts that they made in the old CMS.

  • Sander Houttekier 114 posts 163 karma points
    Oct 12, 2014 @ 13:11
    Sander Houttekier
    0

    for creation date and update date i assume this is the wanted behavior. but for the writerId and creatorId it isnt.

    if you are logged in as user 10 it should save 10, not 0.

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Oct 12, 2014 @ 21:17
    Dan Diplo
    0

    You should be able to set the creator Id by using the overload of CreateContent that takes a User Id as the last parameter:

    var newMessage = Services.ContentService.CreateContent("name", 1117, "message", userId);
  • Nicola Nauwynck 11 posts 76 karma points
    Oct 12, 2014 @ 21:59
    Nicola Nauwynck
    0

    After saving the content the writerId is set to 0. The writerId was set to 1 with the overload method but it changed to 0 after the save.

    writerId

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Oct 12, 2014 @ 22:08
    Dan Diplo
    101

    The Save and Publish methods also have overloads where you can set the UserId, so try setting those:

     contentService.Save(newMessage, userId);
     contentService.PublishWithStatus(newMessage, userId);
  • Biagio Paruolo 1618 posts 1910 karma points c-trib
    Sep 09, 2016 @ 18:35
    Biagio Paruolo
    0

    Good. With these

    ApplicationContext.Current.Services.UserService.GetProfileById(item.WriterId).Name
    

    you can retreive the name of the backoffice user.

  • Nicola Nauwynck 11 posts 76 karma points
    Oct 12, 2014 @ 22:19
    Nicola Nauwynck
    0

    Haha that did the trick :).

    Should have explored those functions before asking :p.

    Seems like it is also possible to change the createDate if it is set between saving the content and publishing it. If you set the value before the save it won't work.

    Haven't found a way yet to make the updateDate save but that is a known issue (http://issues.umbraco.org/issue/U4-4800) :)

  • Richard Soeteman 4046 posts 12899 karma points MVP 2x
    Oct 13, 2014 @ 08:54
    Richard Soeteman
    0

    Ah of course true you can set the user... It's not a property

  • Nitin Anand 46 posts 178 karma points
    Dec 08, 2020 @ 08:36
    Nitin Anand
    0

    while copying content, the creatorId is set to 0. I tried but none of these funtions helps contentService.Save(newMessage, userId); contentService.PublishWithStatus(newMessage, userId);

    Any idea if its possible to change value of creatorId while copying?

Please Sign in or register to post replies

Write your reply to:

Draft