?
Solved

Powershell to update Sharepoint List - error accessing first list item

Posted on 2013-01-10
7
Medium Priority
?
614 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

765 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