set blog post owner

Lorenz Lammersdorf asked on September 6, 2017 14:03

Hi all,

I have to import an old Wordpress blog into Kentico. This works fine using the API, but I need to set the NodeOwner to the Wordpress author (the users exist in Kentico). We're talking about several hundreds of blog posts, so this is not a thing you want to do manually :) I'm quite sure this is possible, but I can't figure out how to do this.

Cheers Lorenz

Correct Answer

Brenden Kehren answered on September 6, 2017 16:59

Ok I was assuming you had one owner. Since that isn't an option, update your code to use the following line:

post.SetValue("NodeOwner", user.UserID);

Using the above over using this

post.NodeOwner = user.UserID;

will allow you to bypass the "read only" option of the strongly typed property and set the value. If this doesn't work when you insert the page, do an update immediately after you insert it and it should work without issue.

1 votesVote for this answer Unmark Correct answer

Recent Answers


Chetan Sharma answered on September 6, 2017 14:28

Few questions 1. How are you importing the pages? 2. Or pages already in Kentico and you just would like to update the Node owner property? 3. Do you have access to Kentico import toolkit? It's a tool installed on where you've installed Kentico.

Solution is staright forward. I would suggest you based on the approach you are taking.

0 votesVote for this answer Mark as a Correct answer

Lorenz Lammersdorf answered on September 6, 2017 14:36

  1. we import the pages from the WP database using the Kentico API as follows:

                            // set the user
                            UserInfo user = UserInfoProvider.GetUserInfo("lorenz");
    
                            // create a new blog post
                            TreeNode post = TreeNode.New("CMS.BlogPost", tree);
    
                            // Sets the page properties and blog post field values
                            post.DocumentName = result.GetString(0);
                            post.SetValue("BlogPostTitle", result.GetString(0));
                            post.SetValue("BlogPostDate", result.GetString(1));
                            post.SetValue("BlogPostSummary", result.GetString(4));
                            post.SetValue("BlogPostBody", post_body);
                            post.SetValue("BlogPostAllowComments", "false");
                            post.NodeOwner = user.UserID; // fails, does nothing
                            post.DocumentCulture = "en-us";
    
  2. no, I try to set the owner only when creating the page

  3. yes, but I have no idea how it works.
0 votesVote for this answer Mark as a Correct answer

Chetan Sharma answered on September 6, 2017 15:12

I believe you've already checked that user.UserID is not returning an empty string.

0 votesVote for this answer Mark as a Correct answer

Lorenz Lammersdorf answered on September 6, 2017 15:14 (last edited on September 6, 2017 15:14)

yes, of course. In the example it returns 67, which is the correct ID.

0 votesVote for this answer Mark as a Correct answer

Chetan Sharma answered on September 6, 2017 15:27

If you use intellisense for NodeOwner Property yoy can see the property coming as read-only. I don't see this for other System Property like DocumentName.

It's a read only property - Link

Can you login into kentico Admin from the account which you want to make as owner and run API and leave the field blank. because Kentico assign's the current user as NodeOwner

Try this and let me know

Thanks, Chetan

1 votesVote for this answer Mark as a Correct answer

Suneel Jhangiani answered on September 6, 2017 16:04

You could try using the AuthenticationHelper.ImpersonateUser(UserInfo ui) method in the CMS.Membership assembly.

ie.

                    // set the user using impersonation
                    UserInfo user = UserInfoProvider.GetUserInfo("lorenz");
                    AuthenticationHelper.ImpersonateUser(user);

                    // create a new blog post
                    TreeNode post = TreeNode.New("CMS.BlogPost", tree);

                    // Sets the page properties and blog post field values
                    post.DocumentName = result.GetString(0);
                    post.SetValue("BlogPostTitle", result.GetString(0));
                    post.SetValue("BlogPostDate", result.GetString(1));
                    post.SetValue("BlogPostSummary", result.GetString(4));
                    post.SetValue("BlogPostBody", post_body);
                    post.SetValue("BlogPostAllowComments", "false");
                    post.DocumentCulture = "en-us";

                    // Cancel impersonation 
                    AuthenticationHelper.CancelImpersonation();
0 votesVote for this answer Mark as a Correct answer

Lorenz Lammersdorf answered on September 6, 2017 16:27

Chetan, how do I run a script from the Admin? Sorry, may be a dumb question :)

Suneel, I tried, but it throws an error:

UserInfo user = UserInfoProvider.GetUserInfo("lorenz");
AuthenticationHelper.ImpersonateUser(user);
...

-> Object reference not set to an instance of an object.

0 votesVote for this answer Mark as a Correct answer

Chetan Sharma answered on September 6, 2017 16:35

Hi Lorenz,

What I meant was set this user to admin from the users module for a time being.

Now log in with this user and run your code so that it is the Authenticated user that your .NET code will get.

Comment the code to set the Nodeowner as Kentico will set it automatically.

I hope this helps.

Chetan

0 votesVote for this answer Mark as a Correct answer

Lorenz Lammersdorf answered on September 6, 2017 16:40

hi chetan,

kentico sets it automatically to "Public Anonymous User (public)", no matter who's logged in.

cheers, lorenz

0 votesVote for this answer Mark as a Correct answer

Chetan Sharma answered on September 6, 2017 16:45

Depends where you run it from. If you create an aspx page inside a Kentico and bring all your logic in that and then run that code on aspx page load or init method, the user you will get will be the current user who is logged in.

I have done this sometime back as far as I remember.

How are you executing your code?

0 votesVote for this answer Mark as a Correct answer

Suneel Jhangiani answered on September 6, 2017 16:45

Ok, I think that because you are using the API from the comand line and the MembershipContext.AuthenticatedUser is probably not set or Kentico can't write a cookie for setting up impersonation.

Perhaps a quick fix to this would be to put the code into webpart and add it to a secured page for you to run once?

Alternatively if you know the users password then you should be able to Authenticate as that user:

// set the user using authentication
UserInfo user = AuthenticationHelper.AuthenticateUser("lorenz", "password", SiteContext.CurrentSiteName);
0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on September 6, 2017 16:46 (last edited on September 6, 2017 16:47)

If you have all of your articles already imported you can do a few things:

  1. If you have workflow enabled, disable it, even if it is the basic versioning workflow.
  2. Use the SQL statement below to set the node owner. Yes it's updating the db directly but this won't hurt the setup unless the user id is not valid.
  3. Restart the website
  4. View the page and see if the node owner updated.

update cms_treenode set NodeOwner = 67 WHERE NodeAliasPath LIKE '/YourBlogPath/%' AND ClassName = 'your.classname'

1 votesVote for this answer Mark as a Correct answer

Lorenz Lammersdorf answered on September 6, 2017 16:51

chetan & suneel: yes, i run this from the command line. i'm on my way home, i'll try your suggestions tomorrow. thanks so far for your great help and patience!

brenden, this does not work in my scenario, at least not in that easy way. there are about 20 different authors and i have no matching rule to determine which article is written by which author.

0 votesVote for this answer Mark as a Correct answer

Chetan Sharma answered on September 6, 2017 17:12

Thanks Brendan, I thought this could be an option but since I saw read-only I thought otherwise.

These are the things you know by experience and you have a lot of that ;)

0 votesVote for this answer Mark as a Correct answer

Lorenz Lammersdorf answered on September 7, 2017 08:33

Godd morning,

thanks Brenden, doing an update did the trick.

Have a nice day! Lorenz

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.