Navigate Up
Sign In
Supporters of Developer
Web

All Kinds of Things You Can Do With SPUser Objects

Item is currently unrated. Press SHIFT+ENTER to rate this item.1 star selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+ESCAPE to leave rating submit mode.2 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.3 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.4 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.5 stars selected. Press SHIFT+ENTER to submit. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.

You may also be interested in: Sharegate No-Brainer Migration Tools

 

Editor's note: Contributor Kate Mogel is a a web application developer from Massachusetts

The SPUser object is a really useful tool, but sadly not very intuitive. Here are some of my favorite SPUser snippets.

People picker list columns don’t return a user object, which is frustrating, but not foreign to those of us with picker experience. An ID is generally returned by a lookup field. Not so for people pickers, which return a domain\user string that is frequently pre-pended by nonsense.

This is how you get a user directly from the list item:


protected SPUser GetUserfromItem(SPListItem userItem)
{
SPFieldUser userField = (SPFieldUser)userItem.Fields.GetField(“User”);
SPFieldUserValue fieldValue = (SPFieldUserValue)userField.GetFieldValue(userItem[userField.Id].ToString());
return fieldValue.User;
}

Now let’s look at a neat little method that grabs some properties by current user! As a side note, there are a lot of issues that are purportedly resolved by setting the context to null – I have never found this helpful. Never. In my lifetime.


//User Properties by user
private bool GetProperty(string userName)
{
//Your return value is a bool
bool found = false;
//Create a profile manager
UserProfileManager profMan;
//Get your site in a “using” so there’s no disposing
using (SPSite site = new SPSite(SPContext.Current.Site.ID))
{
//Make everything unsafe. Just kidding. This is so that you have permissions to edit the profile property.
//If you don’t plan on making changes, you can access the property without this.
Web.AllowUnsafeUpdates = true;
//Okay. Get your web on!
using (SPWeb newWeb = site.OpenWeb())
{
//Get the context and set the profile manager
SPServiceContext context = SPServiceContext.GetContext(site);
profMan = new UserProfileManager(context);
//Get your user profile object
UserProfile prof = profMan.GetUserProfile(userName);
//This is important. Remember to check the Property.Value. Otherwise you are checking to see
//if the property itself is null. And since you know it exists – guess what? It isn’t null.
if (prof["Property"].Value != null)
{
_property = prof["Property"].Value.ToString();
found = true;
}
else
{
found = false;
}
}
}
//Do this last, or even in a “finally” since you really want this set to false at the end of execution
Web.AllowUnsafeUpdates = false;
return found;
}

//And one more

//User properties by property
FullTextSqlQuery query = new FullTextSqlQuery(SPContext.Current.Site);
//Create the SQL query. The scope is important if you want it limited to people. Note that each item you select
//(Here WorkPhone, WorkEmail, DisplayName) has to be a managed property that a. has the same name and b. is
//Set to work in scopes
query.QueryText = “SELECT WorkPhone, WorkEmail, DisplayName FROM SCOPE() WHERE (\”Scope\” = ‘People’) AND (\”Property\” = ‘” + propertyValue + “‘)”;
//Set result type
query.ResultTypes = ResultType.RelevantResults;
//Set result limit
query.RowLimit = 5000;
//Execute to a result table collection
ResultTableCollection results = query.Execute();
//Parse to a single result table
ResultTable resultTable = results[ResultType.RelevantResults];
//And a regular DataTable if you need it
DataTable dt = new DataTable();
dt.Load(resultTable, LoadOption.OverwriteChanges);

There you go! Be a SPUser expert! Goodnight and goodluck.

Categories: dev; User Profile Service; MOSS; WSS; 2010

Comments

Stephan Onisick

Thanks for the snippets

Also thanks for outlining the best way to work with the objects. Getting user information from people pickers is hard work.

Posted 19-Dec-2012 by Stephan Onisick
Christopher Long

DevBox

I am working on a Win7 DevBox and having trouble with the UserProfileManager. Do you have to be working one of the WFEs to get the UserProfile information?

Posted 18-Jun-2013 by Christopher Long
rock

ROCK

An ID is total returned by a lookup area. Negative so for peoples pickers,assignment help uk which stipend a property\\\\user shore that is frequently pre-pended by insanity.

Posted 24-Sep-2013 by rock
kaniz

help

Persons picker directory rows don’t requite a user remonstrate, which is aggravating, nevertheless nay alien to those of us essay help writing among picker have. An ID is commonly returned by a lookup stadium. Hardly so for persons pickers, which repay a sphere\\user twine that is frequently pre-pended by trash.

Posted 02-Oct-2013 by kaniz
Sultan Khan

I am active

I am active on a Win7 DevBox http://www.getdissertation.net/ besides having harass among the UserProfileManager.

Posted 26-Oct-2013 by Sultan Khan
concrete wheel stops

This is a good

This is a good place for the time spent a. I just found your blog informative and wanted to say I really enjoyed reading your messages.

Posted 30-Oct-2013 by concrete wheel stops
x,/.mbvcxvb./

cvxvbxcxv

This skill is also useful in being taught to young minds as you find it in the top of business in the thinking of entrepreneurs and troubleshooters, and that is where all ambitious hopefuls wish to be. COMMERCIAL PLUMBER OCEANSIDE

Posted 14-Nov-2013 by x,/.mbvcxvb./
Sultan Khan

This is a

This is a great place for the time invested a. I just discovered your website useful and desired to say I really experienced studying your information. Food Catering

Posted 16-Nov-2013 by Sultan Khan
Sultan Khan

Instantly

Instantly that you are cognizant of how to scratch a continue that desire favor you custom essay writing services online obtain the employment you deficiency, you just demand to acquire started on it.

Posted 17-Nov-2013 by Sultan Khan
xcxbcxb

ccccccccccccc

That which shocks me most is the sightless identify Republican Celebration females have regarding the party's anti-woman applications and behaviour. The up-coming Republican Conference in Polk, FL, will probably carry forth a celebration system that does not, in any way, benefit females. best vines 2013

Posted 23-Nov-2013 by xcxbcxb
Sultan Khan

That which

That which excitement me most is the sightless recognize Republican Party women have regarding the person's anti-woman programs and behavior. Procedures

Posted 24-Nov-2013 by Sultan Khan
khan

jhfuih

even temporarily, out into the innovative world of miracle and professional, official statement

Posted 25-Nov-2013 by khan
Emma

females

Males and females picker list points don’t returning an individual object, that may be demoralizing, even so, certainly not international to be able to these people applying picker realistic knowledge. car cleaning products A fantastic I . D . is usually delivered using search market. Small number of if you are pickers, that returning any domain\user collection which may be routinely pre-pended simply by nonsense.

Posted 02-Dec-2013 by Emma
skyrimexpart

I like this page.

I like this page. I am very grateful for the information presented was very beneficial to me personally and to others. click the link

Posted 09-Dec-2013 by skyrimexpart
Rdokoye

Great work

Many interesting things you can do with an SPUser object indeed. Your instructions were easy enough to follow, thanks for that.

Posted 29-Dec-2013 by Rdokoye
projects

interviewed

Because of the CSS that is used to NewMenu manages, they are covered in a tag which causes the management to render as empty on the selection.

Posted 19-Mar-2014 by projects
is bubblegum casting legitimate

is bubblegum casting legitimate

This was really interesting info in this blog that to very happy for the nice technology in this blog.

Posted 29-Mar-2014 by is bubblegum casting legitimate

Notify me of comments to this article

E-mail:
   

Add Comment

Title:

 
Comment:
Email:

   


Name:

 
Url: