Programmatically Create Users and Content Blocks

Posted by Community Admin on 03-Aug-2018 19:39

Programmatically Create Users and Content Blocks

All Replies

Posted by Community Admin on 07-May-2011 00:00

Hello,

Would it be possible to programmatically create a new user on the fly, and assign it a custom role from "front-end" code? It seems like the process would need special permission to accomplish that.

Also, we would like to programmatically create the equivalent of 3.x Generic Content items from the front-end. We did this in 3.x, but were not able to programmatically publish the content - just create it.

Thanks for any insight!

Regards,
John G

Posted by Community Admin on 09-May-2011 00:00

Hello John,

You can try and use the following sample code to see how the process of creating users programatically works:

var userManager = UserManager.GetManager("Default");
System.Web.Security.MembershipCreateStatus status;
userManager.Provider.SuppressSecurityChecks = true;
var user = userManager.CreateUser("user1", "user1234@", "user1@test.com", "Question", "Answer", true, null, out status);
user.FirstName = "FirstName";
user.LastName = "LastName";
userManager.SaveChanges();
   
RoleManager roleManager = RoleManager.GetManager("AppRoles");
roleManager.Provider.SuppressSecurityChecks = true;
var role = roleManager.GetRole("Administrators");
roleManager.AddUserToRole(user, role);
roleManager.SaveChanges();

If you want to create generic content please take a look at this article from our documentation which includes code samples and explanations on how to deal with Generic content programatically.

Greetings,
Victor Velev
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 10-May-2011 00:00

Thank you, Victor!

Now, how do I create a category (and/or tag), and then assign it to the new content item that I create using this approach?

I see a "CategoryText" property of the ContentItem object, but I don't see any way to create nest categories like this.

The instructions for managing content do not cover how to programmatically publish content. What we would like to do is this:

1) From the FRONT end (public site), have code that can create a generic content item
2) Create the content either anonymously, or possibly by impersonating a user that is created just for this purpose
2) Add content and a category to the newly created item
3) Save the item, and then publish it

We cannot manage to get the item published. It is created and saved as Draft only. We imagine there is some security aspect here that is preventing us from publishing the item.

Thank you for any help.

Regards,
John G.

Posted by Community Admin on 11-May-2011 00:00

Hello John,

Please see attached file which you can use to see how generic content is being created and published. 

Regards,
Victor Velev
the Telerik team

Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 11-May-2011 00:00

Hello Victor,

Thanks for the example, though it does not appear to be working for us either.

Here is the code we are using - the commented-out bit is our initial code (per the initial link you provided), and we are currently implementing based on your provided example:

01.// Get the category to asssign. If it's not found, create it
02.string categoryName = "Test App Content";
03. 
04.//ContentItem newContent = contentManager.CreateContent();
05. 
06.//newContent.Name = contentName;
07.//newContent.Title = contentName;
08.//newContent.Content = defaultText;
09.//newContent.Description = description;
10.//newContent.CategoryText = categoryName;
11.//newContent.AllowComments = false;
12.//newContent.AllowTrackBacks = false;
13. 
14.//contentManager.RecompileItemUrls<ContentItem>(newContent);
15.//contentManager.Publish(newContent);
16. 
17.//contentManager.SaveChanges();
18. 
19.App.WorkWith().ContentItem()
20.    .CreateNew()
21.    .Do(ci =>
22.    
23.        ci.Name = contentName;
24.        ci.Title = contentName;
25.        ci.Content = defaultText;
26.        ci.Description = description;
27.        ci.CategoryText = categoryName;
28.        ci.AllowComments = false;
29.        ci.AllowTrackBacks = false;
30.        ci.UrlName = urlName;
31.    )
32.    .Publish()
33.    .SaveChanges();
34. 
35.SiteMapBase.Cache.Flush();
 When we run this code from the public site when no user is logged in, the content item is created but it will not publish (no error, just isn't published). Is there some way to create a generic security context and use it to publish the item. I noticed that the Generic Content item that is created does not display anything for "Owner" in the GC list on the Generic Content management page.

Also, your example does not show how to create / attach a category (or tag) to the new content item. How can we do this?

Thanks again!

Regards,
John G

Posted by Community Admin on 16-May-2011 00:00

Hello John,

Thank you for getting back to me. 

You can check our documentation which explains how taxonomies work and how you integrate them with other content items. Please follow this link.

  About the other issue: Let me clear the situation - you want to create a generic content item? If I understand you correctly and the code I sent you, you will be able to create and save such content when running the script without being logged in, but still you will need to associate a content block to display this content on your page. 

 
Greetings,
Victor Velev
the Telerik team

Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 16-May-2011 00:00

Hi Victor,

So, if I am clear, we need to use the Taxonomy APIs to manage Categories and Tags. Is that correct? I will check out the link you provided. Thank you!

Regarding the creation of Generic Content: we will be creating Generic Content items to handle "admin-able" content that will be displayed inside some of our custom controls (i.e. NOT using content blocks). 

The code that I posted (based on the example you provided) does successfully create and save a Generic Content item, but it does not successfully publish the item. When we go to view the item in the Generic Content admin screen, it's status appears as "Draft".

Any ideas?

Thank you!

Regards,
John

Posted by Community Admin on 18-May-2011 00:00

Hello John,

The taxonomies section in our documentation explains how you can use the tags and categories through the api.

About the generic content: you need to have a role in your website which has 'create' permissions. You may assign a such role for the public or specific type of users, but without create permission the pages would not be considered as published.

All the best,
Victor Velev
the Telerik team

Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

This thread is closed