Import-csv Script to check values

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 7 months ago.

  • Author
    Posts
  • #6426

    by bronyx85 at 2013-04-04 02:12:39

    Hi,

    I have the following script which successfully imports users information from a .csv file into a SP List 🙂

    # Check if the Sharepoint Snapin is loaded already, and load if not
    if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
    {
    Add-PSSnapin Microsoft.SharePoint.PowerShell
    }

    #Setting our variables, Site name, List name, file to import and Caml
    $spWeb = Get-SPWeb -Identity "my url"
    $list = $spWeb.Lists["My list"]
    $csv = Import-Csv C:\Users\user\Desktop\MyFile.csv
    $caml=""

    #sets up to remove current items from list
    $query=new-object Microsoft.SharePoint.SPQuery
    $query.ViewAttributes = "Scope='Recursive'"
    $query.Query=$caml
    $items=$list.GetItems($query)

    #removes current items from list
    #$items | % { $list.GetItemById($_.Id).Delete() }

    #adds Report to SharePoint List
    foreach ($row in $csv)
    {
    $item = $list.Items.Add();
    $item["Name"] = $row.Name;
    $item["Surname"] = $row.Surname;
    $item.Update();
    }

    #Dispose of SPWeb, to keep things clean and no memory leaks
    $spweb.Dispose()

    The issue I have is that users details may need to change, i.e Address, or surname etc..

    So presuming the SP list has 100+ records in it, I produce a csv file which contains 103 items within it, 3 of course which are new entries which need to be added to the sp list, and 10 of which need to be updated due to address changes.

    How can I incorporate something along those lines into my script so that

    a)The Poweshell Script checks the records in the SP List and matches them with the CSV file, if there are any changes in the CSV File, then update the SharePoint List with the changes in the CSV file. If the SharePoint list record matches the csv record, then do not update that record whilst at the same time adding new records if that record does not exist in the SP list.

    Hope that makes sense.

    Thanks

    by AlexBrassington at 2013-04-04 08:08:52

    It's pretty simple. Do a foreach on the rows in the CSV, in the List.items try to get an item where the name equals the name for the row of the csv. If none exists then add the csv row as a new item.
    If it does exist then check each of the properties in the item against the matching item in the csv. If any differ then correct them and update the item.

    by bronyx85 at 2013-04-04 08:19:35

    Can you please start me off?

    I currently have a for each which looks at the column name and the csv column name, and if they match, it imports that csv record into the list.

    foreach ($row in $csv)
    {
    $item = $list.Items.Add();
    $item["Name"] = $row.Name;
    $item["Surname"] = $row.Surname;

    }

    Doesn't some sort of If Statement have to go in there, to say If $item["Name] = $row.Name;
    item.update

    Can you start me off, im sure il be able to work it out after.
    Many thanks Alex

You must be logged in to reply to this topic.