none
Powershell to update Document set columns data RRS feed

  • Question

  • Hii

    I have a library which has many document sets, and I want to update the data in its columns using a powershell utility as there is a lot of data, but I have to map as per the id and then update its data, I have just tried below and its incomplete. I don't now which properties to update and how to match the id

    function ImportCsvToSPOList($records,$spoCtx)
    {
        try
        {
    
            $SuccessLogs=$records.LogImportSuccess + (get-date -f yyyy-MM-dd_hh_mm_ss).ToString()+".csv"
            $FailureLogs=$records.LogImportFailure + (get-date -f yyyy-MM-dd_hh_mm_ss).ToString()+".csv"
            Add-Content $SuccessLogs “ID,Record”;
            Add-Content $FailureLogs “ID,Message”;
            $count = 0;
            $logcount = 0;
            [Microsoft.SharePoint.Client.Web]$web = $spoCtx.Web
            [Microsoft.SharePoint.Client.List]$list = $web.Lists.GetByTitle($records.LibraryName)
            
            $rootFolder = $list.RootFolder.Folders
    
            $cType = $list.ContentTypes["Entry"]
    
            $query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery(10000, 'Title')
            $items = $list.GetItems( $query )
     
            $spoCtx.load($items)
            $spoCtx.Load($rootFolder)
            $spoCtx.ExecuteQuery();
    
            Write-Host "Starting Data Import" -ForegroundColor Yellow
    
            foreach($documentsetfolder in $rootFolder)
            {
            #read all the values from csv
            foreach($row in $CSVLocation)
            {
                $logcount = $logcount + 1;
            try
            {
                [Microsoft.SharePoint.Client.ListItemCreationInformation]$itemCreateInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation;
                [Microsoft.SharePoint.Client.ListItem]$item = $list.AddItem($itemCreateInfo); 
         
                #importing values of each column using their internal name
    
                if($documentsetfolder.Name -eq $row.'ID')
                {
         
                    $documentsetfolder.Properties['abc'] = $row.abc                
    
                    $list.Update();
                    $spoCtx.ExecuteQuery();
         
                    $count = $count + 1;
                    Write-Host -ForegroundColor Yellow "Number of record(s) inserted - "  $count
                    $line = $count.ToString() + "," + $item["ID"];
                    Add-Content $SuccessLogs $line
                }
            
            }
            catch  [Exception]
            {   
                Write-Host -ForegroundColor Red "An Error Occured"
                $_.Exception.Message
                $line = $logcount.ToString() + "," + $item["ID"]+","+$_.Exception.Message;
                Add-Content $FailureLogs $line
            }
        }
        }
    }
        catch [Exception]
        {
            Write-Host -ForegroundColor Red "An Error Occured"
            $_.Exception.Message
        }
    
    }

    Regards

    Paru


    Paru


    • Edited by Paru Upreti Tuesday, March 6, 2018 12:08 PM code updated
    Tuesday, March 6, 2018 11:17 AM

Answers

  • Hi

    I am able to do this now, below is my updated code

    function ImportCsvToSPOList($records,$spoCtx)
    {
        try
        {
    
            $SuccessLogs=$records.LogImportSuccess + (get-date -f yyyy-MM-dd_hh_mm_ss).ToString()+".csv"
            $FailureLogs=$records.LogImportFailure + (get-date -f yyyy-MM-dd_hh_mm_ss).ToString()+".csv"
            Add-Content $SuccessLogs “ID,Record”;
            Add-Content $FailureLogs “ID,Message”;
            $count = 0;
            $logcount = 0;
            [Microsoft.SharePoint.Client.Web]$web = $spoCtx.Web
            [Microsoft.SharePoint.Client.List]$list = $web.Lists.GetByTitle($records.LibraryName)
            
            $rootFolder = $list.RootFolder.Folders
    
            $cType = $list.ContentTypes["abc Entry"]
    
            $query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery(10000, 'Title')
            $items = $list.GetItems( $query )
     
            $spoCtx.load($items)
            $spoCtx.Load($rootFolder)
            $spoCtx.ExecuteQuery();
    
            Write-Host "Starting Data Import" -ForegroundColor Yellow
    
            foreach($documentsetfolder in $rootFolder)
            {
            #read all the values from csv
            foreach($row in $CSVLocation)
            {
                $logcount = $logcount + 1;
            try
            {
                [Microsoft.SharePoint.Client.ListItemCreationInformation]$itemCreateInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation;
                [Microsoft.SharePoint.Client.ListItem]$item = $list.AddItem($itemCreateInfo); 
         
                #importing values of each coumn using their internal name
    
                if($documentsetfolder.Name -eq $row.'abcID')
                {
       
                    $documentsetfolder.Properties['selectedSiteProduction'] = $row.selectedSiteProduction                
    
                    $rootFolder.Update();
                    $list.Update();
                    $spoCtx.ExecuteQuery();
         
                    $count = $count + 1;
                    Write-Host -ForegroundColor Yellow "Number of record(s) inserted - "  $count
                    $line = $count.ToString() + "," + $item["abcID"];
                    Add-Content $SuccessLogs $line
                }
            
            }
            catch  [Exception]
            {   
                Write-Host -ForegroundColor Red "An Error Occured"
                $_.Exception.Message
                $line = $logcount.ToString() + "," + $item["abcID"]+","+$_.Exception.Message;
                Add-Content $FailureLogs $line
            }
        }
        }
    }
        catch [Exception]
        {
            Write-Host -ForegroundColor Red "An Error Occured"
            $_.Exception.Message
        }
    
    }


    Paru

    • Marked as answer by Paru Upreti Tuesday, March 20, 2018 12:36 PM
    Tuesday, March 20, 2018 12:35 PM

