[Note]: This script needs to be run locally on a SharePoint server Example: Querying a SQL Server (easy don't you think?):
$conn = New-Object System.Data.SqlClient.SqlConnection(
"Data Source=Library; Initial Catalog=UsefulWebsites; Integrated Security=SSPI"
)
$conn.Open();
$query =
"select lc.CategoryName,il.DisplayName,il.HyperLink,il.Description,il.Shared,lc.Shared as LCShared from incelinks il join linkscategory lc on il.CategoryId = lc.CategoryId where il.Shared = 1 and lc.shared =1 order by lc.CategoryName"
$dap =
new
-
object
System.Data.SqlClient.SqlDataAdapter($query,$conn);
$dt =
System.Data.DataTable;
$dap.Fill($dt);
foreach
($r
in
$dt.Rows)
{
Write-Host $r[
"DisplayName"
]
}
$conn.Close();
$w = Get-SPWeb
"http://corporate/library"
;
$list = $w.Lists[
"Useful Websites"
];
$categoryField = $list.Fields[
"olCategory"
] -
as
[Microsoft.SharePoint.Taxonomy.TaxonomyField];
$tsId = $categoryField.TermSetId;
$termStoreId = $categoryField.SspId;
$tsession = Get-SPTaxonomySession -Site $list.ParentWeb.Site;
$tstore = $tsession.TermStores[$termStoreId];
$tset = $tstore.GetTermSet($tsId);
$i = $list.Items.Add();
$i[
"Title"
] = $r[
$category = $r[
"CategoryName"
#Check if the term exists
$terms = $tset.GetTerms($category,$
false
);
$term = $
null
if
($terms.Count -eq 0)
#Create the term
Write-Host ([String]::Format(
"Creating Term, {0}"
,$category)) -ForegroundColor DarkYellow;
$term = $tset.CreateTerm($category, $tstore.Languages[0]);
$tstore.CommitAll();
else
#The term exists. Note we are using the first instance of the Term
$term = $terms[0];
#Set the Managed Metadata field
$categoryField.SetFieldValue($i,$term);
$i.Update()
function Get-UsefulWebsitesFromSql
#Connect to the database
Write-Progress -Id 1 -ParentId 0 -Activity
"Importing Data From SQL into SharePoint"
-PercentComplete (1) -Status
"Opening Connection to the SQL Server"
try
#Execute the query
-PercentComplete (2) -Status
"Querying SQL Server"
"select lc.CategoryName,il.DisplayName,il.HyperLink,il.Description from links il join linkscategory lc on il.CategoryId = lc.CategoryId where il.Shared = 1 and lc.shared =1 order by lc.CategoryName"
$w = Get-SPWeb http:
//corporate/library;
$listTitle = $list.Title;
$pi = $dt.Rows.Count;
$pci =1;
$itemsAdded = 0;
-PercentComplete (25/($pi+45)*100) -Status
"Importing ($pi) items into SharePoint."
-PercentComplete (($pci+25)/($pi+45)*100) -Status
Write-Progress -Id 2 -ParentId 1 -Activity
"Adding new items to $listTitle"
-PercentComplete ($pci/$pi*100) -Status
"Importing item $pci into SharePoint."
$pci++;
$itemsAdded++;
#Set the title
#Set the Description Field. Trim the description if it's longer than 255 characters
$description =
(($r[
"Description"
]).ToString().Length -gt 255){($r[
]).ToString().SubString(0,254)}
{($r[
]).ToString()};
] = $description;
#Set the URL field (Hyperlink field)
$hyperLink = $r[
"HyperLink"
(!$hyperLink.ToLower().StartsWith(
"http"
))
$hyperLink = ([String]::Format(
"http://{0}"
,$hyperLink));
Write-Host $hyperLink;
$urlFieldValue = New-Object Microsoft.SharePoint.SPFieldUrlValue;
$urlFieldValue.Description = ($r[
]).Replace(
"&"
,
"and"
$urlFieldValue.Url = $hyperLink;
"URL"
] = $urlFieldValue;
#Set the category field (Managed Metadata)
#Save changes to the item
$i.Update();
"Added item: '{0}', with URL: {1}"
,$r[
],$hyperLink)) -ForegroundColor Green;
catch
[System.Exception]{
" Error adding item. Item {0} has been skipped. Error: {1}. "
],$_)) -ForegroundColor Red -BackgroundColor White;
continue
-PercentComplete (80) -Status
"Closing SQL Connection."
"Finished importing items into the list. Imported {0} items. "
,$itemsAdded)) -ForegroundColor Blue -BackgroundColor White;
-PercentComplete (90) -Status
"Finished importing ($pi) items into SharePoint."
$w.Dispose();
"Error: {0} "
,$_)) -ForegroundColor Red -BackgroundColor White;
-PercentComplete (100) -Status
"An error occured."
finally
#Call the function
Get-UsefulWebsitesFromSql;
Richard Mueller edited Revision 6. Comment: Removed extra space in tag "SharePoint 2010", added tag
Gokan Ozcifci edited Revision 5. Comment: Tag & content & title
Matthew Yarlett edited Original. Comment: Minor change to formatting.
Matthew Yarlett edited Revision 2. Comment: Updated the code examples. Added an additional example. Minor changes to wording.
Matthew Yarlett edited Revision 3. Comment: Updated tags.
Hi Matthew,
Thank you for posting the article!
Do we still need BDC and Secure Store Service application setup for the scripts to work?
Hi Dan,
Thanks for the feedback!
You don't need the Secure Store or BDC/BCS. The connection to the SQL server is direct from the PowerShell instance, so it uses the current users windows identity for authentication (and doesn't experience the "Double-Hop" authentication issue).
You do need to run the script from a PowerShell console on a SharePoint server though.