Learning: solution to error in getting USERPROFILE through SSP

Source: Internet
Author: User

 

1, UseCodeWhen Moss SSP is called to obtain USERPROFILE, the code can only run under the moss site or winform. Otherwise, an error occurs. How can this problem be solved?

 

The Code is as follows:

SpsiteSite =New Spsite("Http: // ssjin073: 9031");

 //Obtain the context

Servercontext context = servercontext. getcontext (SITE );//. Getcontext (sspname );

// This. Context. items ["Microsoft. Office. servercontext"] = context;

Userprofilemanager_ Profilemanager;

_ Profilemanager =NewUserprofilemanager (context );

USERPROFILEU = _ profilemanager. getuserprofile ("Saictest" "zjy");

Response. Write (U. personalurl );

 

This code can only be run on the page of the moss site. If it is run on a common site or directly in the small IIS of Vs, the following error will be reported::

 

Error

The value cannot be blank.

Parameter Name: Servercontext

Description:Execute currentWebAn unprocessed exception occurred during the request. Check the stack trace information for details about the error and the source of the error in the code.

 

Exception details: System.Argumentnullexception:The value cannot be blank.

Parameter Name: Servercontext

 

Source Error:

Line46:USERPROFILEU = _ profilemanager. getuserprofile ("Saictest" "zjy");

Line47:

Line48:Response. Write (U. personalurl );

 

This error is caused by a moss bug. The internal code of sitecontext will call servercontext in the current context. If servercontext is not called, an error will occur.

 

We can solve this problem by manually adding context objects:

 SpsiteSite =New Spsite("Http: // ssjin073: 9031");

//Obtain the context

Servercontext context = servercontext. getcontext (SITE );//. Getcontext (sspname );

//Here we willServercontextPut Context

 This. Context. items ["Microsoft. Office. servercontext"] = Context;

 

Userprofilemanager_ Profilemanager;

_ Profilemanager =NewUserprofilemanager (context );

USERPROFILEU = _ profilemanager. getuserprofile ("Saictest" "zjy");

 

Response. Write (U. personalurl );

The above code can be run on any site!

 

Note:

1)Site ApplicationProgramThe account of the pool must have sufficient permissions or directly use the application pool of the moss site.

2)You need to add the following configuration under Web. config of the site:

<Identity impersonate = "true"/>

 

2After the permission is elevated, similar errors may occur during listitem operations, which can be solved in the same way:

 

Protected VoidPage_load (ObjectSender,EventargsE)

{

Spsecurity. Runwithelevatedprivileges (Delegate()

{

Updateitem ();

}

);

}

 

VoidUpdateitem ()

{

SpsiteSite =New Spsite("Http: // ssjin073: 9032");

SpwebWeb = site. rootweb;

//Force set context object

Httpcontext. Current. items ["Httphandlerspweb"] = Web;

 

Web. allowunsafeupdates =True;

SplistList = web. Lists ["Mainlist"];

 

SplistitemItem = List. items [0];

 

Item ["Title"] =Datetime. Now. tostring ();

 

Item. Update ();

 

Web. Dispose ();

Site. Dispose ();

}

 

 

ArticleSource: http://www.cnblogs.com/jianyi0115/archive/2008/04/11/1148827.html

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.