All replies

  • Hi Paru,

    We can use CSOM via C# code instead of PowerShell for the convenience of debugging.

    What are your detailed requirements?

    Do you want update all items in this library?

    How do you want to update the data and what is the updated data?

    Best regards,

    Lee Liu


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Wednesday, March 7, 2018 7:34 AM
  • Hi

    I am able to do this now, below is my updated code

    function ImportCsvToSPOList($records,$spoCtx)
    {
        try
        {
    
            $SuccessLogs=$records.LogImportSuccess + (get-date -f yyyy-MM-dd_hh_mm_ss).ToString()+".csv"
            $FailureLogs=$records.LogImportFailure + (get-date -f yyyy-MM-dd_hh_mm_ss).ToString()+".csv"
            Add-Content $SuccessLogs “ID,Record”;
            Add-Content $FailureLogs “ID,Message”;
            $count = 0;
            $logcount = 0;
            [Microsoft.SharePoint.Client.Web]$web = $spoCtx.Web
            [Microsoft.SharePoint.Client.List]$list = $web.Lists.GetByTitle($records.LibraryName)
            
            $rootFolder = $list.RootFolder.Folders
    
            $cType = $list.ContentTypes["abc Entry"]
    
            $query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery(10000, 'Title')
            $items = $list.GetItems( $query )
     
            $spoCtx.load($items)
            $spoCtx.Load($rootFolder)
            $spoCtx.ExecuteQuery();
    
            Write-Host "Starting Data Import" -ForegroundColor Yellow
    
            foreach($documentsetfolder in $rootFolder)
            {
            #read all the values from csv
            foreach($row in $CSVLocation)
            {
                $logcount = $logcount + 1;
            try
            {
                [Microsoft.SharePoint.Client.ListItemCreationInformation]$itemCreateInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation;
                [Microsoft.SharePoint.Client.ListItem]$item = $list.AddItem($itemCreateInfo); 
         
                #importing values of each coumn using their internal name
    
                if($documentsetfolder.Name -eq $row.'abcID')
                {
       
                    $documentsetfolder.Properties['selectedSiteProduction'] = $row.selectedSiteProduction                
    
                    $rootFolder.Update();
                    $list.Update();
                    $spoCtx.ExecuteQuery();
         
                    $count = $count + 1;
                    Write-Host -ForegroundColor Yellow "Number of record(s) inserted - "  $count
                    $line = $count.ToString() + "," + $item["abcID"];
                    Add-Content $SuccessLogs $line
                }
            
            }
            catch  [Exception]
            {   
                Write-Host -ForegroundColor Red "An Error Occured"
                $_.Exception.Message
                $line = $logcount.ToString() + "," + $item["abcID"]+","+$_.Exception.Message;
                Add-Content $FailureLogs $line
            }
        }
        }
    }
        catch [Exception]
        {
            Write-Host -ForegroundColor Red "An Error Occured"
            $_.Exception.Message
        }
    
    }


    Paru

    • Marked as answer by Paru Upreti Tuesday, March 20, 2018 12:36 PM
    Tuesday, March 20, 2018 12:35 PM