About SharePoint UserProfile error Access Denied or Unauthorized Access

This post is about to tell you a few common and tricky errors in SharePoint UserProfile object model. Using UserProfileManager class, we can easily  instantiate the UserProfile manager object by passing the server context. And here the methods of the UserProfileManager class.

To work with UserProfile data, initially few things need to be checked. First thing is to check your SSP, if no user profile information is available yet, you need to import the UserProfile into your SSP content db. This means establish a connection with your LDAP or AD and use SSP GUI to import the same. Here is one url http://blogs.msdn.com/b/gyorgyh/archive/2009/02/07/how-does-it-work-moss-2007-user-profile-import.aspx which will help you. Or, you can also use the profile imported CodePlex code as well.

Once the UserProfile information is available on your box, then the next part is, build a feature/page to implement editable/update UserProfile information. In other words, you need to pass the User’s LoginID and retrieve the user information, do some changes and update. System will alllow you to update the same in your SSP content DB.  For the R&D purpose, I always prefer a windows application (I can place more criteria in GUI and design it according to my wish :) ) and in this case, I build a simple windows form and build a UserProfile class.

The issue starts while I’m trying to use Count property of the UserProfileManager class, it throws me an System.UnauthorizeAccessException error.

Code is pretty straight forward and it is given below:

//UserProfile Object variables

ServerContext ospServerContext = null;
UserProfileManager ospUserProfileManager = null;
UserProfile up = null;

using (SPSite ospSite = new SPSite(this.strUrl))

