1

I have a document library (contact). Have created a custom list (workstatus). Have added column to contact library which is lookup of workstatus.

Below is my code using which i am setting the lookup column value. Unfortunately code runs fine (no exceptions) but when i check in SharePoint value is not set for the given column

FieldLookupValue lvworkstatus = new FieldLookupValue();
lvworkstatus.LookupId = workstatus;
item["crmworkstatus"] = lvworkstatus;
 item.Update();
context.Load(uploadFile);
context.ExecuteQuery();

Kindly let me know what is wrong / missing.

  • 2
    What is the value of the workstatus variable? It should be the Id of the ListItem in the workstatus list that you want to associate with the contact. – Rob Windsor Jul 5 '15 at 16:13
0

As Rob mentioned, the lookup value must be set by ID

$lookupfield = $l.Fields["lookupfield"] -as [Microsoft.SharePoint.SPFieldLookup];            
$lookuplist = $w.Lists[[Guid]$lookupfield.LookupList];            
$sourcefield =  $lookupfield.LookupField;            
$lookupitem = $lookuplist.Items[0];             
#-or-            
$lookupitem = $lookuplist.GetItemByUniqueId([Guid]"{fc71b84c-74d4-4f7c-9eed-fb7a5fbe24a6}")  
#-or-            
$lookupitem = $lookuplist.GetItemById(1)            
$lookupvalue = New-Object Microsoft.SharePoint.SPFieldLookupValue($lookupitem.ID,$lookupitem.ID.ToString());            
$i["lookupfield"] = $lookupvalue;            
$i.Update();

Here is an excellent reference for getting/setting various types in Sharepoint using Powershell:

http://social.technet.microsoft.com/wiki/contents/articles/20831.sharepoint-a-complete-guide-to-getting-and-setting-fields-using-powershell.aspx

  • 5
    just an FYI, although you are using the same CSOM, the OP is not for PowerShell. His code is in C#, so it would be easier to follow if you provide answers in the same language as OP or at least acknowledge that your answer accomplishes the same as requested but in different language – Thiago Silva Sep 27 '15 at 2:45

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.