{"id":123,"date":"2012-09-15T14:47:20","date_gmt":"2012-09-15T19:47:20","guid":{"rendered":"https:\/\/sharepointhacker.com\/?p=123"},"modified":"2012-09-14T14:52:06","modified_gmt":"2012-09-14T19:52:06","slug":"update-a-sharepoint-list-from-a-csv-file","status":"publish","type":"post","link":"https:\/\/sharepointhacker.com\/?p=123","title":{"rendered":"Update a SharePoint List from a CSV File"},"content":{"rendered":"<p>So in the last post over on http:\/\/SysAdminNightmare.com <a href=\"http:\/\/sysadminnightmare.com\/?p=66\"><here><\/a> I created a CSV file for all the domain computers that had a Bitlocker key assigned to it.<\/p>\n<p>Going with the work smarter not harder mantra this post will take that CSV file and update a list on our sharepoint site so I can create a graph.<\/p>\n<p>Note that this must be run from a location where the Sharepoint Snap in is installed, this is usually where Sharepoint is installed so I would save this to a common drive and run from my app server.<\/p>\n<p>UpdateList.ps1<\/p>\n<p>[code]<\/p>\n<p># Check if the Sharepoint Snapin is loaded already, and load if not<br \/>\nif ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )<br \/>\n{<br \/>\nAdd-PSSnapin Microsoft.SharePoint.PowerShell<br \/>\n}<\/p>\n<p>#Setting our variables, Site name, List name, file to import and Caml<br \/>\n$spWeb = Get-SPWeb -Identity &#8220;https:\/\/<yoursite>\/Departments\/IT\/&#8221;<br \/>\n$list = $spWeb.Lists[&#8220;Bitlocker&#8221;]<br \/>\n$csv = Import-Csv Q:\\BitLockerComputerReport.csv<br \/>\n$caml=&#8221;&#8221;<\/p>\n<p>#sets up to remove current items from list<br \/>\n$query=new-object Microsoft.SharePoint.SPQuery<br \/>\n$query.ViewAttributes = &#8220;Scope=&#8217;Recursive'&#8221;<br \/>\n$query.Query=$caml<br \/>\n$items=$list.GetItems($query)<\/p>\n<p>#removes current items from list<br \/>\n$items | % { $list.GetItemById($_.Id).Delete() }<\/p>\n<p>#adds Report to SharePoint List<br \/>\nforeach ($row in $csv)<br \/>\n{<br \/>\n$item = $list.Items.Add();<br \/>\n$item[&#8220;Name&#8221;] = $row.Name;<br \/>\n$item[&#8220;OS&#8221;] = $row.OperatingSystem;<br \/>\n$item[&#8220;Location&#8221;] = $row.Location;<br \/>\n$item[&#8220;Bitlocker&#8221;] = $row.HasBitlockerRecoveryKey;<br \/>\n$item[&#8220;Virtual&#8221;] = $row.adminDescription;<br \/>\n$item.Update();<br \/>\n}<\/p>\n<p>#Dispose of SPWeb, to keep things clean and no memory leaks<br \/>\n$spweb.Dispose()<\/p>\n<p>[\/code]<\/p>\n<p>Again, you need to change your site name, list name and make sure your list has the correct columns. I would task this to run after the script that creates the CSV file.<\/p>\n<p>Next I have one more post in this series where I take the raw csv file and upload to a sharepoint library incase the bosses want to play around with the numbers (as they always do).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So in the last post over on http:\/\/SysAdminNightmare.com I created a CSV file for all the domain computers that had a Bitlocker key assigned to it. Going with the work smarter not harder mantra this post will take that CSV file and update a list on our sharepoint site so I can create a graph. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[7,11,4],"tags":[30,19,31],"class_list":["post-123","post","type-post","status-publish","format-standard","hentry","category-admin","category-powershell","category-sharepoint-2010","tag-bitlocker","tag-powershell-2","tag-sharepoint"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p289k1-1Z","jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/sharepointhacker.com\/index.php?rest_route=\/wp\/v2\/posts\/123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sharepointhacker.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sharepointhacker.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sharepointhacker.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sharepointhacker.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=123"}],"version-history":[{"count":4,"href":"https:\/\/sharepointhacker.com\/index.php?rest_route=\/wp\/v2\/posts\/123\/revisions"}],"predecessor-version":[{"id":127,"href":"https:\/\/sharepointhacker.com\/index.php?rest_route=\/wp\/v2\/posts\/123\/revisions\/127"}],"wp:attachment":[{"href":"https:\/\/sharepointhacker.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sharepointhacker.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sharepointhacker.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}