{
try

{
ospServerContext = ServerContext.GetContext(ospSite);
ospUserProfileManager = new UserProfileManager(ospServerContext);
up = ospUserProfileManager.GetUserProfile(sUserLoginID);
…………………………………………………………………………………….

…………………………………………………………………………………….

The interesting part is, the UserProfile is loaded properly and the related information is properly displayed.  However when trying to update the UserProfile data, get the following error information. It is saying that “Access Denied: You may only modify your won profile“.

To fix this issue, go to the SSP admin page and then go to Manage Permission: Shared Service Right. There, you need to add the Manage User Profiles right to the credential used in this windows application. In this scenario, I have added the same right to my credential i.e. LAHAH.

Next, while run the code again, you can see the value of the Count property in the debug state. This implies that you now have adequate permission to read/update UserProfile data.

Another error you can found isProperty Not Defined: <property name>. An Administrator must create this property in the profile Administration tool. Below the catch block, where the error has been caught:

This error indicate that this property is not exist in the UserProfile properties. The best way to manage this, specially when you are not sure whether properties is exist or not, do a check of this properties value. For example, like for HomePhone property, first I have checked whether this is a valid property or not. And if this is a valid one, make sure it doesn’t contain null value.

if (up[USER_HOMEPHONE] != null)

{

if (up[USER_HOMEPHONE].Value != null)
this.HomePhone = up[USER_HOMEPHONE].ToString();
}

Again, if you do not check whether the value is not NULL or EMPTY before get the property value, you will get the following error i.e. Object Reference in not set to an instance of an object. This is because, if you are trying to use/convert ToString() of a NULL or EMPTY value.

Another very easy error you can get while trying to edit NON-EDITABLE UserProfile property like Name. While you trying to set its value, you will get the error message “Property Not Editable: This Property can not be modified“. You just need to off  SET statement for this property.

And finally, the save method is pretty simple. You just need to set the value and call the UserProfile.Commit() method and that’s all. And do not forget to release the UserProfileManager, UserProfile object and call the Dispose() method of  spSiteobject. Here we go…

ospServerContext = ServerContext.GetContext(eleviateSPSite);
ospUserProfileManager = new UserProfileManager(ospServerContext);
up = ospUserProfileManager.GetUserProfile(appexID);
//update the user profile new value

if(this.FirstName != null)
up[USER_FIRSTNAME].Value = this.FirstName;
if(this.Lastname !=null )
up[USER_LASTNAME].Value = this.Lastname;
if(this.WorkPhone !=null )
up[USER_WORKPHONE].Value = this.WorkPhone;
//save the value in the system

up.Commit();
}
catch (Exception ex)
{
strResult = ex.Message.ToString();
}
finally

{
ospServerContext = null;
ospUserProfileManager = null;
up = null;
eleviateSPSite.Dispose();
}

To make it more clear, I’m pasting here the two CS files.

SPUserProfile.cs

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.Office.Server;

using Microsoft.Office.Server.Administration;

using Microsoft.Office.Server.UserProfiles;

using Microsoft.SharePoint;

using System.Web;

namespace SPTest
{
/* [DefaultMemberAttribute("Item")]
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)] */

class SPUserProfile

{
private string strResults = “0″;
private string strUrl = “http://<Your Server URL>/”;
private const string USER_ACCOUNTNAME = “Accountname”;
private const string USER_FIRSTNAME = “Firstname”;
private const string USER_LASTNAME = “Lastname”;
private const string USER_WORKPHONE = “WorkPhone”;
private const string USER_HOMEPHONE = “HomePhone”;
private const string USER_NAME = “UserName”;
private const string USER_OFFICE = “Office”;
private const string USER_DEPARTMENT = “Department”;
private const string USER_TITLE = “Title”;
private const string USER_MANAGER = “Manager”;
private const string USER_ABOUTME = “Aboutme”;
private string sAccountName = string.Empty;
private string sFirstname = string.Empty;
private string sLastname = string.Empty;
private string sWorkPhone = string.Empty;
private string sHomePhone = string.Empty;
private string sName = string.Empty;
private string sOffice = string.Empty;
private string sDepartment = string.Empty;
private string sTitle = string.Empty;
private string sManager = string.Empty;
private string sAboutme = string.Empty;
//Object variables

ServerContext ospServerContext = null;
UserProfileManager ospUserProfileManager = null;
UserProfile up = null;
public string URL
{
get { return strUrl; }
set { strUrl = value; }
}
public string AccountName
{
get { return sAccountName; }
}
public string FirstName
{
get { return sFirstname; }
set { sFirstname = value; }
}
public string Lastname
{
get { return sLastname; }
set { sLastname = value; }
}
public string WorkPhone
{
get { return sWorkPhone; }
set { sWorkPhone = value; }
}
public string HomePhone
{
get { return sHomePhone; }
set { sHomePhone = value; }
}
public string Name
{
get { return sName; }
set { sName = value; }
}
public string Office
{
get { return sOffice; }
set { sOffice = value; }
}
public string Department
{
get { return sDepartment; }
set { sDepartment = value; }
}
public string Title
{
get { return sTitle; }
set { sTitle = value; }
}
public string Manager
{
get { return sManager; }
set { sManager = value; }
}
public string Aboutme
{
get { return sAboutme; }
set { sAboutme = value; }
}
public string GetUserByName(string sUserLoginID)
{
using (SPSite ospSite = new SPSite(this.strUrl))
{
try

{
ospServerContext = ServerContext.GetContext(ospSite);
ospUserProfileManager = new UserProfileManager(ospServerContext);
up = ospUserProfileManager.GetUserProfile(sUserLoginID);
//get the value from UserProfile

if (up[USER_ACCOUNTNAME] != null)
{
if (up[USER_ACCOUNTNAME].Value != null)
this.sAccountName = up[USER_ACCOUNTNAME].ToString();
}
if (up[USER_FIRSTNAME] != null)
{
if (up[USER_FIRSTNAME].Value != null)
this.FirstName = up[USER_FIRSTNAME].ToString();
}
if (up[USER_LASTNAME] != null)
{
if (up[USER_LASTNAME].Value != null)
this.Lastname = up[USER_LASTNAME].ToString();
}
if (up[USER_WORKPHONE] != null)
{
if (up[USER_WORKPHONE].Value != null)
this.WorkPhone = up[USER_WORKPHONE].ToString();
}
if (up[USER_HOMEPHONE] != null)
{
if (up[USER_HOMEPHONE].Value != null)
this.HomePhone = up[USER_HOMEPHONE].ToString();
}

if (up[USER_OFFICE] != null)
{
if (up[USER_OFFICE].Value != null)
this.Office = up[USER_OFFICE].ToString();
}
if (up[USER_DEPARTMENT] != null)
{
if (up[USER_DEPARTMENT].Value != null)
this.Department = up[USER_DEPARTMENT].ToString();
}
if (up[USER_TITLE] != null)
{
if (up[USER_TITLE].Value != null)
this.Title = up[USER_TITLE].ToString();
}
if (up[USER_MANAGER] != null)
{
if (up[USER_MANAGER].Value != null)
this.Manager = up[USER_MANAGER].ToString();
}
if (up[USER_ABOUTME] != null)
{
if (up[USER_ABOUTME].Value != null)
this.Aboutme = up[USER_ABOUTME].ToString();
}
strResults = “Display Record”;
}
catch (Exception ex)
{
strResults = ex.Message.ToString();
}
finally

{
ospServerContext = null;
ospUserProfileManager = null;
up = null;
}
}
return strResults;
}

public string UpdateUserByLoginID(string appexID)
{
string strResult = string.Empty;
SPSite eleviateSPSite = null;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite ospSite = new SPSite(this.strUrl))
{
eleviateSPSite = ospSite;
}
});
try

{
ospServerContext = ServerContext.GetContext(eleviateSPSite);
ospUserProfileManager = new UserProfileManager(ospServerContext);
up = ospUserProfileManager.GetUserProfile(LoginID);
//update the user profile new value

if(this.FirstName != null)
up[USER_FIRSTNAME].Value = this.FirstName;
if(this.Lastname !=null )
up[USER_LASTNAME].Value = this.Lastname;
if(this.WorkPhone !=null )
up[USER_WORKPHONE].Value = this.WorkPhone;
if(this.HomePhone !=null )
up[USER_HOMEPHONE].Value = this.HomePhone;
if(this.Name !=null )
up[USER_NAME].Value = this.Name;
if(this.Office !=null )
up[USER_OFFICE].Value = this.Office;
if(this.Department !=null )
up[USER_DEPARTMENT].Value = this.Department;
if(this.Title !=null )
up[USER_TITLE].Value = this.Title;
if(this.Manager !=null )
up[USER_MANAGER].Value = this.Manager;
if (this.Aboutme != null)
up[USER_ABOUTME].Value = this.Aboutme;
//save the value in the system

up.Commit();
strResult = “Save information sucessfully”;
}
catch (Exception ex)
{
strResult = ex.Message.ToString();
}
finally

{
ospServerContext = null;
ospUserProfileManager = null;
up = null;
eleviateSPSite.Dispose();
}
return strResult;
}
}
}
===================================================================================================
And here the Windows GUI  codebehind file
Window.cs
using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Net;

