ServerContext.Current reuturns null when site is being access from the client machine RRS feed

  • Question

  • Hi,

    In one of my sharepoint application I am accessing user profile properties using object model as follows:

    UserProfileManager profileManager = new UserProfileManager(ServerContext.Current);

    UserProfile user = profileManager.GetUserProfile(memberRecordID);

    This code works fine on the server where my site exists.

    But when i try to access the application from one client machine it gives following error:

    Value cannot be null. Parameter name: serverContext

    The operating system on the client machine is windows XP. Can anyone tell me what should I do to sort out this problem.

    Gaurav Dixit.

    Saturday, August 30, 2008 8:47 AM

All replies

  • Since you are useing Sharepoint I would recommend that you use the Sharepoint Objects, maybe that will work, try using SPContext.Current

    In order to make both contexts work, the IP-adress and the host name you should set both up in the Alternate Access Mappings section of the Administrative Site.
    Saturday, August 30, 2008 10:43 AM
  • You cannot use the SharePoint object model remotely. It will only work on a member of the SharePoint farm.

    You will need to use the User Profile web service instead: http://msdn.microsoft.com/en-us/library/ms494053.aspx.

    Posting is provided "AS IS" with no warranties, and confers no rights.
    Wednesday, September 3, 2008 7:03 PM
  • Hi Michael,

    The code is being run on the server only as from client machine it is being accessed using browser.

    Gaurav Dixit
    Wednesday, November 4, 2009 11:48 AM
  • Hi there.

    The correct syntax is:

    UserProfileManager p = new UserProfileManager(Microsoft.Office.Server.ServerContext.Default);
    (Notice the .Default property)

    Also, remember that you must have the required permissions to use the ProfileManager for other user accounts.


    My blog: InsomniacGeek.com
    Wednesday, November 4, 2009 11:55 AM
  • Gaurav,

    You don't have any problem if you use the code below:

    SPSite mySite = SPControl.GetContextSite(Context);  
    SPWeb myWeb = SPControl.GetContextWeb(Context);  
    ServerContext context = ServerContext.GetContext(mySite);  
    UserProfileManager myProfileManager = new UserProfileManager(context);  
    string CurrentUser = SPContext.Current.Web.CurrentUser.LoginName;  
    UserProfile myProfile = myProfileManager.GetUserProfile(CurrentUser);  
    if (myProfile[PropertyConstants.Title].Value != null)  
    oLabelTitle.Text = myProfile[PropertyConstants.Title].Value.ToString();  
    if (myProfile[PropertyConstants.FirstName].Value != null)  
    oLabelFirstName.Text = myProfile[PropertyConstants.FirstName].Value.ToString();  
    if (myProfile[PropertyConstants.LastName].Value != null)  
    oLabelLastname.Text = myProfile[PropertyConstants.LastName].Value.ToString();  

    Best Regards, G Vijai Kumar | My Sharepoint Blog
    Wednesday, November 4, 2009 12:00 PM
  • Hello Vijay,

    I am facing the same error of null returned by SPContext object.When i tried to use your code

    SPSite mySite = SPControl.GetContextSite(Context); 
    It returns error saying that Context does not exist.

    Can you kindly let me know how did you do it.

    Thank you,

    Friday, May 7, 2010 9:17 PM
  • Whenever the browser on the client and the browser on the server behave differently, check the AAM. See this blog for reference.



    Monday, May 10, 2010 4:44 PM
  • Try to use below code. This is working fine here

    ServerContext context = ServerContext.GetContext(SPContext.Current.Site);
    UserProfileManager profileManager = new UserProfileManager(context);
    UserProfile _CurrentUserUserProfile = profileManager.GetUserProfile(System.Web.HttpContext.Current.User.Identity.Name);

    lblPSNO.Text = (string)_CurrentUserUserProfile["UserName"].Value.ToString();
    lblFullName.Text = (string)_CurrentUserUserProfile["FirstName"].Value.ToString();

    Cheers, Hemendra-MCTS
    Tuesday, May 11, 2010 7:14 AM