none
Retrieve SharePoint User Group details in ASP. Gridview in Visual webpart RRS feed

  • Question

  • Hi,

    I have one requirement where i need to fetch user group names in asp gridview column.Actually my design is like below.

    

    IN the above diagram when the user selects a name from people pipcker and clicks the show user group button the group names of that particular user will show in the 1st column of gridview.The first column is a bound field.

    The remaining columns are template field with checkboxes.,

    So once the group names displayed the user will select the checkbox based on his need.Based on the checkbox selection the user will get added to the corresponding sharepoint group.

    Now i am trying to show the group names in gridview bound field but no data is displaying and gridview also not showing in the page.

    Please reply with some sample code.

    Regards,

    Poovi

    Monday, January 9, 2017 4:54 AM

Answers

All replies

  • Hi,

     

    Just sample code for your reference.

     

    <asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
        <asp:GridView ID="grd_View" Caption="" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:BoundField DataField="GroupName" HeaderText="Group Name" ItemStyle-Width="150" />
                <asp:TemplateField HeaderText="Policy">
                    <ItemTemplate>
                        <asp:CheckBox ID="chb_Policy" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Click"></asp:Button>
        <br />
        <asp:Label ID="lb_Tip" runat="server" Text="Label"></asp:Label>
    </asp:Content>

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dt = new DataTable();
    
                    dt.Columns.AddRange(new DataColumn[2] { new DataColumn("GroupID"),new DataColumn("GroupName") });
                    SPWeb web = SPContext.Current.Web;
                    foreach(SPGroup group in web.Groups){
                        dt.Rows.Add(group.ID,group.Name);   
                    }                
                    grd_View.DataSource = dt;
                    grd_View.DataBind();
                    Session["GroupDt"] = dt;
                    //more logic if necessary
                }
            }
    
            protected void btnSave_Click(object sender, EventArgs e)
            {
                string tmp = string.Empty;
                DataTable dt;
                if (Session["GroupDt"] != null)
                {
                    dt = Session["GroupDt"] as DataTable;
    
                    for (int j = 0; j < grd_View.Rows.Count; j++)
                    {
                        ControlCollection childs = grd_View.Rows[j].Cells[1].Controls;
                        foreach (Control ctl in childs)
                        {
                            if (ctl.GetType() == typeof(CheckBox) && ((CheckBox)ctl).Checked)
                            {
                                tmp += dt.Rows[j]["GroupID"] + ";" ;
                                //more logic
                            }
                        }
    
                    }
                    lb_Tip.Text = "Selected:" + tmp.ToString();
                }
                else {
                    lb_Tip.Text = "group data lost";
                }
            }

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, January 10, 2017 5:07 AM
  • Hi,

    Thanks for your reply.

    Actually a little change in my requirement.

    The screen design is same.

    I have a sharepoint site with global navigation and created 5 menu's in that.I have set my custom sharepoint group as the target audience for each menu.

    Now i want to do 2 functionalities.

    1)Programmatically retrieve the target audience group names of each menu in global navigation.

    based on that i need to check the checkboxes automatically.

    2)Programmatically Update the target audience with new sharepoint group to menu in global navigation.

    This one is based on checkbox selection in gridview.

    Now am able to get the global navigation nodecollection using the below code.

               SPNavigationNodeCollection topnav = oSPWeb.Navigation.TopNavigationBar;
                foreach (SPNavigationNode node in topnav)
                {
                    string name=node.Title;

                 }

    here name is displaying the menu name.

    But t want to see the sharepoint group name which is set as target audience for the menu.

    Please help.

    Regards,

    Poovi


    Wednesday, January 11, 2017 9:59 AM
  • Hi,

     

    For navigation target audience, you could check the property (navNode.Properties["Audience"]) of SPNavigationNode.

    Here is one thread for your reference.

    http://reddishetty.blogspot.sg/2012/07/set-target-audience-to-quick-launch.html#!/2012/07/set-target-audience-to-quick-launch.html

     

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by Poovi Wednesday, January 18, 2017 11:34 AM
    Thursday, January 12, 2017 7:55 AM