2

I am developing a web part (not a visual web part) that displays the site owners of a SharePoint site and their email addresses. Right now I am displaying the information in a listbox. It displays the site owners and emails like this:

Smith, John - [email protected]

Washington, Anna - [email protected]

This is the code I have:

public class SiteOwners : WebPart
{

    string mySiteURL = "my SharePoint URL goes here";
    ListBox mySPLists = new ListBox();
    string listInfo = "";

    protected override void CreateChildControls()
    {
        this.Controls.Add(mySPLists);

        using (SPSite site = new SPSite(mySiteURL))
        {
            using (SPWeb web = site.RootWeb)
            {
                SPGroup ownerGroup = web.AssociatedOwnerGroup;
                if (ownerGroup != null)
                {
                    foreach (SPUser user in ownerGroup.Users)
                    {
                            listInfo = user.Name + " - " + user.Email;
                            mySPLists.Items.Add(listInfo);
                    }
                }
                //web.Dispose();
            }

        }
    }
}

I would like to display the site owners' names and emails in 2 different columns so that it looks better. I want it to look like this:

Site Owner              Email

Smith, John             [email protected]

Washington, Anna        [email protected]

Should I use a list view, data grid, table? Advice on how to use these items would be helpful.

1 Answer 1

0

You can use SPGridView. Populate a data table with the necessary values and bind it the SPGridView. You can use below code to start with:

 public class SiteOwners : WebPart
    {

        string mySiteURL = "my SharePoint URL goes here";
                string listInfo = "";
    SPGridView spGrid=new SPGridView();
        protected override void CreateChildControls()
        {

            DataTable dtTable=new DataTable();
            dtTable.Columns.Add("Name");
            dtTable.Columns.Add("Email");

            using (SPSite site = new SPSite(mySiteURL))
            {
                using (SPWeb web = site.RootWeb)
                {
                    SPGroup ownerGroup = web.AssociatedOwnerGroup;
                    if (ownerGroup != null)
                    {
                        foreach (SPUser user in ownerGroup.Users)
                        {
                                DataRow dtRow=dtTable.NewRow();
                                dtRow["Name"]=user.Name;
                                dtRow["Email"]=user.Email;
                               dtTable.Rows.Add(dtRow);
                        }
                    }
                   spGrid.DataSource =dtTable;
                   spGrid.DataBind();
                }

            }
            this.Controls.Add(spGrid);
        }
    }
3
  • @Mischa you will have to add System.Data.dll reference in your solution and add using System.Data in your code
    – Unnie
    Jun 30, 2014 at 16:02
  • I get an error that says: "InvalidOperationException was unhandled by user code. This derived class does not allow AutoGenerateColumns to be true. You must specify each of the columns using BoundField, TemplateField, or other compatible class." I added the line "spGrid.AutoGenerateColumns = false;" and the error goes away, but nothing is displaying when I run the web part.
    – Mischa
    Jun 30, 2014 at 16:36
  • It works now! I added the line dtTable.Rows.Add(dtRow);
    – Mischa
    Jul 1, 2014 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.