Blog

Assign pages a new Created By User

by Joel Dahlin on January 24, 2018

Assign pages a new Created By User

We recently had a client delete a bunch of users (former employees) from their site. In doing so many of their blog posts lost the Author. The blog post transformation was using the out of the box transformation that displayed the Author based on the DocumentCreatedByUserID, so not only did it not show the Author but it was actually throwing an exception.

To remedy this we created a new User that could be used to replace all of the removed users, a more generic company name user. We then wrote a quick script to run through all the blog posts that didn't have a Created By User ID and assign the blog post this new Created By User ID.

First after creating the new user we ran a quick query to get the new UserID.

SELECT * FROM CMS_User 

Then we just created a quick new aspx page that we could go to and click a run button on.

        var docs = DocumentHelper.GetDocuments().Types("CMS.BlogPost").Where("DocumentCreatedByUserID IS NULL").WithCoupledColumns();
        StringBuilder sb = new StringBuilder();
        foreach (var doc in docs)
        {            
            sb.AppendLine(doc.ClassName + " " + doc.DocumentName + "--");
            doc.CheckOut();
            doc.SetValue("NodeOwner", 109);
            doc.SetValue("DocumentCreatedByUserID", 109);
            doc.Update();
            doc.CheckIn();
            doc.Publish();
            sb.AppendLine(doc.DocumentName + " Updated
"); } litResults.Text = sb.ToString(); }

Notice we are calling all blog posts WithCoupledColumns() to make sure no data is lost. We also make sure to checkout/checkin/publish the doc for versioning.

You may want to take a backup of your db before you run this just in case you have something set wrong in the script.

#bug #tip

Comments
Blog post currently doesn't have any comments.
 Security code