using System.Net.Security;

using System.Text;

using System.Windows.Forms;

using Microsoft.SharePoint;

using Microsoft.SharePoint.SoapServer;

using Microsoft.SharePoint.Search;

namespace SPTest
{
public partial class MasterWindow : Form

{
public MasterWindow()
{
InitializeComponent();
}
//CUSTOM API

SPUserProfile spUsrProf = null;
private void btnSearch_Click(object sender, EventArgs e)
{
}
//Get User information By LoginID

private void btnGetUserByLoginID_Click(object sender, EventArgs e)
{
string strOutPut = string.Empty;
lblError.Text = “”;
if (txtGetUserByLoginID.Text.Trim() == “”)
{
MessageBox.Show(“Please enter LOGINID in the test box before hit the search button.”);
return;
}
try

{
if(spUsrProf == null)
spUsrProf = new SPUserProfile();
strOutPut = spUsrProf.GetUserByName(txtGetUserByLoginID.Text.Trim());
//LoginID – not in edit mode

this.txtUserAppexID.Text = this.txtGetUserByLoginID.Text;
//get the properties details

this.txtAccountName.Text = spUsrProf.AccountName;
this.txtFirstName.Text = spUsrProf.FirstName;
this.txtLastName.Text = spUsrProf.Lastname;
this.txtWorkPhone.Text = spUsrProf.WorkPhone;
this.txtHomePhone.Text = spUsrProf.HomePhone;
this.txtTitle.Text = spUsrProf.Title;
this.txtName.Text = spUsrProf.Name;
this.txtManager.Text = spUsrProf.Manager;
this.txtOffice.Text = spUsrProf.Office;
this.txtDepartment.Text = spUsrProf.Department;
this.txtAboutme.Text = spUsrProf.Aboutme;
}
catch (Exception ex)
{
//write error in screen

this.lblError.Enabled = true;
this.lblError.Text = ex.Message.ToString();
}
finally

{
//do nothing

}
}
private void btnUpdateUserProfile_Click(object sender, EventArgs e)
{
lblError.Text = “”;
lblError.Enabled = true;
if (spUsrProf == null)
{
MessageBox.Show(“Sorry, UserProfile class yet not initialize. Please get user profile information again by cliking the GetUserProfile button”);
return;
}
try

{
//update the new value in properties

spUsrProf.FirstName = this.txtFirstName.Text.ToString();
spUsrProf.Lastname = this.txtLastName.Text.ToString();
spUsrProf.WorkPhone = this.txtWorkPhone.Text.ToString();
spUsrProf.HomePhone = this.txtHomePhone.Text.ToString();
spUsrProf.Name = this.txtName.Text.ToString();
spUsrProf.Office = this.txtOffice.Text.ToString();
spUsrProf.Title = this.txtTitle.Text.ToString();
spUsrProf.Manager = this.txtManager.Text.ToString();
spUsrProf.Department = this.txtDepartment.Text.ToString();
//fire the update method

lblError.Text = spUsrProf.UpdateUserByLoginID(txtUserLoginID.Text);
}
catch (Exception ex)
{
//write error in screen

this.lblError.Text = ex.Message.ToString();
}
finally

{
spUsrProf = null;
}
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Dispose();
}
}
}
Hopefully, the above will help you :)
  1. November 11th, 2010 at 04:37

    this post is very usefull thx!

  2. January 21st, 2011 at 14:07

    But yeah Several thanks for taking the time to discuss this, I feel strongly about it and truly like studying a lot more on this subject. If possible, as you acquire expertise, would you thoughts updating your blog with much more data? It’s very helpful for me.

  3. February 25th, 2011 at 02:56

    Very interesting entry, I look forward to the next! Thx for share

You must be logged in to post a comment.