Updating/setting multiple values in lookup field through powershell

I'm trying to set multiple values to lookup column. I can get it to set one value but multiple values don't work
see below
[Microsoft.SharePoint.SPFieldLookupValueCollection] $itemValues = New-Object Microsoft.SharePoint.SPFieldLookupValueCollection
[Microsoft.SharePoint.SPFieldLookupValue] $lookupvalue = New-Object Microsoft.SharePoint.SPFieldLookupValue
$lookupvalue.LookupId = 1
$lookupvalue.LookupId = 2
$itemValues.Add($lookupvalue)
$CMRSItems["Event Type"] = $itemValues
$CMRSItems.Update()
I was expecting the LookupID 1 and 2 to update but it doesn't. When i specify LookupId = 1 it works
What am i doing wrong.
Thanks in Advance

Hi Patrick, 
In your script above you're using the same object to set both ID's on. Setting the ID of $lookupvalue, then adding it to the lookupvaluecollection, before changing the ID value to 2, then adding that to the collection. 
I wrote an article on setting fields using PowerShell, but it doesn't include multiple lookup values. It does contain multiple choice and taxonomy values though, which is a similar concept. It's here if you want to take a look: http://social.technet.microsoft.com/wiki/contents/articles/20831.sharepoint-a-complete-guide-to-getting-and-setting-fields-using-powershell.aspx
Regards, Matthew
MCPD | MCITP
My Blog
View
Matthew Yarlett's profile
See my webpart on the TechNet Gallery that allows administrative users to upload, crop and format user profile photos. Check it out here:
Upload and Crop User Profile Photos

