Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell updating lookup field in SharePoint 2010

Posted on 2011-03-24
2
Medium Priority
?
5,515 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
[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
2 Comments
 
LVL 14

Accepted Solution

by:
KoenVosters earned 2000 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

722 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