Solved

Powershell to update Sharepoint List - error accessing first list item

Posted on 2013-01-10
7
607 Views
Last Modified: 2013-01-10
I have a Powershell I am trying to write to update a list. I copied it from another Powershell that is working - the only thing is the original Powershell was for a list within a subsite - i.e. you drill down from main site to dwhcities/Houston and there is a link to Lists on left navigation.

For mine, however, it is for a List available from the main site home page - there is a link to Lists on left navigation there. I am running into problems getting the parameters right for this.

Working powershell:
$site = new-object Microsoft.SharePoint.SPSite("http://xxxxxx.net/dwhcities/Houston/")
$web = $site.OpenWeb("dwhcities/Houston")
$list = $web.lists["Loan Tracking"]

Attempted powershell:
$site = new-object Microsoft.SharePoint.SPSite("http://xxxxxx.net/")
#$web = $site.OpenWeb("lists/") - this did not work
$web = $site.OpenWeb()
$list = $web.lists["DWH Communities"]

Added this line to see value of field COM_ID in the 1st record in List
$item["COM_ID"]

In debug mode this fails with a cryptic message. If I try to step thru, it just gives more messages similar to this. I have tried multiple ways of defining the $site and $web but all give the same message. Can you suggest anything to try? Thanks!

[DBG]>>> Stopped at: if ($_.FullyQualifiedErrorId -ne "NativeCommandErrorMessage" -and $ErrorView -ne "CategoryView") {

Here are the respective site URLs for new list and older list:
http://xxxxxx.net/Lists/DWH%20Communities/AllItems.aspx
http://xxxxxx.net/dwhcities/Houston/Lists/Loan%20Tracking/1%20%20By%20Project.aspx
0
Comment
Question by:davidweekleyhomes
  • 4
  • 3
7 Comments
 
LVL 38

Expert Comment

by:Justin Smith
ID: 38762903
What version of SharePoint?  Guessing 2007.
0
 
LVL 38

Expert Comment

by:Justin Smith
ID: 38762913
Also, I'm assuming you left some code out of your question, but how did you get from $list to $item?

What happens if you just type $list after grabbing the list....does it show info for the DWH list?  That would tell you if you actually grabed an instance of the list.
0
 

Author Comment

by:davidweekleyhomes
ID: 38763082
Yes, 2007. I did as you suggested and it did seem to list a bunch of stuff including this line
Items                            : {Arbor Manors, Foxwood Glen, Mueller - Market, Mueller - Affordable...}
These are community names so it does look like it is getting to the list - good!

So I guess my problem is a different one. I had this other code
$Items = $List.items
foreach ($data in $LotsOfData) {    #note lotsofdata is my sql server stored procedure
$data.Com_id  #this display worked like I thought it would
$item["COM_ID"]   #this line gave me the error

Note, the reason I was displaying the $item line was that this had failed with that same error, so I was trying to isolate the issue
if ($data.Com_id -eq $item["COM_ID"])  {

THANKS!!
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 38

Expert Comment

by:Justin Smith
ID: 38763141
Is your end goal to grab the Item that has a COM_ID equal to the $data.com_Id?

If so.....

$Items = $List.items
foreach ($data in $LotsOfData) {    
foreach ($item in $items | where {$_.Fields["COM_ID"] -eq $data.Com_id){
  <insert code to run>
}
}


________________________________________________________
Follow me on Twitter!  @justinsmith317
0
 

Author Comment

by:davidweekleyhomes
ID: 38763232
I tried this - note, you had a { before $_.Fields - I got an error with this in there, even if I added a closing brace later. I took it out altogether and when I got to this line in debug mode, it gave me the message: Stopped at: if ($_.FullyQualifiedErrorId -ne "NativeCommandErrorMessage" -and $ErrorView -ne "CategoryView") {

I am not familiar with the "$_.Fields " nomenclature, sorry.

My code:
$Items = $List.items
foreach ($data in $LotsOfData) {

foreach ($item in $items | where $_.Fields["COM_ID"] -eq $data.Com_id){
0
 
LVL 38

Accepted Solution

by:
Justin Smith earned 500 total points
ID: 38763327
Should have been foreach ($item in $items | where {$_.Fields["COM_ID"] -eq $data.Com_id}){


The "where" clause needs to be in {brackets}

Also, "$_" just signifies the current item in the foreach loop.  So, where the current item's COM_ID field is equal to $data.Com_id, run code against it.
0
 

Author Closing Comment

by:davidweekleyhomes
ID: 38763536
YEA!  I was finally able to get past my errors. Thanks so much!
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
Synchronize a new Active Directory domain with an existing Office 365 tenant
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question