listitem updates column randomly

Hi

I have list with two columns

name as  title and UserName as person or Group field.

I am updating the list from custom timer job.

I am getiing all the usernames and user login names in the Dictonary object in key-value pair.

But when i am updating these values in list its updating wrong UserName to wrong name

like

user1 -- User_name45

user2 -- User_name1

user3 -- user_name5

user4 -- No value

It should be like this-

user1 -- User_name1

user2 -- User_name1

user3 -- user_name3

user4 --  user_name4

I am not understanding if I am getting proper data in dictonary why its not updating in the list properly.

below is my code,

if (collListItemUser.Count > 1)
{                      
                      SPSecurity.RunWithElevatedPrivileges(delegate()
{
 using (SPSite ElevatedSite = new SPSite(spSiteURL))
{
using (SPWeb oSPMyWeb = ElevatedSite.OpenWeb())
{
                                  oSPMyWeb.AllowUnsafeUpdates = true;
 SPList oSPListUsers = oSPMyWeb.Lists["UserList"];
 SPListItemCollection oSPListItemCollectionUser;
                                  oSPListItemCollectionUser = oSPListUsers.Items;

oSPMyWeb.AllowUnsafeUpdates = true;


Dictionary<string, string> userList = new Dictionary<string, string>();
foreach (ListItem oclistItem in collListItemUser)
{
string uName = oclistItem["Title"].ToString().Trim();
if (oclistItem["UserName"] != null)
{
                                          Microsoft.SharePoint.Client.FieldUserValue[] userValue = oclistItem["UserName"] as Microsoft.SharePoint.Client.FieldUserValue[];
int userID = 0;
string userlogin = string.Empty;
string userIDLoginName = string.Empty;
bool getCCUserExc = false;
try
{
String strUserName = serValue[0].LookupValue.ToString();
 User oUser = clientContext.Web.EnsureUser(strUserName);
                                                clientContext.Load(oUser);
                                                clientContext.ExecuteQuery();
 userID = oUser.Id;
                                                userlogin = oUser.LoginName;
                                                userIDLoginName = userID.ToString() + ";#" + userlogin;
 if (!userList.ContainsValue(userIDLoginName))
 {
                                                    userList.Add(uName, userIDLoginName);
 }
                                                
 }
catch (Exception ex)
{
                                                getCCUserExc = true;
 }
 if (getCCUserExc)
 {
string userLogin = userValue[0].LookupId.ToString()+";#" + userValue[0].LookupValue;
                                                userList.Add(uName,userLogin);
 }                                                
                                          
}
 }
foreach (KeyValuePair<string, string> entry in userList)
 {
 try
{
SPListItem oSPListItemUser= SPListUsers.Items.Add();                                          
                                          oSPListItemUser["Title"] = entry.Key;
                                          oSPListItemUser["UserName"] = entry.Value;
                                          oSPListItemUser.Update();
}
catch (Exception e)
{ 

}
 }
                                 
                                  oSPMyWeb.AllowUnsafeUpdates = false;
}

}
});
}

This code is not showing any error but updating the data wrongly.

Anybody got this type of issue before please suggest what wrong here.


  • Edited by Vyanky Thursday, February 27, 2014 1:58 PM
February 27th, 2014 4:45pm

The code return no error because you trap the exception with the try catch statement, but the catch flow is empty.

Try to log somewhere the exception to undestand what the problem is...

catch (Exception e)
{ 

}

Free Windows Admin Tool Kit Click here and download it now
February 28th, 2014 2:24pm

hi

you most get spuser from context of List that you want to save it, so change:

User oUser = clientContext.Web.EnsureUser(strUserName);

with:

User oUser =oSPMyWeb.EnsureUser(strUserName);


March 1st, 2014 7:00am

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

Other recent topics Other recent topics