Quantcast
Channel: SharePoint 2010 - Development and Programming forum
Viewing all articles
Browse latest Browse all 11571

listitem updates column randomly

$
0
0

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.



Viewing all articles
Browse latest Browse all 11571

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>