2

I am trying to get the details of users from User Profiles using CSOM. I have a list of users in Excel file(approx 20000).

I am reading the excel file and storing data in DataTable, i am iterating over the DataTable rows and trying to get details for each of the user using CSOM.

foreach (DataRow row in Inputtbl_ExcelFile.Rows)
        {
            GroupCollection grps1;
            User user;
            i++;
            string loginName = "";
            try
            {
                loginName = row["UserID"].ToString();
                //loginName = @"dom4\1234567";
                #region GetUserDetails

                    user = clientContext.Web.SiteUsers.GetByLoginName(loginName);
                    clientContext.Load(user, user1 => user1.LoginName, user1 => user1.Email, user1 => user1.Groups, user1 => user1.Id);

                    grps1 = user.Groups;
                    clientContext.Load(grps1, groups => groups.Include(group => group.Title, group => group.Users, group => group.Id));
                    clientContext.ExecuteQuery();

                string groupName = string.Empty;
                if (grps1.Count == 0)
                    groupName = "NT AUTHORITY\authenticated users";
                foreach (Group gr in grps1)
                {
                    groupName = groupName + gr.Title + ";";
                }
                #endregion

                #region UserProf
                //// PeopleManager class provides the methods for operations related to people
                PeopleManager peopleManager = new PeopleManager(clientContext);

                PersonProperties personProperties = peopleManager.GetPropertiesFor(loginName);

                clientContext.Load(personProperties);
                clientContext.ExecuteQuery();

                string Sector = personProperties.UserProfileProperties["Sector"];
                #endregion


                Outputtbl.Rows.Add(i, user.Id, loginName.Replace("i:0#.w|", ""), user.Email, groupName, "Success", Sector);

            }
            catch (Exception ep)
            {
                Outputtbl.Rows.Add(i, "", loginName.Replace("i:0#.w|", ""), "", "", "Failed ," + ep.Message, "");

                Console.WriteLine(ep.Message);
            }
            clientContext.Dispose();
        }

Now the issue is after some point of time, approx after it has iterated for 2000 items or sometimes 1500 or sometimes even after 3000 items, it throws an error at line :

user = clientContext.Web.SiteUsers.GetByLoginName(loginName);

Exception of type 'System.OutOfMemoryException' was thrown.

I have gone through various article on internet and I understand that this is not related physical memory space but is given slot available at one time.

Can anybody please help me understand if there is any other issue in code or if i can improve code?

1 Answer 1

1

Try not to dispose the clientcontext every time in the for loop,
maybe this kind of structure will help you

var clientContext = //get clientContext
using(clientContext)
{
    foreach(DataRow row in Inputtbl_ExcelFile.Rows)
    {
        //your stuff
    }
}
//clientContext will be disposed here
2
  • Thanks for this i had tried that as well but the same issue.. well i just noticed that the code works fine on another machine, even without using dispose or using.
    – Ishan
    Commented Aug 12, 2015 at 13:11
  • Glad it worked, but I suggest you to use the dispose(). It is important
    – Nk SP
    Commented Aug 12, 2015 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.