January 13, 2012

Powershell to Migrate Data From CSV


clear
Add-PSSnapin microsoft.sharepoint.PowerShell
$FilePath = "C:\Path\FAQ_List.csv"
$listurl="http://siteurl/Lists/FAQList";
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null;
$site=new-object Microsoft.SharePoint.SPSite($listurl);
$web=$site.openweb();
$list=$web.GetList($listurl);
$csv_file = Import-Csv $FilePath -Header("FAQ_ID","FAQ_Title","FAQ_PL","FAQ_Category","FAQ_Topic","FAQ_Answer");
$TaxSession =  Get-SPTaxonomySession -Site "centraladminurl"
$TermStore = $TaxSession.TermStores["Managed Metadata Service"]
$TermStoreGroup = $TermStore.Groups["GroupName"]
$TermSet1 = $TermStoreGroup.TermSets["Product Lines1"]
$TermSet2 = $TermStoreGroup.TermSets["FAQ Categories1"]
#foreach ($line in $csv_file)
for($i=0;$i -le $csv_file.Count-2; $i++)
{
 $line = $csv_file[$i];
 $PL = ('#' + $line.FAQ_PL).Split(';');
 $FC = ('#' + $line.FAQ_Category).Split(';');
 $newItem = $list.Items.Add();
 $targetPLField = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$newItem.Fields["Product Lines"]
 $targetFCField = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$newItem.Fields["FAQ Category"]
 $taxPLCollection = new-object Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection($targetPLField)
 for($k=0;$k -le $PL.Count-1; $k++)
 {
$taxonomyPLFieldValue = new-object Microsoft.SharePoint.Taxonomy.TaxonomyFieldValue($targetPLField)
if($TermSet1.GetTerms($PL[$k].Substring("1"),$true).Count -ne 0)
{
$taxonomyPLFieldValue.TermGuid = $TermSet1.GetTerms($PL[$k].Substring("1"),$true)[0].id
$taxonomyPLFieldValue.Label = $TermSet1.GetTerms($PL[$k].Substring("1"),$true)[0].Name
$taxPLCollection.Add($taxonomyPLFieldValue)
}
 }
 $targetPLField.SetFieldValue($newItem, $taxPLCollection)
 $taxFCCollection = new-object Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection($targetFCField)
 for($l=0;$l -le $FC.Count-1; $l++)
 {
$taxonomyFCFieldValue = new-object Microsoft.SharePoint.Taxonomy.TaxonomyFieldValue($targetPLField)
if($TermSet2.GetTerms($FC[$l].Substring("1"),$true).Count -ne 0)
{
$taxonomyFCFieldValue.TermGuid = $TermSet2.GetTerms($FC[$l].Substring("1"),$true)[0].id
$taxonomyFCFieldValue.Label = $TermSet2.GetTerms($FC[$l].Substring("1"),$true)[0].Name
$taxFCCollection.Add($taxonomyFCFieldValue)
}
 }
 $targetFCField.SetFieldValue($newItem, $taxFCCollection)
 $newItem["Title"] = $line.FAQ_Title;
 $newItem["FAQ Topic"] = $line.FAQ_Topic;
 $newItem["Answer"] = $line.FAQ_Answer;
 $newItem.Update();
}
Remove-PSSnapin microsoft.sharepoint.PowerShell

http://ravendra.wordpress.com/uploading-csv-excel-data-into-sharepoint-list/
http://www.wapshere.com/missmiis/powershell-retrieving-data-from-excel

No comments:

SonarQube with Jenkins Setup using Docker Images

https://funnelgarden.com/sonarqube-jenkins-docker/  https://medium.com/@hakdogan/an-end-to-end-tutorial-to-continuous-integration-and-con...