Friday, June 24, 2011

Find All Incoming E-Mail Enabled Lists and Libraries in SharePoint

During Migration, Needed a List of location where Incoming E-Mail is enabled. So Here is the script which gives that!

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$SPWebApp = Get-SPWebApplication "http://sharepoint.com"
 
<# If it is MOSS 2007, You can use:
$SPWebApp = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup(http://sharepoint.com)
 To get SPWebApplication Object
#>
 
#create a CSV file
"E-Mail,List,Site" > "EMail-Enabled.txt" #Write the Headers in to a text file
 
foreach ($SPsite in $SPwebApp.Sites)  # get the collection of site collections
{ 
    foreach($SPweb in $SPsite.AllWebs)  # get the collection of sub sites
        {
            foreach ($SPList in $SPweb.Lists)
                {
                    if ( ($splist.CanReceiveEmail) -and ($SPlist.EmailAlias) )
                        {
                            # WRITE-HOST "E-Mail -" $SPList.EmailAlias "is configured for the list "$SPlist.Title "in "$SPweb.Url
                            $SPList.EmailAlias + "," + $SPlist.Title +"," + $SPweb.Url >> EMail-Enabled.txt  #append the data
                        }
                }
        }
}

Don't have PowerShell? Want to achieve the same using C# code: (console application).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;

namespace GetEnvironmentDetails
{
    class FindIncomingEMailEnabled
    {
        static void Main(string[] args)
        {
            string site;

            try
            {
                if (args.Length == 0)
                {
                    Console.WriteLine("Enter the Web Application URL:");
                    site = Console.ReadLine();
                }
                else
                {
                    site = args[0];
                }

                //objects for the CSV file generation
                StreamWriter SW;
                SW = File.AppendText("c:\\IncomingEmails.csv");

                //Write the CSV Header
                SW.WriteLine("Site Name, List Name, List URL, E-Mail");
                SPSite tmpRoot = new SPSite(site);
                SPSiteCollection tmpRootColl = tmpRoot.WebApplication.Sites;

                //Enumerate through each site collection
                foreach (SPSite tmpSite in tmpRootColl)
                {
                   //Enumerate through each sub-site
                    foreach (SPWeb tmpWeb in tmpSite.AllWebs)
                    {
                        //Enumerate through each List
                        foreach (SPList tmpList in tmpWeb.Lists)
                        {
                            //Check Incoming Email is enabled
                            if(tmpList.CanReceiveEmail & (!string.IsNullOrEmpty(tmpList.EmailAlias)))
                            {
                            //Log the details to a file
                            SW.WriteLine(tmpWeb.Title.Replace(",", " ") + "," + tmpList.Title.Replace(",", " ") + "," + tmpSite.Url + tmpList.DefaultViewUrl + "," + tmpList.EmailAlias.ToString());
                            }
                        }                 
                    }
                }
                                                
                //Dispose of the bjects
                SW.Close();
            }

            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("Get Incoming Emails Detail", ex.Message);
            } 
        }
    }
}

Alternatively, You can write & run the below SQL Query on each content database, However I wont recommend it (Even with No Lock!)

SELECT Webs.FullUrl, AllLists.tp_Title, AllLists.tp_EmailAlias
FROM AllLists With (NOLOCK)
Inner join webs With (NOLOCK) on AllLists.tp_WebID = Webs.Id
where AllLists.tp_EmailAlias is not null



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

6 comments :

  1. I was searching for this all over the net! Thank you so much.

    ReplyDelete
  2. Hi, this script is giving me a syntax error. Can you repost the script please?

    ReplyDelete
  3. Works great! Thanks

    ReplyDelete
  4. Hi. I was able to run this script successfully the first time but now, it stops at a certain library for an oddly long time but the script never stops executing. Any ideas on this? -Anon

    ReplyDelete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...