kamagra how much to take

Portal Context, User Profile Manager, and User Profile

I was looking at some code that I wrote in the past (I don’t even remember when!), interested in what classes might have been deprecated. One of the classes that I figured would be deprecated was the PortalContext class that provides run-time support for a portal site, however I was wrong, and it is still around. When I was writing profile management tools for clients, I used this class somewhat frequently in a combination with the UserProfileManager class. I found this code useful in order to iterate through the available user profiles, and return a set of usernames for the context.

Let me go through some of the code, and it will begin to make a little more sense, and maybe you find it helpful in some of your development.
There is some setup that is necessary firstly, no different when writing other WebParts. The most important is just to get the site context. This is going to be done by using the GetContextSite method that returns a reference to the site collection. Also, we should setup a currentUser using the SPUser class which represents a user in SharePoint.

  1. SPSite currentSite = SPControl.GetContextSite(Context);
  2. SPUser currentUser = null;

Next, there is the declaration of three things. A Boolean flag is necessary to provide logical interrogation of whether further iterations are necessary. For primarily instantiation, this is going to be declared as false. We are also going to instantiate an SPSite object and the PortalContext object and set it to null.

  1. bool iUPM = false;
  2. SPSite sSite = null;
  3. PortalContext cContext = null;

Following, we are going to set up an iteration for the portal context. If this proves successful, we can set the user profile iteration to true. This will later be consumed in another conditional statement. In the first statement, you can see the use of the MakeFullUrl method in order to make the server-relative URL a full URL.

  1. try
  2. {
  3. sSite = new SPSite(TheCurSite.MakeFullUrl(TheCurSite.PortalUrl));
  4. cContext = PortalApplication.GetContext(sSite.ID);
  5. iUPM = true;
  6. }

In the catch block, set the UPM iteration to false, and display a relevant errors that occurred during processing using Response.Write to push the relevant text to an HTTP stream.

  1. catch (Exception exception)
  2. {
  3. string disException = exception.Message.ToString();
  4. iUPM = false;
  5. this.Page.Response.Write("An Error Has Occurred:" + disException);
  6. }

Following is when you can start to work with the actual user profile manager. The UserProfileManager class allows the exposure of the relevant UserProfile objects that allow the surfacing of the relevant user profile information, a very powerful programmatic asset. We can use the profile iteration test that was setup before, and use it to construct around via an if statement.

  1. UserProfileManager upm = null;
  2. if (iUPM)
  3. {
  4. try
  5. {
  6. upm = new UserProfileManager(cContext);
  7. }
  8. catch
  9. {
  10. iUPM = false;
  11. }
  12. }

Now, you can get the relevant user profiles within a try/catch block.

  1. try
  2. {
  3. UserProfile currentUP = upm.GetUserProfile(currentUser.LoginName);
  4. }
  5. catch (Exception exception)
  6. {
  7. string disException = exception.Message.ToString();                    this.Page.Response.Write("An Error Has Occurred:" + disException);
  8. }

One Comment

  1. Andy says:

    Don’t forget to dispose of your SPSite object when you’re done with it – see http://msdn2.microsoft.com/en-us/library/ms778813.aspx

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>