none
How to Update SPUser information in C#? RRS feed

  • Question

  • SPSite site = new SPSite(siteUrl);
                   
    SPWeb oweb = site.OpenWeb();
    
    
                   
    oweb.AllowUnsafeUpdates = true;
    
                   
    SPSecurity.RunWithElevatedPrivileges(delegate()        
    {         
        SPList list = oweb.Lists[targetList];       
        if (list.ItemCount != 0)
        {
    
             SPQuery oQuery = new SPQuery();
             oQuery.Query = "<Where></Where>"; 
             SPListItemCollection collListItems = list.GetItems(oQuery);
                 
        foreach (SPListItem listItem in collListItems)
        {
                       
            String slt_userloginname = Convert.ToString(listItem["users"]);
                               
            SPUser tmp_user = oweb.EnsureUser("i:0#.w|" + slt_userloginname);
    
                               
            tmp_user.Name = "myCustomer_perfix_" + tmp_user.Name;
                               
            tmp_user.Update();
                           
        }          
    }       
    });
    In my project, i want to update the user name of some specific users in console program. 


    I have full ctrl and the program is run as admin. But the following error threw out when execute

     tmp_user.Update();

    The error message: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

    Please help.

    Thank you very much

     


    • Edited by Johnson yau Tuesday, March 7, 2017 11:11 AM
    Tuesday, March 7, 2017 11:05 AM

All replies