Solved

Powershell updating lookup field in SharePoint 2010

Posted on 2011-03-24
2
5,400 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 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

What, When and Where - Security Threats from Q1

Join Corey Nachreiner, CTO, and Marc Laliberte, Information Security Threat Analyst, on July 26th as they explore their key findings from the first quarter of 2017.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

635 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