0

below is the code updating a list using data from CSV file. In this we need to update the Created by column. Below code works but is there a better way to get userLogin based on email address.

 #Importing PowerShell Module
        Import-Module Microsoft.Online.SharePoint.PowerShell


    #Authenticating User to SharePoint Online
       $username = "username" 
       $Adminurl = "TenantAdminSite"
       $siteColUrl = "0365 site"

    #Logging Method for errors
       $DATE = get-date
       $LogPath = "LogPath"
       $LogFileName = "LogFileName.log"

    $FilePath = $LogPath +"\" + $LogFileName
    $logFileCreated = $False
    function write-log([string]$label, [string]$logMsg)
    { 

        if($logFileCreated -eq $False)
        {
            write-host "Creating log file..."
            if((Test-Path -path $LogPath) -ne $True)
            {
                write-host"Provide proper values to LogPath folder" -ForegroundColor Red
            }
            else
            {
                Add-Content -Path $FilePath -Value $logHeader
                $script:logFileCreated  = $True
                write-host "Log file created..."
                [string]$info = [System.String]::Format("[$Date] {0}: {1}",$label, $logMsg)
                Add-Content -Path $FilePath -Value $info
            }
        }
        else
        {
            [string]$info = [System.String]::Format("[$Date] {0}: {1}",$label, $logMsg)
            Add-Content -Path $FilePath -Value $info
        }
     }

    try
    {
       Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
       Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll"
       Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"


       #Bind to site collection
       $password = Read-Host -Prompt "Enter password" -AsSecureString 
       $Context = New-Object Microsoft.SharePoint.Client.ClientContext($siteColUrl)
       $Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username,$Password)
       $Context.Credentials = $Creds
       Write-Host "Successfully connected.." -ForegroundColor Green



       #Retrieve lists
       $web = $Context.Web
       $List = $Context.Web.Lists.GetByTitle("ListName")
       $Users = $Context.Web.SiteUsers
       $Context.Load($Users)
       $Context.Load($web)
       $Context.Load($List)
       $Context.ExecuteQuery()

         $Directory = (Split-Path $MyInvocation.mycommand.path)
         Write-Host $Directory
         $csvFile = Import-Csv $Directory\test.csv
       ____________________________________________________________________________ 
       foreach($line in $csvFile)
       {
        $Url = "";
        $Title = "";
        $userString = "";
        $userEmail = $line.EmailAddress
        Foreach ($User in $Users)
        {      
          if($User.Email -ne "")
            {
              if($User.Email -eq $userEmail)
            {
             $userString = "{0};#{1}" -f $User.ID, $User.LoginName
             $Url = $line.URL
             $Title = $line.Title
             break;
            }  
          }
        } 
    if($userString -ne "") 
       { 
             #Adds an item to the list
             $ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
             $Item = $List.AddItem($ListItemInfo)
             $Item["Author"] = $userString
             $Item["Editor"] = $userString
             $Item["Urlvalue"] = $Url
             $Item["Title"] = $Title
             $Item.Update()
             $Context.ExecuteQuery()
       }
    else 

       {  $userEmail  = $userEmail + " Not found in the Site"
          write-log "Error: " $userEmail 
       }
    }

    }
    catch
    { 
       write-log "Error: " $_.Exception.Message
  • What's the issue ? – Mohamed El-Qassas MVP Aug 18 '16 at 19:14
  • No Issue, works fine. Just need to make the performance better, better way of getting userID from Email Address, the code below the "line" – spStacker Aug 18 '16 at 19:26

Your Answer

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

Browse other questions tagged or ask your own question.