The field you are trying to update may be read only error while adding item to list programmatically
-
10-12-2019 - |
Question
I am adding a list item in from an application page which is redirected from web part page.
My code is :
protected void Page_Load(object sender, EventArgs e)
{
SPSite site = new SPSite("http://bipc11:1234/sites/webparts");
SPWeb web = site.RootWeb;
web.AllowUnsafeUpdates = true;
SPUser currentUser = web.CurrentUser;
SPList list = web.Lists["Cart"];
SPListItem item = list.AddItem();
item["Picture"] = Request.QueryString["title"];
Response.Write("<script> alert('Pictrue Updated')</script>");
item["User"] = currentUser;
Response.Write("<script> alert('User Updated')</script>");
item.Update();
Response.Write("<script> alert('Item Updated')</script>");
web.Update();
web.AllowUnsafeUpdates = false;
Response.Write("<script> alert('Value Added To Cart Successfully')</script>");
Response.Redirect(Microsoft.SharePoint.Utilities.SPUrlUtility.CombineUrl(web.Url, "/SiteAssets/WebPartPage.aspx"));
}
Here field Picture
is of type look up in Cart
list. User
is the type of person or group. Java script to redirect to the application page is:
function myfunction(title)
{
window.location.assign("http://bipc11:1234/sites/webparts/_layouts/AddValueToCartList/AddValueToCartList.aspx?title="+title);
}
When the page is redirected to the application page I am getting above mentioned error.
What am I missing? Please help.
Solution
I am unable to find the exact reason for the error: However pls go through the following points:
1) How to update a Readonly field.
First set Readonly to false and update the field and then revert it back as:
Field.ReadOnlyField = false;
Field.Update();
2) Updating the Lookupfield:
new SPFieldLookupValue(1, "ASP.Net");
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange