Solved

Powershell updating lookup field in SharePoint 2010

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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Suggested Solutions

Synchronize a new Active Directory domain with an existing Office 365 tenant
When using a search centre, I'm going to show you how to configure Sharepoint's search to only return results from the current site collection. Very useful when using Office 365 with multiple site collections.
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ā€¦

752 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