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


November 27th, 2012 3:15pm

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....

Free Windows Admin Tool Kit Click here and download it now
November 27th, 2012 3:29pm

Hi, 

I tried this but still getting same error.

piItem["Product_x0020_Number"] = Convert.ToString(prodid) + ";#" + ProductNumber;

Regards,

Mehar

November 27th, 2012 3:40pm

Cuold you paste the screenshot of the column settings here...

Free Windows Admin Tool Kit Click here and download it now
November 27th, 2012 5:05pm

Hi, 

I just checked the columns, the problem was due to  "User" field. I was assigning value as string. 

piItem["User_x0020_Adding"] = UserAdding;
Solve it by:
piItem["User_x0020_Adding"] = client.Web.EnsureUser(UserAdding);

Thanks for your support.

Mehar

November 27th, 2012 9:39pm

For reference, if you are in a workflow, you could then also do:

public SPWorkflowActivationProperties workflowProperties = new SPWorkflowActivationProperties();

workflowProperties.Web.EnsureUser("Public, John Q.").  

You could also do this in regular forms:

using (SPSite site = new SPSite(SPContext.Current.Site.Url))

{

using (SPWeb web = site.OpenWeb())

{

web.EnsureUser("Public, John Q.");

}

}





  • Edited by navyjax2 2 hours 26 minutes ago
Free Windows Admin Tool Kit Click here and download it now
April 7th, 2015 1:10am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics