Tuesday, January 15, 2013

Binding Groups from SharePoint Site to DropDownlist Without a Single line of Loop

Binding Groups from SharePoint Site to DropDownlist Without a Single line of Loop


Ascx
Note:- In my Previous Post I had showed how to fetch users from a selected SharePoint. Now I have Binded SharePoint Groups Dynamically to Dropdown without a single line of loop. You can use the dropdown selected text or value and use that in the place PRASAD KINGDOM Owners in my previous post.
<asp:DropDownList ID="ddlSharePointGroups" runat="server" DataValueField="Title" DataTextField="Title">
</asp:DropDownList>
<br />
<asp:Label ID="lblErrorMessage" runat="server"></asp:Label>


Ascx.cs
try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPWeb currentWeb = SPContext.Current.Web;
                    SPList lst = currentWeb.Lists["User Information List"];
                    SPQuery sQuery = new SPQuery();
                    sQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy><Where><Eq><FieldRef Name='ContentType' /><Value Type='Text'>SharePointGroup</Value></Eq></Where>";
                    sQuery.ViewFields = "<FieldRef Name='Title' />";
                    sQuery.ViewFieldsOnly = true;
                    SPListItemCollection myColl = lst.GetItems(sQuery);
                    if (myColl.Count > 0)
                    {
                        ddlSharePointGroups.DataSource = myColl.GetDataTable();
                        ddlSharePointGroups.DataBind();
                    }
                    ddlSharePointGroups.Items.Insert(0, new ListItem("Select SharePoint Group"));
                });

No comments:

Post a Comment