Allowing UserProfileManager Permissions in SharePoint 2010 -
i trying display list of users in custom webpart using userprofilemanager. reason, can view webpart , profiles output screen (maybe because administrator). when standard user logs in, encounter 403 page.
i have done reading on , know permissions. have in code:
private datatable getprofiles() { datatable dtuserprofile = new datatable(); //...datatable columns spsecurity.runwithelevatedprivileges(delegate() { guid guid = spcontext.current.site.id; using (spsite intranet = new spsite(guid)) { spusertoken usertoken = intranet.owner.usertoken; //get current intranet context. spservicecontext scontext = spservicecontext.getcontext(intranet); userprofilemanager profilemanager = new userprofilemanager(scontext); int totalusers = int.parse(profilemanager.count.tostring()); random random = new random(); (int = 0; < numberofuserstoretrieve(noofprofiles, totalusers); i++) { int randnumber = random.next(1, totalusers); datarow druserprofile; userprofile = profilemanager.getuserprofile(randnumber); druserprofile = dtuserprofile.newrow(); druserprofile["displayname"] = up.displayname; druserprofile["firstname"] = up["firstname"].value; druserprofile["lastname"] = up["lastname"].value; druserprofile["department"] = up["department"].value; druserprofile["contactnumber"] = up["office"].value; druserprofile["mysiteurl"] = up.publicurl; dtuserprofile.rows.add(druserprofile); } } }); return dtuserprofile; }
my code gets random collection of users depending on number of users want return.
is possible create spusertoken user permissions needed retrieve user profiles?
thanks!
i appreciate question old, had exact same problem. original poster , other users, have altered code original post following:
spsecurity.runwithelevatedprivileges(delegate() { spsite sc = new spsite(spcontext.current.site.id); spservicecontext context = spservicecontext.getcontext(sc); httpcontext currentcontext = httpcontext.current; httpcontext.current = null; userprofilemanager profilemanager = new userprofilemanager(context); ienumerator profileenum = profilemanager.getenumerator(); while (profileenum.movenext()) { userprofile = (userprofile)profileenum.current; if ((up["firstname"] != null && up["firstname"].value != null && !string.isnullorempty(up["firstname"].value.tostring())) && (up.publicurl != null && !string.isnullorempty(up.publicurl.tostring()))) { datarow druserprofile; druserprofile = dtuserprofile.newrow(); druserprofile["displayname"] = up.displayname; druserprofile["firstname"] = up["firstname"].value; druserprofile["lastname"] = up["lastname"].value; druserprofile["department"] = up["department"].value; druserprofile["location"] = up["sps-location"].value; druserprofile["mysiteurl"] = up.publicurl.tostring().replace(@"\", @"\"); dtuserprofile.rows.add(druserprofile); } } } httpcontext.current = currentcontext;
hopefully code should resolve error.
Comments
Post a Comment