+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Add tool to admin programatically to allow automatic creation of user, role, page and blog...

Add tool to admin programatically to allow automatic creation of user, role, page and blog...

13 posts, 0 answered
  1. Ian Johnson
    Ian Johnson avatar
    11 posts
    Registered:
    11 Jan 2006
    14 Jan 2009
    Link to this post
    Hi,

    I am looking at using the sitefinity CMS to make a system that will allow a website to have a members area that has a specific blog for each user (ie. client) that is only accessible by that user (client). This way the site owner could create updates for each of the clients to their specific blog to give them updates relevant to them.

    Looking through the sitefinity admin, the only way I can see to do this is..
    1. Create a new role eg. client1_role
    2. Create new user eg client1
    3. Apply client1 to client1_role
    4. Create new page eg. client1_page
    5. Drop blog posts control onto client1_page
    6. Create new blog eg. client1_blog
    7. Apply client1_blog to client1_page
    8. Apply user role client1_role to allow - view on client1_page
    9. Deny anonymous access on client1_page

    My questions are:
    1. Can a tool that would simplify this drawn out process by included in the admin system options somewhere programatically- if so, where is an example of adding a tool to the admin system.
    2.Are there any other examples of performing any of the required actions above to save me development time.

    Many thanks in advance!

    Regards
    Ian

  2. Ian Johnson
    Ian Johnson avatar
    11 posts
    Registered:
    11 Jan 2006
    14 Jan 2009
    Link to this post
    Excuse my newbie questions.

    I just looked a little into it. I am trying to get the firstname from the field to create a role as a starting point.

    How do I reference the value of the firstname field through the control.. this doesn't seem to work....

     

    Control container = manageUsers.GetCurrentContainer();

     

     

    HtmlGenericControl FirstName = container.FindControl("FirstName") as HtmlGenericControl;

     

     

    UserManager.Default.CreateRole(FirstName.InnerText); 

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. (Firstname.InnerText)

     

  3. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    14 Jan 2009
    Link to this post
    Hello Ian Johnson,

    Give a try ti the snipped below

     // check whether there is a role 
     if(Roles.GetAllRoles().Length == 0) 
     // create a role using the UserName of the user 
     Roles.CreateRole(user.UserName); 
     Roles.AddUserToRole(user.UserName, user.UserName); 

    Let us know if there is anything else that we can do for you.

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  4. Ian Johnson
    Ian Johnson avatar
    11 posts
    Registered:
    11 Jan 2006
    19 Jan 2009
    Link to this post

    Hey Ivan,

    From what you said, I tried the following in the users.aspx.cs file. "user.username" as you suggest below produces.. "The name user does not exist in the current context"., so I used manageUsers... but manageUsers.UserName is null in that context. I want a way to access the value of the username field from the insert mode of the user form when they create a new user. Can you suggest anything?

     

     

     

     

     

    public void SaveButton_Click(object sender, EventArgs e)

     

    {

     

                if (manageUsers.Mode == ManageUsers.Modes.Insert)

     

    {

     

                // check whether there is a role

     

     

     

     

     

    if (!Roles.RoleExists(manageUsers.UserName))

     

    {

     

    // create a role using the UserName of the user

     

     

     

     

     

    Roles.CreateRole(manageUsers.UserName);

     

    }

     

                Roles.AddUserToRole(manageUsers.UserName, manageUsers.UserName);

     

                }

    }

     

     

     

     

     

     

     

     

  5. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    19 Jan 2009
    Link to this post
    Hi Ian Johnson,

    Below is the code that you could use:

    // create user using CreateUser method  
    MembershipUser user = Membership.CreateUser("USER_NAME""PASSWORD""EMAIL@ADDHERE.COM"); 
    ProfileBase profile = ProfileBase.Create(user.UserName); 
    profile.SetPropertyValue("FirstName", user.UserName); 
    // assign user to role 
    Roles.CreateRole(user.UserName); 
    Roles.AddUserToRole(user.UserName, user.UserName); 
    // save user 
    profile.Save(); 

    All the best,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  6. Ian Johnson
    Ian Johnson avatar
    11 posts
    Registered:
    11 Jan 2006
    20 Jan 2009
    Link to this post

    Hey Ivan,

    Thanks for the help, I am getting closer now. I just need some coding help to add a blog to a page programatically. I can add generic content...
    Telerik.Cms.Engine.WebControls.GenericContent gcContent = new Telerik.Cms.Engine.WebControls.GenericContent();
    gcContent.Content = "<strong>Hello world!</strong>";
    pageFromDb.Staged.AddControl("SideBarContent", gcContent);

    but cannot get a blog to display. Can you give me some sample code for programmatically adding a blogposts control to a page and making it only display posts from a specific blog.

    Many Thanks
    Ian

     

  7. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    20 Jan 2009
    Link to this post
    Hi Ian Johnson,

    Here is a sample that demonstrates  adding BlogPosts control on a page:

    CmsManager manager = new CmsManager(); 
    IPage page = manager.GetPage(cmsPage.ID, true); 
    page.Staged.AddControl("HERE NAME OF PLACEHOLDER"typeof(BlogPosts), 0, "ID OF PLACEHOLDER"); 
    cmsManager.SavePage(page); 

    Here is the code that will return the content of all blog posts

      // create new instance of BlogManager 
          BlogManager blogManager = new BlogManager(); 
          IList listOfAllBlogs = blogManager.GetBlogs(); 
          foreach (IBlog blogItem in listOfAllBlogs) 
              if(blogItem.Name == "MyBlog"
                    { 
                  IList allposts = blogManager.GetPosts(string.Empty, blogItem.ID); 
                       foreach(IContent post in allposts) 
                           Response.Write(post.Content + "<br />"); 
                    }  

    I hope this helps.

    Sincerely yours,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  8. Ian Johnson
    Ian Johnson avatar
    11 posts
    Registered:
    11 Jan 2006
    29 Jan 2009
    Link to this post
    Hey Ivan,

    I can get blog posts control to display on the page now, but how do I limit it to displaying a certain blog.
     
    ie.When I 'edit' the resulting blogposts control on the page.. under "Which blogs to display?" it says "All Blogs".  I want it to change to "Selected blogs only" and then have one blog eg. client1_blog ticked. How do I do this programatically?

    Even if I pass the BlogPosts page of a specific blog (clientBlog) to the page, it seems to display all blogs... eg.

    pageFromDb.Staged.AddControl(

    "Content", typeof(Telerik.Blogs.WebControls.BlogPosts), 0, clientBlog.PostPage);

     

    Can it be done when adding the control or does it need to be done after the control is added through LoadControl() function of ICmsWebControl?

    Thanks

    Regards
    Ian
  9. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    29 Jan 2009
    Link to this post
    Hello Ian Johnson,

    Here is an example where BlogPosts control will show all posts for a given blog (MyBlog)

                    BlogManager blogManager = new BlogManager(); 
                    IList listOfAllBlogs = blogManager.GetBlogs(); 
                    Guid[] blogIDs = new Guid[1]; 
                    if (listOfAllBlogs.Count > 0) 
                    { 
     
                        foreach (IBlog blog in listOfAllBlogs) 
                            if (blog.Name == "MyBlog"
                            { 
                                blogIDs[0] = blog.ID; 
                            } 
                    } 
     
                    Telerik.Blogs.WebControls.BlogPosts blogPosts = (Telerik.Blogs.WebControls.BlogPosts)control.LoadControl(); 
                    // blogPosts.DisplayMode = Telerik.Cms.Engine.WebControls.ContentView.DisplayModes.Auto; 
                    blogPosts.ParentIDs = blogIDs; 
                    manager.SaveControl(control); 

    I hope this helps.

    Regards,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  10. Ian Johnson
    Ian Johnson avatar
    11 posts
    Registered:
    11 Jan 2006
    02 Feb 2009
    Link to this post

    Hey Ivan,

    Thanks for your help so far. I am using the default membership provider. I am stuck trying to allow a single role "view" access to a single page programatically.


    Thanks
    Ian 

  11. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    02 Feb 2009
    Link to this post
    Hi Ian Johnson,

    Check out the code below:

          protected void button1_Click(object sender, System.EventArgs e) 
        {  
            SiteMapNode sNode = SiteMap.Provider.FindSiteMapNode("~/MyBlogPage.aspx"); 
            SetPage(((CmsSiteMapNode)sNode).CmsPage); 
        } 
     
        private void SetPage(ICmsPage _pg) 
        { 
            // new instance of CmsManager 
            CmsManager cManager = new CmsManager(); 
            // cast to ICms Page 
            ICmsPage pg = cManager.GetPage(_pg.ID, trueas ICmsPage; 
            //set permissions for our role "editors" 
            pg.CmsProvider.SecurityManager.SetPermissions((ISecured)pg, (int)PageRights.View, (int)PageRights.Delete, "custom"); 
            cManager.SavePage(pg); 
        } 


    Sincerely yours,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  12. Hafeez
    Hafeez avatar
    1 posts
    Registered:
    05 Jan 2012
    27 Jan 2012
    Link to this post
    i  developed an asp.net page i want to secure this page by login (that will get username and password). can i make the  login on asp.net page in sitefinity? i will be grateful if anyone can help me.
  13. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    30 Jan 2012
    Link to this post
    Hi,

    You can add your page as "external page" in Sitefinity and protect it by the login, or you can create Sitefinity's page and use the built-in security functionality.

    Greetings,
    Ivan Dimitrov
    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
Register for webinar
13 posts, 0 answered