Products

Solutions

Learn More

Partners

Community

Blog

About

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeOur CommunityOur CommunityGeneral Discuss...General Discuss...FYI: Getting DNN Module Running Context - C# code sampleFYI: Getting DNN Module Running Context - C# code sample
Previous
 
Next
New Post
9/30/2009 5:54 PM
 

Hi All,

Here is a tested working C# code sample I have just "cooked" after digging through DNN sources - I tried but I didn't find something like that on Internet therefore I hope this sample will be helpful to the folks starting DNN custom modules development:

 /// <summary>Constructor</summary>
public class MyDNNContext
{
    private DotNetNuke.Entities.Portals.PortalSettings _portalSettings;
    private DotNetNuke.Entities.Users.UserInfo _userInfo;
    private DotNetNuke.Entities.Tabs.TabInfo _tabInfo;
    private DotNetNuke.Services.Tokens.TokenReplace _tokenizer;

    /// <summary>Constructor</summary>
    public DNNModuleRunningContext()
    {
         _portalSettings =
                 DotNetNuke.Entities.Portals.PortalController.GetCurrentPortalSettings();
         _userInfo = _portalSettings.UserInfo;
         _tabInfo = _portalSettings.ActiveTab;

         _tokenizer = new DotNetNuke.Services.Tokens.TokenReplace();
         _tokenizer.User = _userInfo;
         _tokenizer.AccessingUser = _userInfo;
         _tokenizer.DebugMessages = false;

    }

    /// <summary> Version Info </summary>
    public string VersionInfo
    {
        get
        {
            return string.format("version ={0}, type = {1}, name = {2}",
              DotNetNuke.Application.DotNetNukeContext.Current.Application.Version,
              DotNetNuke.Application.DotNetNukeContext.Current.Application.Type,
              DotNetNuke.Application.DotNetNukeContext.Current.Application.Name);
        }
    }

    /// <summary> Connection String</summary>
    public string ConnectionString
    {
        get
        {
            return DotNetNuke.Common.Utilities.Config.GetConnectionString("SiteSqlServer");
            //OK also - return DotNetNuke.Common.Utilities.Config.GetConnectionString();
        }
    }

    /// <summary> Current Portal Id</summary>
    public int PortalId
    {
        get { return _portalSettings.UserId; }
    }

    /// <summary> Current User Id</summary>
    public int UserId
    {
        get { return _portalSettings.UserId; }
    }

    /// <summary> Current User Name</summary>
    public string UserName
    {
        get
        {
            return _userInfo.Username;
        }
    }

    /// <summary> Return True if current user is SuperUser</summary>
    public bool IsSuperUser
    {
        get
        {
            return _userInfo.IsSuperUser;
        }
    }


    /// <summary> User Roles names array</summary>
    public string[] UserRoles
    {
        get
        {
            return _userInfo.Roles;
        }
    }

    /// <summary> Current Tab Id</summary>
    public int TabId
    {
        get
        {
            return _tabInfo.TabID;
        }
    }

    /// <summary> Current Tab Name</summary>
    public string TabName
    {
        get
        {
            return _tabInfo.TabName;
        }
    }


    /// <summary> Current Tab Name via [Tab:TabName] token</summary>
    public string TabName1
    {
        get
        {
        return _tokenizer.ReplaceEnvironmentTokens("[Tab:TabName]"); }}

    /// <summary> Current UserName via [User:Username] token</summary>
    public string UserName1
    {
        get
        {
            return _tokenizer.ReplaceEnvironmentTokens("[User:Username]");
        }
    }

    /// <summary> Get Current user profile property by property name</summary>
    public string GetUserProfileProperty(string propertyName)
    {
        return _tokenizer.ReplaceEnvironmentTokens(
               string.format("[Profile:{0}]", propertyName));
    }
}
 

Thank you.

--Shamil

 

 
New Post
3/4/2011 8:00 AM
 
yr property Portal id is wrong, it should return the PortalId and not the UserId public int PortalId { get { return _portalSettings.UserId; } } Should be public int PortalId { get { return _portalSettings.PortalId; } }
 
Previous
 
Next
HomeHomeOur CommunityOur CommunityGeneral Discuss...General Discuss...FYI: Getting DNN Module Running Context - C# code sampleFYI: Getting DNN Module Running Context - C# code sample


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out