Skip to main content

Copy Outlook data to SharePoint online List using Powershell

I want to copy an outlook email data into a SharePoint online list. To achieve this, we can use CSOM /Power shell.




Here I am showing to copy outlook data to SharePoint List using Power shell.

Step 1 : Create a SharePoint List and columns(Question-Multi line text , CretaedBy - People Picker).

Step 2 : Download and install the below tools.
1.SharePoint Online Management shell - https://www.microsoft.com/en-in/download/details.aspx?id=42038
2. Exchange Web service- EWS API is found at: http://www.microsoft.com/en-us/download/details.aspx?id=28952

Step 3 : Open your Windows Powershell ISE and run the follwing script.

#---Outlook mail box name, Username, password and domain ---#

$MailboxName = "AllHandsQuestions@ftech.com"
$LoginId="venkat.m";
$Password='P@ssw0rd';
$Domain='FTech';

#---Log file location ---#

$downloadDirectory = "C:\OutlookLogs_Test\"
$currentDate = Get-Date -UFormat "%Y%m%d"
$Logfile = "Archiving_"+$currentDate+".txt"
$LogFullPath = "C:\OutlookLogs_Test\"
$LogFilePath = $LogFullPath+ $Logfile
$emailFrom = "AllHandsQuestions@ftech.com"
$emailTo = "AllHandsQuestions@ftech.com"
$UserName="venkat.m@ftech.com"
$SecurePassword="P@ssw0rd"

#--SharePoint Site Collection URL ---#

$SPOSiteUrl ="https://ftech.sharepoint.com/sites/Test"

#--SharePoint List Name --#

$ListTitle= "All Hands Questions"


#--Loading SharePoint APIS
#Import-Module "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.PowerShell.dll"
Add-Type –Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type –Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$Credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $UserName, $SecurePassword

   if(![System.IO.File]::Exists($LogFilePath))
   {
      New-Item  -Path $LogFullPath  -Name $Logfile -ItemType File
    }
 
function Write-Log {
     [CmdletBinding()]
     param(
         [Parameter()]
         [ValidateNotNullOrEmpty()]
         [string]$Message
       
     )
     [string]$logMessage = $(Get-Date -Format "d") + " " + $(Get-Date -Format "T") +"       "+  $Message
     Add-Content -Path $LogFilePath -Value $logMessage 
 } 

 Write-Log -Message "***** ARCHIVING POWERSHELL SCRIPT STARTED *****"

#################PRE REQUISISTE to install ###############################
#
#Load the EWS API and connect to the CAS/EWS
# EWS API is found at: http://www.microsoft.com/en-us/download/details.aspx?id=28952
#
################################################

################################################
#
#Accept any certificates presented by the CAS
#
################################################

## Create a compilation environment
$Provider=New-Object Microsoft.CSharp.CSharpCodeProvider
$Compiler=$Provider.CreateCompiler()
$Params=New-Object System.CodeDom.Compiler.CompilerParameters
$Params.GenerateExecutable=$False
$Params.GenerateInMemory=$True
$Params.IncludeDebugInformation=$False
$Params.ReferencedAssemblies.Add("System.DLL") | Out-Null

$TASource=@'
  namespace Local.ToolkitExtensions.Net.CertificatePolicy{
    public class TrustAll : System.Net.ICertificatePolicy {
      public TrustAll() {
      }
      public bool CheckValidationResult(System.Net.ServicePoint sp,
        System.Security.Cryptography.X509Certificates.X509Certificate cert,
        System.Net.WebRequest req, int problem) {
        return true;
      }
    }
  }
'@
$TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
$TAAssembly=$TAResults.CompiledAssembly

## We now create an instance of the TrustAll and attach it to the ServicePointManager
$TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
[System.Net.ServicePointManager]::CertificatePolicy=$TrustAll

################################################
#
#Load the EWS API and connect to the CAS/EWS
# EWS API is found at: http://www.microsoft.com/en-us/download/details.aspx?id=28952
#
################################################

## Load Managed API dll
Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\1.2\Microsoft.Exchange.WebServices.dll"
$ModuleDllPath="C:\Program Files\Microsoft\Exchange\Web Services\1.2\Microsoft.Exchange.WebServices.dll"
 Import-Module -Name:$ModuleDllPath -ErrorAction:Stop

## Set Exchange Version (Exchange2010, Exchange2010_SP1 or Exchange2010_SP2)

$ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2

## Create Exchange Service Object
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)

## Set Credentials to use two options are availible Option1 to use explict credentials or Option 2 use the Default (logged On) credentials
#Credentials Option 1 using UPN for the windows Account
$creds = New-Object System.Net.NetworkCredential($LoginId,$Password,$Domain)
$service.Credentials = $creds 

#Credentials Option 2
$service.UseDefaultCredentials = $true

## Set the URL of the CAS (Client Access Server) to use two options are availbe to use Autodiscover to find the CAS URL or Hardcode the CAS to use

#CAS URL Option 1 Autodiscover
$service.AutodiscoverUrl($MailboxName,{$true})
"Using CAS Server : " + $Service.url

#CAS URL Option 2 Hardcoded  
$uri=[system.URI] $Service.url
$service.Url = $uri 
Write-Log -Message ("GET Exchange Web service URL "+  $service.Url)


#Bind to the Inbox folder
$Sfha = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::HasAttachments, $false)
$folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName) 
$Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid) 

#Get the ID of the folder to move to 
$fvFolderView =  New-Object Microsoft.Exchange.WebServices.Data.FolderView(100) 
$fvFolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Shallow;



  Write-Log -Message "GET PROCESSED FOLDER"
 
####################################################################################################
#
#This section finds attachments and copies the attachment to the download directory
#
####################################################################################################


