Dear Techies,
I am working on migration of Discussion List, Here I am facing some issue
Invalid data has been used to update the list item The field you are trying to update may be read only
Let me explain What I am doing
List Template Default Discussion list
I have added one lookup column with the source is from same site
1.Its a Console Application
2. I am creating the Question , Then I am passing the listitem to create a reply for it , Its created
3.Then I am passing the parent listitem and replied item id to set it as bestanswer id
The above steps are working fine without lookup filed, If I add the lookup filed its thowrs error as nativehr along withInvalid data has been used to update the list item The field you are trying to update may be read only
Please find below the code sinnpet
private static void CreateQuestion(Entities oEntities){
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite oSPSite = new SPSite(site))
{
using (SPWeb oSPWeb = oSPSite.OpenWeb(web))
{
SPList oSPList = oSPWeb.Lists[list];
SPListItem oSPListItem = Microsoft.SharePoint.Utilities.SPUtility.CreateNewDiscussion(
oSPList, oEntities.Question);
oSPListItem[SPBuiltInFieldId.Body] = oEntities.QuestionDescription;
//oSPListItem["Discipline"] = GetLookUpId(oSPWeb, oEntities);
SPFieldLookupValue lookupField = new SPFieldLookupValue();
lookupField.LookupId = GetLookUpId(oSPWeb, oEntities);
oSPListItem["Discipline"] = lookupField;
oSPListItem[SPBuiltInFieldId.IsQuestion] = true;
oSPListItem["Created By"] = GetCurrentUser(oSPWeb, oEntities.QuestionAskedBy);
oSPListItem.SystemUpdate();
CreateReply(oSPListItem, oEntities);
}
}
});
}
catch (Exception e)
{
}
}
private static void CreateReply(SPListItem oSPListItem, Entities oEntities)
{
try
{
SPListItem rSPListItem = Microsoft.SharePoint.Utilities.SPUtility.CreateNewDiscussionReply(oSPListItem);
rSPListItem["Body"] = oEntities.Reply;
rSPListItem["Created By"] = GetCurrentUser(oSPListItem.Web, oEntities.RepliedBy);
rSPListItem.SystemUpdate();
// MarkAsBestReply(oSPListItem, rSPListItem.ID, oEntities);
}
catch (Exception e)
{
}
}
private static void MarkAsBestReply(SPListItem bSPListItem, int id)
{
try
{
SPList bSPList = bSPListItem.ParentList;
SPListItem SPListItem = bSPList.GetItemById(bSPListItem.ID);
SPListItem[SPBuiltInFieldId.BestAnswerId] = int.Parse(id.ToString());
//SPListItem["BestAnswerId"] = int.Parse(id.ToString());
//SPListItem["BestReplyBy"] = SPListItem["Created By"].ToString();
SPListItem["Modified By"] = bSPListItem["Created By"];
SPListItem.SystemUpdate();
}
catch (Exception e)
{
}
}
private static SPUser GetCurrentUser(SPWeb oSPWeb, string UserId)
{
SPUser oUser = null;
try
{ oUser = oSPWeb.EnsureUser(UserId); }
catch (Exception ex)
{ }
return oUser;
}
private static int GetLookUpId(SPWeb oSPWeb, Entities oValues)
{
int id = 0;
try
{
SPList oSPList = oSPWeb.Lists[LookUpSourcelist];
SPListItemCollection listItems = oSPList.Items;
foreach (SPListItem item in listItems)
{
if (item[LookUpSourceField].ToString() == oValues.Discipline.ToString())
{ id = item.ID; break; }
}
}
catch (Exception ex)
{ }
return (id);
}
Thanks in advance
Sathish Kasinathan