Copied to clipboard

Flag this post as spam?

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


  • Richard Kingston 46 posts 69 karma points
    Nov 08, 2010 @ 22:39
    Richard Kingston
    0

    Difficulties with UserType.MakeNew

    Hi,

    Apologies if this isn't in the write forum - I couldn't decide between here and the our.dev section - but I'm having difficulties with the UserType.MakeNew function, even after copying the exact code from http://our.umbraco.org/wiki/reference/api-cheatsheet/users,-user-types-and-permissions.

    The issue I get is "The type initializer for 'umbraco.BusinessLogic.UserType' threw an exception" and no other useful information to help me track down the issue.

    Searches through Google and this forum have all come up blank.

    Can anyone help?

    Thanks

    Rich

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Nov 08, 2010 @ 23:13
    Aaron Powell
    0

    Are you trying to do this from a web application or from outside a web application (ie - a console application)?

    Can you provide all your code?

     

    PS: This is the right forum, the our.dev is for questions about the forum itself :)

  • Richard Kingston 46 posts 69 karma points
    Nov 09, 2010 @ 20:02
    Richard Kingston
    0

    Thanks slace.

    I'm really just testing at this point, but I'm running a web application that on the click of a button, sets off the method NewRegistration.

    The line that fails is the first line of the method CreateUserType.

    Code is:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using umbraco.cms.businesslogic.web;
    using umbraco.BusinessLogic;

    namespace UmbracoLink
    {
        public class UmbracoLink
        {

            #region Public methods
         
            public void NewRegistration(String SiteName, String Username, String Password)
            {
                UserType userType = CreateUserType(SiteName);
               // User user = CreateUser(userType, SiteName, Username, Password);
               // Int64 siteId = CreateSite(SiteName, user);
            }

            #endregion

            #region Private methods

            private UserType CreateUserType(String SiteName)
            {
                UserType ut = UserType.MakeNew(SiteName + "Admins", "CAD", SiteName.ToLower() + "-admins");
                ut.Save();
                return ut;
            }
           
            private User CreateUser(UserType UserType, String SiteName, String Username, String Password)
            {
                //set the right user type ID (available in usertype tree in the backend)
                User.MakeNew(SiteName + "Admin", Username, Password, UserType);
               
                //Get the user from ID or login
                User u = new User(Username);
               
                //Allow access to umbraco section
                u.addApplication("settings");
                u.Save();
                return u;
            }

            private Int64 CreateSite(String SiteName, User User)
            {
                //Get the type you would like to use by its alias
                //and the user who should be the creator of the document
                DocumentType dt = DocumentType.GetByAlias("Textpage");
              
                //create a document with a name, a type, an umbraco user, and the ID of the document's parent page.
                //To create a document at the root of umbraco, use the id -1
                Document doc = Document.MakeNew("My new document", dt, User, -1);
               
                //after creating the document, prepare it for publishing
                doc.Publish(User);
               
                //Tell umbraco to publish the document
                umbraco.library.UpdateDocumentCache(doc.Id);
                return doc.Id;
            }

            #endregion
        }
    }

  • Eduardo 106 posts 130 karma points
    Nov 15, 2010 @ 11:43
    Eduardo
    0

    Hi Richard,

    Have you configured the data adapters within the web.config?

    Sincere regards,
    Eduardo

  • Richard Kingston 46 posts 69 karma points
    Nov 17, 2010 @ 22:20
    Richard Kingston
    0

    Hi Eduardo,

    Where would I find instructions about that?

    So far I've installed the product using the MS Web Platform Installer, configured it and created a few sample templates, stylesheets, content, etc using the UI.

    Then I went straight into creating the .NET project and class that's above.

  • Eduardo 106 posts 130 karma points
    Nov 17, 2010 @ 22:47
    Eduardo
    0

    Hi Richard,

    Why do you want to create the groups dinamically? I prefer creating groups through Umbraco BackEnd.

    If you want to create members(frontend user accounts), use ASP.NET controls like createUserWizard:

    http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/login/createuserwizard.aspx

    You can create members with createuserwizard and then, create an user for the backend. Here goes the example.

    On the aspx or ascx page:

         <asp:CreateUserWizard ID="CreateUserWizard1" runat="server" 
    oncreateduser="CreateUserWizard1_CreatedUser">
    <WizardSteps>
    <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">

    </asp:CreateUserWizardStep>
    <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">
    </asp:CompleteWizardStep>
    </WizardSteps>
    </asp:CreateUserWizard>

    On the createduser event handler:

     protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e) 
    {       

    var newgroup =umbraco.BusinessLogic.UserType.MakeNew("MynewGroup",
    "PD",
    "newgroup");   
    var user = umbraco.BusinessLogic.User.MakeNew(CreateUserWizard1.UserName.ToString(),
    CreateUserWizard1.UserName,
    CreateUserWizard1.Password, newgroup);
    user.Save();
     }

     

    Web.Config example(it works):

      
    <!-- Membership Provider -->
    <membership defaultProvider="UmbracoMembershipProvider" userIsOnlineTimeWindow="15">
    <providers>
    <clear />
    <add name="UmbracoMembershipProvider" type="umbraco.providers.members.UmbracoMembershipProvider" enablePasswordRetrieval="false" enablePasswordReset="false" requiresQuestionAndAnswer="false" defaultMemberTypeAlias="Another Type" passwordFormat="Hashed" />
    <add name="UsersMembershipProvider" type="umbraco.providers.UsersMembershipProvider" enablePasswordRetrieval="false" enablePasswordReset="false" requiresQuestionAndAnswer="false" passwordFormat="Hashed" />
    </providers>
    </membership>
    <profile defaultProvider="UmbracoMemberProfileProvider" enabled="true" >
    <!--inherits="BeComputed.MemberProfile"-->
    <providers>
    <clear />
    <add name="UmbracoMemberProfileProvider" type="umbraco.providers.members.UmbracoProfileProvider, umbraco.providers" />
    </providers>
    <properties>
    <clear />
    <add name="nombre" allowAnonymous="false" provider="UmbracoMemberProfileProvider" type="System.String" />
    <add name="apellidos" allowAnonymous="false" provider="UmbracoMemberProfileProvider" type="System.String" />
    <add name="fechaexpiracion" allowAnonymous="false" provider="UmbracoMemberProfileProvider" type="System.DateTime" />
    </properties>
    </profile>
    <!-- added by NH to support membership providers in access layer -->
    <roleManager enabled="true" defaultProvider="UmbracoRoleProvider">
    <!--AspNetSqlRoleProvider-->
    <providers>
    <clear />
    <add name="UmbracoRoleProvider" type="umbraco.providers.members.UmbracoRoleProvider" />
    <!--<add connectionStringName="LocalSqlServer" applicationName="UMBRACO" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    <add applicationName="UMBRACO" name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />-->
    </providers>
    </roleManager>

     

    For more information:

    Follow the this link, you will find an interesting wiki article.

    http://our.umbraco.org/forum/developers/api-questions/14567-Add-new-user-via-API

    If you have any doubt let me know.

    HTH.

    Sincere regards,
    Eduardo Macho

  • Richard Kingston 46 posts 69 karma points
    Dec 02, 2010 @ 12:03
    Richard Kingston
    0

    Thanks Eduardo,

    I'll give that a shot.

Please Sign in or register to post replies

Write your reply to:

Draft