Copied to clipboard

Flag this post as spam?

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


  • Harshit 64 posts 226 karma points
    Mar 08, 2016 @ 06:07
    Harshit
    0

    MemberService GetAll

    Hi,

    I am trying to fetch all the member using Memberservice as below

      public  GetAllmembers()
        {
    
            int totalRecords;
            var Members = ApplicationContext.Current.Services.MemberService.GetAll(0, int.MaxValue, out totalRecords);
            foreach (var member in Members)
            {
                //
            }
            return ;
    
        }
    

    what could be the best way to store member and return to the angular js code to display them in a ng-table? any idea on this?

    Regards, Harshit

  • Dennis Adolfi 1072 posts 6378 karma points MVP 2x c-trib
    Mar 08, 2016 @ 07:06
    Dennis Adolfi
    2

    I personally is a sucker for JSON. I would return a list with only the member properties that interest me, as a JSON object.

    public JsonResult GetAllmembers()
            {
                int totalRecords;
                var Members = ApplicationContext.Current.Services.MemberService.GetAll(0, int.MaxValue, out totalRecords);
                var viewModel = new List<JsonViewModel>();
                foreach (var member in Members)
                {
                    viewModel.Add(new JsonViewModel()
                                  {
                                      Name = member.Name
                                  });
                }
                return Json(viewModel, JsonRequestBehavior.AllowGet);
            }
    
  • Harshit 64 posts 226 karma points
    Mar 08, 2016 @ 08:19
    Harshit
    0

    Hi Dennis,

    I am getting error on return.

    Json(viewModel, JsonRequestBehavior.AllowGet);
    

    it showing it has some invalid arguments.

    any idea?

  • Dennis Adolfi 1072 posts 6378 karma points MVP 2x c-trib
    Mar 08, 2016 @ 10:08
    Dennis Adolfi
    0

    Sorry, did´nt test the code, it was more to give an idea off the concept. But i can post the full code.

    This code workes for me and returns a json object with all members:

    Using:

    using System.Web.Mvc;
    using umbraco.cms.businesslogic.member;
    using System.Collections.Generic;
    using Umbraco.Web.Mvc;
    

    SurfaceController:

    namespace ExampleNamespace
    {
        public class GetMemberController : SurfaceController
        {
            public JsonResult GetAllMembers()
            {
                var members = Member.GetAll;
                var viewModel = new List<JsonViewModel>();
                foreach (var member in members)
                {
                    viewModel.Add(new JsonViewModel()
                    {
                        LoginName = member.LoginName
                    });
                }
                return Json(viewModel, JsonRequestBehavior.AllowGet);
            }
        }
    
        public class JsonViewModel
        {
            public string LoginName { get; set; }
        }
    }
    

    Action Url:

    /umbraco/surface/GetMember/GetAllMembers
    

    Result:

    [{"LoginName":"Jane Doe"},{"LoginName":"John Doe"}]
    
  • Aristotelis Pitaridis 76 posts 393 karma points
    Mar 08, 2016 @ 10:19
    Aristotelis Pitaridis
    1

    You do not need to create a view model. You can just use Linq in order to get the data in the form you want.

    using System.Linq;
    using System.Web.Mvc;
    using Umbraco.Core;
    using Umbraco.Web.Mvc;
    
    namespace MyData
    {
        public class GetMemberController : SurfaceController
        {
            public JsonResult GetAllMembers()
            {
                int totalRecords;
                var Members = ApplicationContext.Current.Services.MemberService.GetAll(0, int.MaxValue, out totalRecords);
    
                var viewModel = from m in Members
                                select new { Name = m.Name, Email = m.Email };
    
                return Json(viewModel, JsonRequestBehavior.AllowGet);
            }
        }
    }
    
  • Harshit 64 posts 226 karma points
    Mar 08, 2016 @ 11:56
    Harshit
    0

    Hi Aristotelis ,

    Can we do it with ApiController instead of surface controller?

    Thanks,

  • Harshit 64 posts 226 karma points
    Mar 08, 2016 @ 12:19
    Harshit
    0

    Yes, I was trying using UmbracoApiController

    public class GetAllMemberController : UmbracoApiController
    { 
        public Returntype GetAllmembers()
        {
        int totalRecords;
        var Members = ApplicationContext.Current.Services.MemberService.GetAll(0, int.MaxValue, out totalRecords);
        foreach (var member in Members)
        {
            // store member 
        }
        return Returntype_list ;
    
    }
    

    i was not getting the idea on Returntype and store member accordingly.

    can you please suggest ?

    Thanks

  • Aristotelis Pitaridis 76 posts 393 karma points
    Mar 08, 2016 @ 12:19
    Aristotelis Pitaridis
    100

    Try the code below. I have not tested so inform me if it does not work.

    using System.Linq;
    using System.Web.Http;
    using Umbraco.Core;
    using Umbraco.Web.WebApi;
    
    namespace MyData
    {
        public class GetMemberApiController : UmbracoApiController
        {
            [HttpGet]
            public object GetAllMembers()
            {
                int totalRecords;
                var Members = ApplicationContext.Current.Services.MemberService.GetAll(0, int.MaxValue, out totalRecords);
    
                var viewModel = from m in Members
                                select new
                                {
                                    Name = m.Name,
                                    Email = m.Email
                                };
    
                return viewModel;
            }
        }
    }
    

    You can get the data using the following code.

    $.ajax({
        type: "GET",
        async: false,
        url: "/umbraco/api/GetMemberApi/GetAllMembers",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        statusCode: {
            200: function (data) {
                console.log(data);
                // Do whatever you want with your data
            }
        },
        error: function () { }
    });
    
  • Harshit 64 posts 226 karma points
    Mar 08, 2016 @ 15:24
    Harshit
    0

    its working perfectly fine.

    Thanks a lot :)

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies