Solved

Powershell updating lookup field in SharePoint 2010

Posted on 2011-03-24
2
4,983 Views
Last Modified: 2012-05-11
Using Powershell I have uploaded documents to a SharePoint 2010 document library. In the metadata, for each document, there is a lookup field named "Master". It allows the user to select another document in the library based on another documents Title. This works fine manually.

I know what value goes into Master initially, so I want to set this field in my powershell script...just can't get the syntax right.

I can update other metadata fields OK with the simple syntax of:
$docItem["myColumnName"] = $someValue

But the lookup field throws an error when I do the same thing. TIA
0
Comment
Question by:neptuneit
2 Comments
 
LVL 14

Accepted Solution

by:
KoenVosters earned 500 total points
ID: 35214923
This post explains it for most of the field types:

http://www.powershell.nu/2009/01/08/add-items-to-a-list-in-sharepoint/

For lookup it is:

First Let's create a variable holding the information about the "LookupList".



PS > $SPSite = New-Object Microsoft.SharePoint.SPSite("http://wss"); $OpenWeb = $SpSite.OpenWeb("/"); $LookupList = $OpenWeb.Lists["LookupList"]; $OpenWeb.Dispose(); $SPSite.Dispose()

Next, we need to get the specific Item in the list that we want to Lookup. In this example I'll use the Where-Object to retrieve the Item that
I want. It's important to match on a unique value. If you have 2 values with the same name you could use the -and operator to get a more exact matching.



PS > $LookupItem = $LookupList.Items | Where-Object { $_.Name -match 1 }

The LookupField requires that the Item that we want to Lookup is presented in a specific way: ID;#Name
So lets prepare a variable that's presented correctly.



PS > $Lookup = ($LookupItem.ID).ToString() + ";#" + ($LookupItem.Name).ToString()

Now we can create a new item.



PS > $SPSite = New-Object Microsoft.SharePoint.SPSite("http://wss"); $OpenWeb = $SpSite.OpenWeb("/"); $List = $OpenWeb.Lists["My Custom List"]; $OpenWeb.Dispose(); $SPSite.Dispose()

PS > $Item = $List.Items.Add()
PS > $Item["Title"] = "Title"
PS > $Item["UserField"] = $User1
PS > $Item["TextField"] = $Text
PS > $Item["ChoiceField"] = $Choice
PS > $Item["LookupField"] = $Lookup
PS > $Item.Update()
0
 
LVL 1

Author Closing Comment

by:neptuneit
ID: 35234792
Thanks, that did it!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

15 Experts available now in Live!

Get 1:1 Help Now