0

How to set client side object FieldLookupValues using managed.NET

I've tried setting the lookup field to the id directly. I've tried setting it to the string as shown in the sample code below. I've tried providing the specific listitem from the reference table all to no avail. I've found other examples that are close, but can't quite get things to work. How do you make a field in the record you are about to add properly reference the lookup table value (Account Manager)? And how would this be done for a multiselect (product)?

using (ClientContext context = new ClientContext(this.WebUrl))
{
    SecureString securePassword = new SecureString();
    foreach (char c in this.CRMPassword) { securePassword.AppendChar(c); }

    CredentialCache cc = new CredentialCache();
    NetworkCredential nc = new NetworkCredential(this.UserName, securePassword);                    

    cc.Add(new Uri(this.CRMWebUrl), "NTLM", nc);

    context.Credentials = cc;

    Web web = context.Web;
    context.Load(web);

    // the list I want to add a new item to
    Microsoft.SharePoint.Client.List lstSecurityTasks = context.Web.Lists.GetByTitle("SecurityTasks");

    // Reference lists for my lookups
    Microsoft.SharePoint.Client.List lstAccounts = context.Web.Lists.GetByTitle("Products");
    Microsoft.SharePoint.Client.List lstContacts = context.Web.Lists.GetByTitle("Account Managers");

    //Add a new item in the List
    ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
    Microsoft.SharePoint.Client.ListItem itemToAdd =  lstSecurityTasks.AddItem(itemCreateInfo);
    // set required fields
    itemToAdd["Title0"] = "Security Configuration Request";
    itemToAdd["Status0"] = "Not Started";
    // manually setting lookups to respective strings does NOT work?
    itemToAdd["MY_x0020_Product"] = ";#DataEntry Web 2.5;#Analytics Portal;#";
    itemToAdd["MY_x0020_Account_x0020_Manager"] = "3;#John Smith";

    itemToAdd.Update();

    context.ExecuteQuery();
}
1

I think your question is related to Set Multi-Value for FieldLookup not a single value. So to set a multi-value column, you need to set a FieldLookupValue[] as array to be as the following

Get manually the ID of your lookup values. in this case, it's (DataEntry Web 2.5,Analytics Portal) and set it as an array as the following

var products = new[] { new FieldLookupValue { LookupId = 1 }, new FieldLookupValue { LookupId = 2} };
itemToAdd["MY_x0020_Product"] = products;
itemToAdd.Update();
context.ExecuteQuery();

Note : LookupId = 1 refer to the ID of your lookup field value DataEntry Web 2.5 as you mention at your code, so that you should set it based on your current field value ID.

You can also get programmatically the ID of your lookup values based on your value at if condition below as the following

var LookupIdField = ListItem["LookupFieldName"] as FieldLookupValue[];

if (LookupIdField != null)
{
    foreach(var flookupID in LookupIdField)
    {
        if (flookupID.LookupValue == "DataEntry Web 2.5")
         {
           var FiledLoopkup_Id = flookupID.LookupId;
         }
   }
}

Regarding setting value for a single-value look up column you just need to set the column value ID to a FieldLookupValue instance, as mentioned at SharePoint client model and setting up lookup field

  • how does it know that var products relates to the "Products" list? – littlegreendude Sep 12 '16 at 12:22
  • At which code you talk ? are you mean this var products = new[] { new FieldLookupValue { LookupId = 1 }, new FieldLookupValue { LookupId = 2} }; ? it's only array that you initalize it with the Lookup ID from Product List , please don't hesitate to ask me if you need more clarafication – Mohamed El-Qassas MVP Sep 12 '16 at 12:26
  • Yes, that is the line I am referring to. – littlegreendude Sep 12 '16 at 12:30
  • it's only array that you initalize it with the Lookup ID of items from Product List , – Mohamed El-Qassas MVP Sep 12 '16 at 12:30
  • Thank you for the help. I got it to work! It must know the cast/type of the lookup field based on the field being assigned. The problem I was having was that I didn't realize I needed to do an update and execute for each lookup field. The entire record has 6 lookup fields and I was only doing 1 update/execute at the very end. – littlegreendude Sep 12 '16 at 13:07
1

I have found similar question please refer it.
ID can then be used to set the listitem's FieldLookupValue.
Use below code to set your lookup field:

FieldLookupValue lv = new FieldLookupValue();
lv.LookupId = 1;
item["Project"] = lv;
item.Update();
clientContext.ExecuteQuery();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.