Developer forum

Forum » CMS - Standard features » Create new user via API

Create new user via API

Bjørn Kamfjord
Reply

Hey.

I working on an frontend adminstration of users. Ref. this thread: https://doc.dynamicweb.com/forum/templates/templates/get-users-from-same-group

From the frontend you submit a new user with username, Name and a selectable "deparmtment"(group).

I use an ajax post function to update users, and delete users. This is working fine at the moment. The problem arise when I want to create a new user... I'm not really sure how to go about actually doing that. I'm using this code below for deleting a user.

Dynamicweb.Security.UserManagement.User.Delete(userID);

Is there a similar method for Creating a user? Also I guess I would have to check to see if the username already exist...?

My thoughts so far... Not working...

@inherits Dynamicweb.Rendering.RazorTemplateBase<Dynamicweb.Rendering.RazorTemplateModel<Dynamicweb.Rendering.Template>>

@using System.Web;
@using Dynamicweb;
@using System.Data.Linq;
@using Dynamicweb.Security.UserManagement;
@using System.Collections.Generic;
@using System.Linq;
@using System.Text;
@using System.Threading.Tasks;

@Title("JSON - Add New User")

@{

    List<Root> feedObject = new List<Root>();

    Root feedRoot = new Root();

    var userUpdate = new UserUpdate();

    try
    {
        int userID = 0; //How to find a new unique user ID?
        var userName = HttpContext.Current.Request["userName"];
        var userNiceName = HttpContext.Current.Request["niceName"];

        int groupID = 0; // This we get from the POST
        var parameterGroupID = HttpContext.Current.Request["groupId"];
        int.TryParse(parameterGroupID, out groupID);

        var userNew = Dynamicweb.Security.UserManagement.User.GetUserByUserName(userName);
                

        if (userNew != null)
        {

            userNew.Name = userNiceName;
            userNew.AddToGroup(groupID);
            userUpdate.groupId = groupID;
            userUpdate.name = userNew.Name;

            feedRoot.UserUpdate = userUpdate;

            var state = userNew.Save();
            feedRoot.state = state;
            //Clears the session cahce
            System.Web.HttpContext.Current.Session.Clear();
        }
        else
        {
            feedRoot.state = false;
        }
    }
    catch
    {
        feedRoot.state = false;
    }
    feedObject.Add(feedRoot);

    string jsonFeed = Newtonsoft.Json.JsonConvert.SerializeObject(feedObject);
}

@functions{
    string getParameter(string param)
    {
        string value = HttpContext.Current.Request[param];
        return (value == null ? string.Empty : value);
    }

    public class UserUpdate
    {
        public string name { get; set; }
        public int groupId { get; set; }
        public int userId { get; set; }

    }

    public class Root
    {
        public Root()
        {
            UserUpdate = new UserUpdate();
        }

        public UserUpdate UserUpdate { get; set; }
        public bool state { get; set; }
    }
}

@jsonFeed

 

 

 

 

addNewUser.png

Replies

 
Bjørn Kamfjord
Reply

Solved it.....

I had to use

var userNew = new Dynamicweb.Security.UserManagement.User();
@inherits Dynamicweb.Rendering.RazorTemplateBase<Dynamicweb.Rendering.RazorTemplateModel<Dynamicweb.Rendering.Template>>

@using System.Web;
@using Dynamicweb;
@using System.Data.Linq;
@using Dynamicweb.Security.UserManagement;
@using System.Collections.Generic;
@using System.Linq;
@using System.Text;
@using System.Threading.Tasks;

@Title("JSON - Add New User")

@{

    List<Root> feedObject = new List<Root>();

    Root feedRoot = new Root();

    var userCreate = new UserCreate();

    try
    {
        int userID = 0; //How to find a new unique user ID?
        var userName = HttpContext.Current.Request["userName"];
        var userNiceName = HttpContext.Current.Request["niceName"];

        int groupID = 0; // This we get from the POST
        var parameterGroupID = HttpContext.Current.Request["groupId"];
        int.TryParse(parameterGroupID, out groupID);

        var userNew = new Dynamicweb.Security.UserManagement.User();
        

        if (userNew != null)
        {

            userNew.Name = userNiceName;
            userNew.UserName = userName;
            userNew.Save();
            userNew.AddToGroup(groupID);
            //userUpdate.groupId = groupID;
            //userUpdate.name = userNew.Name;

          //feedRoot.UserCreate = userCreate;

            var state = userNew.Save();
            feedRoot.state = state;
            feedRoot.message = userNew.Name;
            //Clears the session cahce
            System.Web.HttpContext.Current.Session.Clear();
        }
        else
        {
            feedRoot.state = false;
        }
    }
    catch
    {
        feedRoot.state = false;
    }
    feedObject.Add(feedRoot);

    string jsonFeed = Newtonsoft.Json.JsonConvert.SerializeObject(feedObject);
}

@functions{
    string getParameter(string param)
    {
        string value = HttpContext.Current.Request[param];
        return (value == null ? string.Empty : value);
    }

    public class UserCreate
    {
        public string name { get; set; }
        public int groupId { get; set; }
        public int userId { get; set; }

    }

    public class Root
    {
        public Root()
        {
            UserCreate = new UserCreate();

        }

        public UserCreate UserCreate { get; set; }
        public bool state { get; set; }
        public string message { get; set; }
    }
}

@jsonFeed

 

You must be logged in to post in the forum