locked
Extracting email addresses for owners RRS feed

  • Question

  • Hi

    I need to send an email to all site collection owners. How do I extract the email addresses?

    /Jakob

    Sunday, May 27, 2012 7:25 PM

Answers

  • Hi Jakob, you could use a Powershell script like this one:

    Get-SPWebApplication | Get-SPSite -Limit All | ForEach-Object{
    
     Get-SPUser -Web $_.URL | ?{$_.isSiteAdmin -eq "$true" -and $_.Email -ne ""} | select DisplayName, Email
    
    }

    The script extract DisplayName and Email of all site collection administrators with an e-mail.

    Cheers
    Riccardo


    Italian SharePoint Community - My blog - My profile


    Monday, May 28, 2012 2:27 PM

All replies

  • Hi Jakob,

    For sending the email to all site collection administrator. If it is one time activity then simply you can write one console application and write code for sending emails, if it is regular job then you can write one Timer Job and write code. The code should be look like below.

    1. First please check if all sites collection are within one WebApplication, if it is so then you need to get only one webapplication object by using SharePoint Object mode like SPFarm, SPSite or so. Otherwise you need to check each webapplication and again need to iterate the code for checking site collection within every webapplication in that SPFarm.
    2. In Code get the WebApplication object then in every webapplication get the SPSite object.
    3. Get SPuser object by using  the SPSite.SiteCollectionAdministrator object.
    4. If you have the SPUser object then we can get the email of that user by using SPUser.emial property.
    5. Append all the user’s email in StringBuilder object. And use this string as email address to send the email.

    Please feel free to reply if you face any issue, or need the code for the same.


    Krishana Kumar http://www.mosstechnet-kk.com

    Monday, May 28, 2012 12:04 PM
  • Hi Krishana

    Thanks for the answer. I'm afraid you expect me to be much more experienced in writing code than I am. Could you maybe point me to a site where I can find a more detailed explanation?

    /Jakob


    • Edited by J Rohde Monday, May 28, 2012 1:13 PM
    Monday, May 28, 2012 1:13 PM
  • Hi Jakob, you could use a Powershell script like this one:

    Get-SPWebApplication | Get-SPSite -Limit All | ForEach-Object{
    
     Get-SPUser -Web $_.URL | ?{$_.isSiteAdmin -eq "$true" -and $_.Email -ne ""} | select DisplayName, Email
    
    }

    The script extract DisplayName and Email of all site collection administrators with an e-mail.

    Cheers
    Riccardo


    Italian SharePoint Community - My blog - My profile


    Monday, May 28, 2012 2:27 PM
  • Hi Riccardo

    Thanks, that did the trick.

    If I wanted to add a column with the site name and/or URL of the site, how would that look?

    Thanks again

    Jakob

    Tuesday, May 29, 2012 6:25 AM
  • Hi Jakob,

    1. Create a new project by using Visual Studio Console application.
    2. Add the reference of Microsoft.SharePoint assembly.
    3. Write the following code in main method.

                SPSecurity.RunWithElevatedPrivileges(delegate()

                {

                    StringBuilder allSiteAdminEmail = new StringBuilder();

                    using (SPSite objSite = new SPSite("SiteURL"))

                    {

                        Console.WriteLine("Find Site Collection " + objSite.Url);

                        SPWebApplication webApp = objSite.WebApplication;

                        SPSiteCollection allSiteColl =  webApp.Sites;

                        foreach (SPSite site in allSiteColl)

                        {

                            string email = site.Owner.Email;

                            allSiteAdminEmail.Append(email);

                            allSiteAdminEmail.Append(";");

                        }

                        string emailSummary = string.Empty;

                        var messageHeaders = new StringDictionary();

                        messageHeaders.Add("to", Convert.ToString(allSiteAdminEmail));

                        messageHeaders.Add("from", "no_reply_sharepoint@kk.com");//pass as your desire

                        messageHeaders.Add("subject", "Pass as you desire");//pass as your desire

                        messageHeaders.Add("cc", "CCEmailID");//pass as your desire

                        messageHeaders.Add("bcc", "BCCEmailID");//pass as your desire

                        bool sendMail = SPUtility.SendEmail(objSite.RootWeb, messageHeaders,"Message Body [Text]");

                        allSiteAdminEmail = null;

                        if (sendMail)

                        {

                            // emailSummary = "<EmailMessage>" + "<To>" + to + "</To>" + "<From>" + from + "</From>" + "<Subject>" + SPEncode.HtmlEncode(subject) + "</Subject>" + "<CC>" + cc + "</CC>" + "<BCC>" + bcc + "</BCC>" + "<Body>" + SPEncode.HtmlEncode(body) + "</Body>" + "</EmailMesage>";

                        }

                        else

                        {

                        }

                      

                       // web.Dispose();

                        objSite.Dispose();

                    }

                }); //end run with elevated priv

    Jakob, Please tell me the exact requirment so that we can have the right approach.


    Krishana Kumar http://www.mosstechnet-kk.com

    Tuesday, May 29, 2012 7:50 AM
  • Hi jakob,

    We can create site column by using SharePoint Object model or using XML definition [Element.xml], here is the first type below.

    SPSite objSiteColl = new SPSite(http://spsite);
    SPWeb objWeb
    = ObjSiteColl.OpenWeb();
    string fieldname = objSite.OpenWeb().Fields.Add("SiteName/URL", SPFieldType.URL, true);

    Below is the second approach.

    1. Visual Studio creates an empty SharePoint project for you. When it’s completed, right-click the toplevel project node and select Add New Item.
    2. Select the Empty Element template, Provide a name (for example, MycolumnName) for the file and click Add.
    3. Add the following code to the Elements.xml file that is created in the default project:

    <?xml version=”1.0” encoding=”utf-8”?>
    <Elements xmlns=”http://schemas.microsoft.com/sharepoint/”>
    <Field ID=”{5644d72d-325f-4462-8fd2-09d455f4910e}”
    Type= “URL” AllowDeletion=”FALSE” Description=”SiteCollection Name / URL.”
    Name=”MyCustomCol”
    DisplayName=”SiteCollectionName/SiteURL”
    Group=”CustomColumnGroup”>
    </Field>
    </Elements>

    4. After you’ve completed this, press F6 to build the project. When the project successfully builds, click Build Deploy Solution to deploy the site column to SharePoint.
    5. Navigate to your SharePoint site and click Site Actions Site Settings. Under Galleries, click Site Columns. You should now see a CustomColumnGroup group with a “SiteCollectionName/SiteURL” site column.


    Krishana Kumar http://www.mosstechnet-kk.com

    Tuesday, May 29, 2012 8:05 AM
  • Hi Jakob, this whould work.

    Get-SPWebApplication | Get-SPSite -Limit All | ForEach-Object{
    $url = $_.url
    $users = Get-SPUser -Web $_.URL | ?{$_.isSiteAdmin -eq "$true" -and $_.Email -ne ""}  
    foreach ($user in $users){
        write-host $url,",", $user.DisplayName,",", $user.Email
    }
    }
    Cheers
    Riccardo

    Italian SharePoint Community - My blog - My profile

    Tuesday, May 29, 2012 8:25 AM
  • Hi again

    Thank you for your answers - I really appreciate it :-)

    I will try your solutions and report back here.

    /Jakob

    Tuesday, May 29, 2012 8:55 AM