0

I am creating a visual web part that displays the site owners of a SharePoint site and each site owner's email. I would like users to be able to click the email address, and this will open up a new email window in Outlook.

How can you do this in C#?

This is the code I have:

(It does not contain any code that links the email addresses to Outlook yet).

namespace DisplaySiteOwnersVWP.VisualWebPart1
{
[ToolboxItemAttribute(false)]
public partial class VisualWebPart1 : WebPart
{

string mySiteURL = "my Site URL goes here";
DataTable dtTable = new DataTable();

public VisualWebPart1()
{
}

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    InitializeControl();
}


protected void Page_Load(object sender, EventArgs e)
{

    dtTable.Columns.Add(new DataColumn("Name"));
    dtTable.Columns.Add(new DataColumn("UserID"));
    dtTable.Columns.Add(new DataColumn("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["UserID"] = user.LoginName.Substring(6);
                        dtRow["Email"] = user.Email;
                        dtTable.Rows.Add(dtRow);

                }
            }

            gvOwners.DataSource = dtTable;
            gvOwners.DataBind();

        }
    }

}


}
}
1

Make a standard web link in the format mailto:user@domain.com

2
  • I'm having trouble figuring out how to make the email a link to click on. I know that the code "System.Diagnostics.Process.Start("mailto:xxx@xxx.com");" will open a new email in outlook. But I do not know how to link this to the email address so that when it is clicked, the new Outlook email pops up. – Mischa Jul 2 '14 at 15:21
  • Try: stackoverflow.com/questions/9813144/… – Aboba Jul 2 '14 at 15:54
0

I figured out how to do it. I added a column like this:

<asp:TemplateField HeaderText=" " SortExpression="EmailAdress">
      <EditItemTemplate>
      <asp:TextBox ID="txtEmailAddress" runat="server" Text='<%# Bind("EmailAddress") %>'></asp:TextBox>
      </EditItemTemplate>

      <ItemTemplate>
      <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("EmailAddress" , "mailto:{0}") %>'
                Text="Click here to email"></asp:HyperLink>

      </ItemTemplate>
</asp:TemplateField>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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