Similar Messages

  • Issues updating/setting multi value lookup columns via powershell

    Hi All,
         I have an issue updating multi values in a lookup field via powershell
    I can update a single value  lookup field as below but can't get to update if its multi value.
    As stated below when I hardcode it. It works.
    No idea what 'm missing. Any help will be appreciated.
    #Hardcoded works below as you can see i'm setting 3 values
    $array = @($realval.Split(';'))
    for ($i = 0; $i -lt $array.Count - 1; $i += 2)
    $word = $array[$i].Trim('#')
    $number = $array[$i+1].Trim('#')
    "$number $word"
    $lookupvalue1 = GetLookUpValues -val $number
    [Microsoft.SharePoint.SPFieldLookupValueCollection] $itemValues = New-Object Microsoft.SharePoint.SPFieldLookupValueCollection
    [Microsoft.SharePoint.SPFieldLookupValue] $lookupvalue = New-Object Microsoft.SharePoint.SPFieldLookupValue
    [Microsoft.SharePoint.SPFieldLookupValue] $lookupvalue2 = New-Object Microsoft.SharePoint.SPFieldLookupValue
    [Microsoft.SharePoint.SPFieldLookupValue] $lookupvalue3 = New-Object Microsoft.SharePoint.SPFieldLookupValue
    $lookupvalue.LookupId = 1
    $lookupvalue2.LookupId = 2
    $lookupvalue3.LookupId = 6
    $itemValues.Add($lookupvalue)
    $itemValues.Add($lookupvalue2)
    $itemValues.Add($lookupvalue3)
    #$itemValues.Add($lookupvalue)
    $CMRSItems["Event Type"] = $itemValues;
    Write-Host "items:" $itemValues
    $CMRSItems.Update()
    # This works when its updating only one value but when it needs to update multivalue it only updates the last one
    #so for example with the lookupvalue above only 6 gets updated below
    $array = @($realval.Split(';'))
    for ($i = 0; $i -lt $array.Count - 1; $i += 2)
    $word = $array[$i].Trim('#')
    $number = $array[$i+1].Trim('#')
    #$number
    "$number $word"
    #send param to GetLoolValues func to return records as SPFieldLookupValue
    $lookupvalue1 = GetLookUpValues -val $number
    #I can view the lookupvalue returned successfully
    #Write-Host $lookupvalue1
    [Microsoft.SharePoint.SPFieldLookupValueCollection] $itemValues = New-Object Microsoft.SharePoint.SPFieldLookupValueCollection
    #This LookupId returns 3 values like on the hardcoded one above like so :1,2,6
    $lookupvalue.LookupId = $number
    $itemValues.Add($lookupvalue)
    $CMRSItems["Event Type"] = $itemValues;
    #I can view the items returned successfully
    Write-Host "items:" $itemValues
    $CMRSItems.Update()

    The problem I can see with your code is that the below line of code, you are instantiating inside the for loop. This should have been outside the for loop as by keeping it inside the loop you are overriding the value.
    [Microsoft.SharePoint.SPFieldLookupValueCollection] $itemValues = New-Object Microsoft.SharePoint.SPFieldLookupValueCollection
    Please have a look at the below solution and modify your code as per your requirement. What I am trying to achieve in the below code is that I have a listA in which one of the field is being used as a multi-lookup in my listB.
    $lookupCollection = $something.split(";")
    $LookupMasterList=$web.Lists["ListA"]
    [Microsoft.SharePoint.SPFieldLookupValueCollection] $lookupValueCollection = New-Object Microsoft.SharePoint.SPFieldLookupValueCollection
    #Get the Lookup Item from Parent List
    foreach($item in $lookupCollection){
    IF([string]::IsNullOrEmpty($item.trim())) {
    continue;
    $LookupItem = $LookupMasterList.Items | Where-Object { $_.Item("FieldInternalName") -eq $item.trim()}
    if($LookupItem -ne $null)
    $myLookup = New-Object Microsoft.Sharepoint.SPFieldLookupValue($LookupItem.ID,$item.trim())
    $lookupValueCollection.Add($myLookup);
    #Set the Lookup field value
    if([string]::IsNullOrEmpty($lookupValueCollection)){
    continue;
    else{
    $newItem["Lookupfieldinternalname"] = $lookupValueCollection
    The above logic has no hard coding and it fetches the lookup information directly from the master list and generates a collection based on that. You can modify the above code as per your requirement.
    Geetanjali Arora | My blogs |

  • How to Insert more than one record at a time- with fixed set of values in one field

    Can someone guide as to how to insert multiple records at a time using ASP VBScript in Dreamweaver CS4 or ADDT.
    If someone can guide then the exact problem is given below.
    I have a MS access database with one table. The table has three fields. The first field is an autonumber field for ID number.
    The second field contains string values- One out of 7 predefined values. One set of records consists of 7 records containing all the seven types of predefined values in fields no 1.
    The third field is a numeric field and can contain integers.
    I want that the user can enter data for the table using an ASP form in such a way that he enters one set of records at a time in one single screen. This way he will not have to remember that he has /has not entered all the seven set of values for the field no 1.
    I am not creating fields with the 7 types of field1value as their names as it will increase the number of fields drastically.
    Please help for dreamweaver ASP VBScript.

    I have successfully inserted seven records in one form submit operation by looping through the command object execute method.
    However, the data that is being inserted by the command object is repetition of the first record and the command object is not taking any data from the text boxes for the second record onwards. In this I had used the isert record behavious generated by DW CS4 and modified it to suit my requirement. However, the data inserted in the first row is getting repeated in all the seven rows.
    Please help.
    Also advise if there are any free dreamweaver server side validation extensions available.

  • Reading values from lookup columns through custom workflow in SharePoint 2013

    We are able to read the values of text, number columns through custom workflow (via coding) in SharePoint 2013. However, we are not able to read values from lookup columns. So, request anyone to provide help on this.
    Thanks & regards,
    Aditya

    Hi,
    According to your post, my understanding is that you want to read values from lookup columns through custom workflow in SharePoint 2013.
    Since the workflow just doesn't get lookup fields, let's give it something static to work with instead. If we can capture the ID of the lookup field and store that as a static value in our list, the workflow can happily use that to look up our related.
    For more information, you can refer to:
    SharePoint 2013 Workflows and Lookup Columns
    Thanks,
    Linda Li                
    Forum Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact
    [email protected]
    Linda Li
    TechNet Community Support

  • Invalid data has been used to update the list item. The field you are trying to update may be read only (Lookup Field).

    Hi.
    I am getting below error while adding value to look-up field.
    Invalid data has been used to update the list item. The field you are trying to update may be read only.
    I have tried many forums ans post but didn't come to know what's the root cause of issue. I am also posting Code for creating and adding lookup field.
    CAML to create lookup field (It works Fine)
    string lkproductNumber = "<Field Type='Lookup' DisplayName='Product Number' StaticName='ProductNumber' ReadOnly='FALSE' List='" + pNewMaster.Id + "' ShowField='Product_x0020_Number' />";
    Code to insert value to lookup field
    ClientContext client = new ClientContext(SiteUrl);
    client.Load(client.Web);
    client.Credentials = new NetworkCredential(this.UserName, this.Password, this.Domain);
    // Lookup Lists
    List pmList = client.Web.Lists.GetByTitle("Product_Master");
    //List Conatining Lookup Columns
    List piList = client.Web.Lists.GetByTitle("Product_Inventory");
    client.Load(piList);
    query.ViewXml = "<View/>";
    ListItemCollection collection = pmList.GetItems(query);
    client.Load(collection);
    client.ExecuteQuery();
    int prodid=0;
    foreach (ListItem item in collection)
    if (Convert.ToString(item["Product_x0020_Number"]) == ProductNumber)
    { prodid = Convert.ToInt32(item["ID"]); }
    ListItem piItem = piList.AddItem(new ListItemCreationInformation());
    piItem["Product_x0020_Number"] = new FieldLookupValue() { LookupId = prodid };
    piItem.Update();
    client.ExecuteQuery();
    Exception Detail
    Microsoft.SharePoint.Client.ServerException was caught
    Message=Invalid data has been used to update the list item. The field you are trying to update may be read only.
    Source=Microsoft.SharePoint.Client.Runtime
    ServerErrorCode=-2147352571
    ServerErrorTypeName=Microsoft.SharePoint.SPException
    ServerStackTrace=""
    StackTrace:
    at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream)
    at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()
    at WebServiceProviders.ClientServices.NewProductInventory() in Z:\.............ClientServices.cs:line 889
    InnerException:
    Quick response is highly appreciated.
    Thanks
    Mehar

    Try some thing like below,
    your data value that needs to be update should be in this format "ID of the lookup";#"Title of the Lookup" 
    For example,
    listItem["Product_x0020_Number"]
    = "1;#iPhone";
    listItem["Product_x0020_Number"]
    = "2;#Mobile";
    Hope this helped you....

  • Setting the value of a field based on a dropdown list

    I am using the latest production release of JHeadstart 10.1.3.0.91. I am trying to set the value of a field based on selecting the value of another field (drop down list). The drop down list field has the following attributes set autoSubmit="true" immediate="true" valueChangeListener="#{jhsPageLifecycle.updateModelValue}".
    The other field has the partialtrigger set to the first field. ie "depends on" selection from JHeadStart file. The value of the second field is set in the setter of the VO RowImpl java file.
    The value of that field is only populated on the screen if it is set to disabled="true". This seems a bit bizzare behaviour. Can you explain why it cannot set the value of the field when it is not disabled.

    Worked out that if i set the "Clear/Refresh value" attribute on the field that i want updated then it will work ok
    Alan

  • How do you set multiple values in "Internal-Properties" in XSLT

    Hi All,
    I'm running SOA 11g with the Oracle B2B Adapter.
    I have a need to dynamically set the InterchangeReceiverID, InterchangeSenderID, GroupSenderID, and GroupReceiverID from within SOA prior to passing the xml to the B2B Adapter. When using the Transform Activity, I don't understand how to set multiple values at the Interchange/Group levels. I am able to set ONE value at each level using the structure below, but have not been able to determine the proper way to set more than one value at each level.
    Any help would be appreciated.
    thanks,
    bw
    <ns4:Internal-Properties>
    <ns4:Data-Structure>
    <xsl:attribute name="Name">
    <xsl:text disable-output-escaping="no">Interchange</xsl:text>
    </xsl:attribute>
    <ns4:Lookup>
    <xsl:attribute name="Name">
    <xsl:text disable-output-escaping="no">InterchangeSenderID</xsl:text>
    </xsl:attribute>
    <xsl:text disable-output-escaping="no">1234</xsl:text>
    </ns4:Lookup>
    <ns4:Data-Structure>
    <xsl:attribute name="Name">
    <xsl:text disable-output-escaping="no">Group</xsl:text>
    </xsl:attribute>
    <ns4:Lookup>
    <xsl:attribute name="Name">
    <xsl:text disable-output-escaping="no">GroupSenderID</xsl:text>
    </xsl:attribute>
    <xsl:text disable-output-escaping="no">5678</xsl:text>
    </ns4:Lookup>
    <ns4:Data-Structure>
    <xsl:attribute name="Name">
    <xsl:text disable-output-escaping="no">Transaction</xsl:text>
    </xsl:attribute>
    <ns4:Lookup>
    <xsl:attribute name="Name">
    <xsl:text disable-output-escaping="no">TransactionID</xsl:text>
    </xsl:attribute>
    <xsl:text disable-output-escaping="no">856</xsl:text>
    </ns4:Lookup>
    </ns4:Data-Structure>
    </ns4:Data-Structure>
    </ns4:Data-Structure>
    </ns4:Internal-Properties>

    Hi Anuj,
    Thanks for taking time to consider this. I think I did not explain my problem clearly. I'm not trying to assign more than one value to any one property. My issue is that I cannot figure out the proper syntax within the XSLT to assign a value to more than one property at the Interchange level. For instance, do you have an example of how I would assign one value to the InterchangeSenderID and another value to the InterchangeReceiverID? Constant values (rather than dynamic ones) would be fine for an example. Thanks again for your time.
    thanks,
    bw

  • How to set multiple values in one context-attribute

    Hi all,
    Anybody knows a possibility to set multiple Values to a context-attribute?
    I know it how to get it with the following code:
    String break[]= request.getParameterValues("break");
    Now I want to do something like:
    request.setParameterValues(break[no]);
    where no is a counter in a loop.
    With the Method setAttribute(), I overwrite the previous inserted value.
    Thanx
    Robert

    I have not explizit declared break as an array.
    It is the Context- Attribute I want to send. I thaught that I can use it as an array in the same way I can do it when I send Data from an HTML- Form (with multiple values) to an servlet.
    I don't know how to declare the Attribute explizit as an array.
    that it is you wanted to know?
    I think my main problem is to get an array from the servlet to the jsp. Is there an other possibility (other than via Context-Attributes) to do that?
    thanx
    robert

  • Multiple records to be created for multiple values in a field

    I am having a requirement in which i will have multiple values for a field.The values are seperated by commas.I need a record to be created for each value.(eg: if there are 4 values i need 4 records to be created). Any one please help me out.

    Hi !
    You can try solve it at File Content Conversion level, by specifing your value separator as the endSeparator.
    Or you can import all values as a unique record and then solve it via graphical mapping, using an advanced UDF, where you receive N records, then inside the UDF, you split the values via its separator and create new array values (one for each input value after splitting) and output that array to be used to fill destination structure.
    Regards,
    Matias.

  • Need to set default values for the fields plant and location in ME21N

    Hi All,
    i need to set default values for the fields plant and location in ME21N tcode.
    In accout assignment if we give 'A' then we need to create asset by clicking the Asset tab,there you have the fields plant and location.
    How to resolve this?
    Thanks in Advance

    hi .
    i needed to set default strorage location and plant in personal setting  in me21n.
    i solve it with this way.
    go to program SAPLMEPERS in se38.
    create new function in output with the sample name :MODULE ZTEST_001 OUTPUT.
    in this module write:
    if MEPOITEM_PROP-WERKS is INITIAL and  MEPOITEM_PROP-LGORT is INITIAL.
    MEPOITEM_PROP-WERKS = '1000'.
    MEPOITEM_PROP-LGORT = '0032'.
    endif.
    this is the solution.
    best regards.
    maryam

  • PowerShell: Update SharePoint multiple value lookup column?!

    Hi.
    I have some PowerShell scripts that access, update and insert data into various SharePoint 2010 lists. This all works nicely but there is one thing I'm having trouble with. I want to populate a lookup column in a list with multiple values. The column allows
    multiple values and has many such records inserted manually - but I cannot do this using PowerShell. I can update the lookup column using a single "ID" lookup value - but not with multiple values.
    I cannot find any documentation on how to do this using PowerShell. I have seen some examples (C#?) but there's little help in them.
    Any suggestions?
    Thank you,
    Markus
    Markus Sveinn Markusson

    Well, being impatient in nature, I did a lot of browsing and searching - and finally found a solution that works for me - in this thread:
    http://social.technet.microsoft.com/Forums/en/winserverpowershell/thread/3788a0ff-7011-4aea-bcc6-96a276a50e60. Adapted
    to my code it looks like this:
    $spAssignment = Start-SPAssignment
    $spList = (Get-SPWeb -identity
    http://site.domain.com -AssignmentCollection $spAssignment).Lists["Tickets"]
    $spNew = $spList.Items.Add()
    $spNew["Title"] = $Title
    $spNew["Assigned To"] = $AssignID
    $spNew["Description"] = $Descr
    $spNew["Priority"] = $Priority 
    #----- Begining of "solution"
    $lookupentry1 = New-Object Microsoft.Sharepoint.SPFieldLookupValue(1,"Service 1")
    $lookupentry2 = New-Object Microsoft.Sharepoint.SPFieldLookupValue(2,"Service 2")
    $lookupentry3 = New-Object Microsoft.Sharepoint.SPFieldLookupValue(3,"Service 3")
    $multientry = New-Object Microsoft.Sharepoint.SPFieldMultiChoiceValue($null)
    $multientry.Add($lookupentry1)
    $multientry.Add($lookupentry2)
    $multientry.Add($lookupentry3)
    $spNew["Service"] = $multientry
    #----- End of "solution"
    $spNew.Update()
    Regards,
    Markus
    Markus Sveinn Markusson

  • Multiple values for a field

    Hi All,
            In the vendor repository, i have fields like Phone No, Fax Number, Email addresss which should hold multiple values. i.e One vendor may have have multiple phone no's or email address. How should this be managed. Is it by the Qualifier table or how ? Because i dont want to have Phone No 1, Phone No 2 etc .
    Thanks in Advance !

    In the scenario you mention (the Vendor repository), both the Phone Number and Fax Number fields are links to corresponding Qualified Lookup tables. These are inherently multi-value fields.
    Just for the sake of clarity - with the exception of Measurement type fields, it is only lookup fields that can be set to have multiple values. Refer to the matrix <a href="http://help.sap.com/saphelp_mdm550/helpdata/en/1f/2ddb4203d82b78e10000000a155106/content.htm">here</a>.
    Mark

  • Error while updating the forecast value in M.Master through RMDATIND

    Hi All
    Good Day
      In my custom Program I am using the three structures bgr00,bmm00,bmmh1 & bmmh3 and through rmdatind(Direct input method) program, i am trying to update the forecast value in material master data
    and i am getting error message as Specify a tracking limit and i have taken the tracking limit as 4 in my program.
    Actually there are 2 fields exists for tracking limit in structure bmmh1
    1.SIGGR and
    2.SIGG2
    I had given the  input as 4 for these 2 fields but I am getting the same error message Specify a tracking limit
    Can anyone please help me in this case.
    Thanks in advance.
    Cheers
    Vallabhaneni

    debug your program.
    Compare the field content of the value in the field you submitted with the content in field that is checked  when message M3 456 is issued.

  • Set Default Value based on Field from another table for a custom object

    I'm trying to set the default value on a custom object field to the value of an account field. I have tried the syntax 50 different ways, and just am getting no where. The account field label displays as DBA, the integration tag is ltDBA_ACCT, and it shows up in reporting fx area as Account.Text_22.
    The custom object field I'm triying to update is also called DBA, which was originally the "NAME" required field. Does the table name, Account, have to be included? Do I need a function in front of the field?
    I have been updating the external ID using the row ID with syntex <ID> (Less than ID greater than) so I know it is possible to set the Default Value, but <DBA>, <ltDBA_ACCT>, "Account"."DBA", and so on just don't not work.
    If anyone knows how to enter this I'd really appreciate the help.

    Ok, so if you want to default a field to the value from another object, you have to use the JoinFieldValue function. I think you understand that, based on your original post, but I want to be sure that you do.
    Next, this will only work as a default if the record is created from the object that you wish to join on because a default works at record creation and the ID needs to be available for it to work properly. It will not work if you choose the related object record after the custom object record is created. You could set the value as a post-default, but that does not seem to meet your requirements.
    The syntax for the Default Value would be as follows: JoinFieldValue(ref_record_type, foreign_key, field_name).
    In your case, ref_record_type is '<Account>', foreign_key is &#91;<AccountId>&#93;, and field_name is '<YourFieldName>'. The best way to determine what the field name is would be to create a new workflow for Account and use the Workflow Rule Condition expression builder to pick your field ("DBA") from the list. The value that is returned by the expression builder should be placed in the field_name variable in the JoinFieldValue function (minus the brackets and in single quotes).
    Give this a shot and let me know how you do.
    Thom

  • Multiple Value in additional fields using SAP Query

    HI All,
    I have a question related additional field in SAP Query. I tried to create report about PR and PO using SAP Query. Since one PR can be converted to multiple PO, I decided to create additional field
    po_1
    to display the value of PO that related to the PR. But I meet problem when I want to show several PO number in additional field
    po_1
    , I couldn't look the way to solve this problem. Can anybody help me? Just for info, I assign value to additional field
    po_1
    in record processing part.
    Thanks....

    HI,
    IN SQ02, IF table is already in JOIN, just click on the PLUS sign to expand the strcture.
    All the field showing Plus sign in the strcture are selected in your query and with MINUS sing are no selected.
    So you have to just click on the MINUS sign for the field you want to use through table field.
    Generate the query and execute thriugh SQ01.
    If you want to add some other field which is not a part of JOIN,
    then click on EXTRAS Button shown in the MENU option.
    Click on Create button and system will ask abt the additional field.
    Regds,
    Anil

Maybe you are looking for