none
Retrieve Users from target audience of sharepoint global navigation based on Domain name RRS feed

  • Question

  • Hi,

    I have retrived the Users and groups  name from sharepoint global navigation using C# like below.

    protected void getusersfromMenu()

            {

                SPSite oSPsite = new SPSite(SPContext.Current.Site.ID);

                SPWeb oSPWeb = oSPsite.OpenWeb();

                oSPWeb.AllowUnsafeUpdates = true;

                string user = SPContext.Current.Web.CurrentUser.LoginName.ToString();

                string domainname = user;

                string s = domainname.Remove(0, 7);

                string results = s.Split('\\')[0];

                txtPolicyLimit.Text = results.ToString();

                SPNavigationNodeCollection topnav = oSPWeb.Navigation.TopNavigationBar;

                foreach (SPNavigationNode node in topnav)

                {

                    string name = node.Title;

                    if (name == "Manage Claims")

                    {

                        string strTragetgrp = node.Properties["Audience"].ToString();

                        strTragetgrp = strTragetgrp.Replace(";", "").ToLower();

                        txtTradingLimit.Text = strTragetgrp.ToString();

                        if (strTragetgrp != null && !string.IsNullOrEmpty(strTragetgrp))

                        {

                            string[] strArrTargetUserGrps = strTragetgrp.Split(',');

                            if (strArrTargetUserGrps != null && strArrTargetUserGrps.Length > 0)

                            {

                                foreach (string strgroupName in strArrTargetUserGrps)

                                {

                                    if (!string.IsNullOrEmpty(strgroupName))

                                    {

                                        SPGroup grp = oSPWeb.Groups.GetByName(strgroupName);

                                        foreach (SPUser grpUser in grp.Users)

                                        {

                                          System.Web.UI.WebControls.ListItem lstitem = new System.Web.UI.WebControls.ListItem(grpUser.Name);

                                            if (!lstuser.Items.Contains(lstitem))

                                                lstuser.Items.Add(lstitem);

                                        }

                                    }

                                }

                            }

                        }

                    }

                }

                oSPWeb.AllowUnsafeUpdates = false;

            }

           

        }

    Now i want to retrieve the Users based on domain name.

    For example i have logged in with domain name as abc,then i should be able to see only abc domain users who are having access to Target audience of menu.

    Now am able to retrieve sharepoint group names and all users from those groups.

    Instead i should filter the users from particular domain from sharepoint group and display.

    Can anybody implemented this approach.Please let me know.

    Thursday, March 2, 2017 9:08 AM

All replies

  • hello, for retrieving users only from particular domain add additional filter to the loop by users:

    foreach (SPUser grpUser in grp.Users)
    {
        if (!grpUser.LoginName.ToLower().Contains("mydomain\\"))
            continue;
    
        System.Web.UI.WebControls.ListItem lstitem = new System.Web.UI.WebControls.ListItem(grpUser.Name);
        if (!lstuser.Items.Contains(lstitem))
            lstuser.Items.Add(lstitem);
    }


    Blog - http://sadomovalex.blogspot.com
    Dynamic CAML queries via C# - http://camlex.codeplex.com

    Thursday, March 2, 2017 10:28 AM