$ivItemView = New-Object Microsoft.Exchange.WebServices.Data.ItemView(10)

$psPropset = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.ItemSchema]::MimeContent)
$psPropsetSubject = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.ItemSchema]::Subject)
$psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$psPropertySet.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::Text;


$errorMessage =''
$attachmentCount = 0;
$findItemsResults = $Inbox.FindItems($ivItemView)
$service.LoadPropertiesForItems($findItemsResults,$psPropertySet)

#Connection Sharepoint online
$creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,$SecurePassword);
$Context= New-Object Microsoft.SharePoint.Client.ClientContext($SPOSiteUrl);         
$Context.Credentials=$creds;
$testWeb = $Context.Web;
$Context.Load($testWeb);
$Context.Load($testWeb.Lists);
$Context.ExecuteQuery();
$tuitionFeed=$testWeb.Lists.GetByTitle($ListTitle)
$Context.Load($tuitionFeed)
$Context.ExecuteQuery()
Write-Host "Credentials verified successfully!!"

foreach($miMailItems in $findItemsResults.Items){
try{
       
       
            $subject = $miMailItems.Subject.ToString()
            $body = $miMailItems.Body.Text
            $sender = $miMailItems.Sender.Address
            $from = $miMailItems.From.Name
           
            Write-Log -Message ("Downloading E-Mail: " + $subject)           
            $lici =New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
            $listItem = $tuitionFeed.AddItem($lici)
            $listItem["Question"]=$body  

            $listItem["CreatedBy"] = $testWeb.EnsureUser($from)
                    
            $listItem.Update()
          

           $Context.ExecuteQuery()
        #   $miMailItems.IsRead=$true
        #   $miMailItems.Update(Microsoft.Exchange.WebServices.Data.
       
    }
     catch [System.Net.WebException],[System.Exception]
     {
        $ErrorMessage = $_.Exception.Message
        Write-Log -Message $_.Exception.Message

Write-Log -Message "***** ERROR OCCURED *****"
        $failedCount++;
        $ErrorMessage = $_.Exception.Message
        Write-Log -Message $_.Exception.Message       
        Write-Log -Message "***** ERROR CLOSED *****"
        $errorMessage +=$_.Exception.Message
     }
   
   
}
$comments=''
try
{   
$message ='';
   if($errorMessage -eq '')
{
$message="Job result: Succeeded"
}
else
{
$message="Job result: Failed"
$comments = "`nError Message: " +$errorMessage

}

     
     
        Write-Log -Message "***** SENDING CONFIRMATION EMAIL *****"
       
       # $smtpServer = "mail.ftech.com "
       # $subject = "EFT Invoice mailbox archive job report"
       # $body = $message +"`nComments: "+$comments
       # Send-MailMessage -To $emailTo -From $emailFrom -Subject $subject -Body $body -SmtpServer $smtpServer
        Write-Log -Message "***** SENT CONFIRMATION EMAIL *****"

}
     catch [System.Net.WebException],[System.Exception]
     {
        Write-Log -Message "***** ERROR OCCURED *****"
         
        $ErrorMessage = $_.Exception.Message
        Write-Log -Message $_.Exception.Message       
        Write-Log -Message "***** ERROR CLOSED *****"
     }

Write-Log -Message "***** ARCHIVING POWERSHELL  SCRIPT END *****"


Step 4: Run the above script and followed by output.


Step 5 : Check SharePoint List now.

Happy Coding..!



Comments

Post a Comment

Popular posts from this blog

Tab Control in Asp.Net

Scenerio: I need your help in designing tab control in   asp.net .My requirement is I need a tab control in   asp.net (C#) like  for example goto my computer ,right click c drive and select properties.we get tabs like general,security etc....... like that i need to design one tab control(no need of any rightclick) in my web page and the database table columns should come as tabs and inseide the tab data of that particular column should come.   Example:Employee master tab1:Employee name.........his name in the tab tab2:Age.............his age tab3:Address........his address   Solution:   You can do this using a simple div <style type="text/css"> .tabs         {             position: relative;             height: 20px;             margin: 0;   ...

AI and Microsoft: Revolutionizing Efficiency in Nonprofit Organizations

  How AI and Microsoft Enhance Efficiency in Nonprofit Organizations In today’s fast-paced world, nonprofit organizations face unique challenges—limited resources, increasing demands, and the constant need to do more with less. But what if technology could be the game-changer they need? In my latest research paper,  "How AI and Microsoft Enhance Efficiency in Nonprofit Organizations" , I explore how cutting-edge technologies like Artificial Intelligence (AI) and Microsoft’s innovative tools are revolutionizing the way nonprofits operate. From streamlining administrative tasks to enhancing donor engagement and optimizing resource allocation, AI and Microsoft’s solutions are empowering nonprofits to focus on what truly matters—their mission. This paper dives deep into real-world examples, practical applications, and the transformative potential of these technologies. Whether you’re a nonprofit professional, a tech enthusiast, or simply curious about the intersection of technolo...

Social tagging overview in Sharepoint 2010

A tag is a word or phrase that identifies an individual piece of information according to a set of attributes or criteria. Tags make it easy to find and share information about a specific subject or task. Social tagging helps users categorize information in ways that are meaningful to them. Social tagging can improve the quality of search results by filtering against specific tags, and it can also connect individuals who want to share information with other users who have like interests. This article describes the social tagging features in Microsoft SharePoint Server 2010. This article does not describe how to configure social tagging features. It also does not discuss how to implement social tagging features as part of an overall social media strategy for an enterprise. About using social tagging features Social tagging features help users to share information and to retrieve relevant, high-quality content more efficiently. Such sharing encourages collaboration and b...