Search This Blog

Thursday, May 31, 2018

simple log file c#


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            LogWriter log = new LogWriter("Time JobExcuted");
            log.LogWrite("customerTable getting data");
        }
    }
    public class LogWriter
    {
        private string m_exePath = string.Empty;
        public LogWriter(string logMessage)
        {
            LogWrite(logMessage);
        }
        public void LogWrite(string logMessage)
        {
            m_exePath = "D:\\CustomLog";
            try
            {
                using (StreamWriter w = File.AppendText(m_exePath + "\\" + "log.txt"))
                {
                    Log(logMessage, w);
                }
            }
            catch (Exception ex)
            {
            }
        }

        public void Log(string logMessage, TextWriter txtWriter)
        {
            try
            {
                txtWriter.Write("\r\nLog Entry : ");
                txtWriter.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
                    DateTime.Now.ToLongDateString());
                txtWriter.WriteLine("  :{0}", logMessage);
                txtWriter.WriteLine("-------------------------------");
            }
            catch (Exception ex)
            {
            }
        }
    }
}


Thursday, May 24, 2018

Send Email using Gmail SMTP Server - C#

SMTP Server: Authentication the Server Response was 5.5.1 Authentication required in gmail

First login into Gmail Account from which Gmail credentials are you using to send email.

Now go to Gmail Settings Feature, it comes in your Gmail Inbox on right side.

Eanble to Security Feature

Now click on Gmail Setting Feature, move to security feature or alternatively can hit/paste this URL in your browser https://www.google.com/settings/security/lesssecureapps where you can see settings. 
Access for less secure apps
1.            Turn off (default) 
2.            Turn On  


using System.Net;
using System.Net.Mail;
   static void Main(string[] args)
        {
          
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";

            NetworkCredential authinfo = new NetworkCredential("test@gmail.com", "testPassword");
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = authinfo;
            smtp.EnableSsl = true;
            smtp.Send("testfrom@gmail.com", "testTo@gmail.com", "test", "testbody");
          
            Console.WriteLine("Sent");
            Console.ReadLine();

        }



Wednesday, May 16, 2018

SharePoint - download all document library files and metadata Using PowerShell


Reference : http://www.nikcraik.ca/sharepoint-powershell-script-to-extract-all-documents-and-their-versions/

# This script will extract all of the documents and their versions from a site. It will also
# download all of the list data and document library metadata as a CSV file.

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
#
# $destination: Where the files will be downloaded to
# $webUrl: The URL of the website containing the document library for download
# $listUrl: The URL of the document library to download

#Where to Download the files to. Sub-folders will be created for the documents and lists, respectively.
$destination = "C:\Export"

#The site to extract from. Make sure there is no trailing slash.
$site = "http://win-2016/"
$docName ="Documents";
# Function: HTTPDownloadFile
# Description: Downloads a file using webclient
# Variables
# $ServerFileLocation: Where the source file is located on the web
# $DownloadPath: The destination to download to

function HTTPDownloadFile($ServerFileLocation, $DownloadPath)
{
       $webclient = New-Object System.Net.WebClient
       $webClient.UseDefaultCredentials = $true
       $webclient.DownloadFile($ServerFileLocation,$DownloadPath)
}

function DownloadMetadata($sourceweb, $metadatadestination)
{
       Write-Host "Creating Lists and Metadata"
       $sourceSPweb = Get-SPWeb -Identity $sourceweb
       $metadataFolder = $destination+"\"+$sourceSPweb.Title+" Lists and Metadata"
       $createMetaDataFolder = New-Item $metadataFolder -type directory
       $metadatadestination = $metadataFolder

       foreach($list in $sourceSPweb.Lists)
       {
        if($list.Title -eq $docName)   #remove this condition you can get all Site Document and list metadata
               {
                      Write-Host "Exporting List MetaData: " $list.Title
                      $ListItems = $list.Items
                      $Listlocation = $metadatadestination+"\"+$list.Title+".csv"
                      $ListItems | Select * | Export-Csv $Listlocation  -Force
         }
       }
}

# Function: GetFileVersions
# Description: Downloads all versions of every file in a document library
# Variables
# $WebURL: The URL of the website that contains the document library
# $DocLibURL: The location of the document Library in the site
# $DownloadLocation: The path to download the files to

function GetFileVersions($file)
{
       foreach($version in $file.Versions)
       {
              #Add version label to file in format: [Filename]_v[version#].[extension]
              $filesplit = $file.Name.split(".")
              $fullname = $filesplit[0]
              $fileext = $filesplit[1]
              $FullFileName = $fullname+"_v"+$version.VersionLabel+"."+$fileext                

              #Can't create an SPFile object from historical versions, but CAN download via HTTP
              #Create the full File URL using the Website URL and version's URL
              $fileURL = $webUrl+"/"+$version.Url

              #Full Download path including filename
              $DownloadPath = $destinationfolder+"\"+$FullFileName

              #Download the file from the version's URL, download to the $DownloadPath location
              HTTPDownloadFile "$fileURL" "$DownloadPath"
       }
}

# Function: DownloadDocLib
# Description: Downloads a document library's files; called GetGileVersions to download versions.
# Credit
# Used Varun Malhotra's script to download a document library
# as a starting point: http://blogs.msdn.com/b/varun_malhotra/archive/2012/02/13/10265370.aspx
# Variables
# $folderUrl: The Document Library to Download
# $DownloadPath: The destination to download to
function DownloadDocLib($folderUrl)
{
    $folder = $web.GetFolder($folderUrl)
    foreach ($file in $folder.Files)
       {
        #Ensure destination directory
              $destinationfolder = $destination + "\" + $folder.Url
        if (!(Test-Path -path $destinationfolder))
        {
            $dest = New-Item $destinationfolder -type directory
        }

        #Download file
        $binary = $file.OpenBinary()
        $stream = New-Object System.IO.FileStream($destinationfolder + "\" + $file.Name), Create
        $writer = New-Object System.IO.BinaryWriter($stream)
        $writer.write($binary)
        $writer.Close()

              #Download file versions. If you don't need versions, comment the line below.
              GetFileVersions $file
       }
}

# Function: DownloadSite
# Description: Calls DownloadDocLib recursiveley to download all document libraries in a site.
# Variables
# $webUrl: The URL of the site to download all document libraries
function DownloadSite($webUrl)
{
       $web = Get-SPWeb -Identity $webUrl

       #Create a folder using the site's name
       $siteFolder = $destination + "\" +$web.Title+" Documents"
       $createSiteFolder = New-Item $siteFolder -type directory
       $destination = $siteFolder

       foreach($list in $web.Lists)
       {
              if($list.BaseType -eq "DocumentLibrary")  
              {
            if($list.Title -eq $docName)   #remove this condition you can get all Site Document and list
                  {
                         Write-Host "Downloading Document Library: " $list.Title
                         $listUrl = $web.Url +"/"+ $list.RootFolder.Url
                         #Download root files
                         DownloadDocLib $list.RootFolder.Url
                         #Download files in folders
                         foreach ($folder in $list.Folders)
                         {
                         DownloadDocLib $folder.Url
                         }
            }
              }
       }
}

#Download Site Documents + Versions
DownloadSite "$site"

#Download Site Lists and Document Library Metadata
DownloadMetadata $site $destination