Solved

Powershell to update Sharepoint List - error accessing first list item

Posted on 2013-01-10
7
603 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

757 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

20 Experts available now in Live!

Get 1:1 Help Now