Solved

Powershell to update Sharepoint List - error accessing first list item

Posted on 2013-01-10
7
605 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Set OWA language and time zone in Exchange for individuals, all users or per database.
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now