Link to home
Start Free TrialLog in
Avatar of davidweekleyhomes
davidweekleyhomesFlag for United States of America

asked on

Powershell to update Sharepoint List - error accessing first list item

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
Avatar of Justin Smith
Justin Smith
Flag of United States of America image

What version of SharePoint?  Guessing 2007.
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.
Avatar of davidweekleyhomes

ASKER

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!!
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
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){
ASKER CERTIFIED SOLUTION
Avatar of Justin Smith
Justin Smith
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
YEA!  I was finally able to get past my errors. Thanks so much!