Wednesday, February 27, 2008

How to configure your MyInbox webpart automatically ?

Follow the autoConfig lab below to succeed the configuration for every user at runtime
1. Create a new Webpart Library project
2. Override the CreateChildControls
// import section
using Microsoft.SharePoint.Portal.WebControls;
// attributes
private OWAInboxPart wpInbox;
protected override void CreateChildControls()
{
// inbox webpart
wpInbox = new OWAInboxPart();
Controls.Add(configureInbox(wpInbox));
}
3. Create a new method to configure your Inbox webpart. This method will determine the mailboxname and the servername at runtime for a particular user.

private OWAInboxPart configureInbox(OWAInboxPart wpInbox)
{
//Connect to the portal and get the portal context.
TopologyManager topology = new TopologyManager();
PortalSite portal = topology.PortalSites[new Uri(http://servername)];
PortalContext context = PortalApplication.GetContext(portal);
//initialize user profile config manager object
UserProfileManager profileManager = new UserProfileManager(context);
UserProfile prof = profileManager.GetUserProfile(true);
// use the profile object to retrieve the properties you need in your company to
// retrieve the mailboxname
// example: string = workmail =proPropertyConstants.WorkEmail].ToString();
wpInbox.MailboxName = "kristofdc";
wpInbox.OWAServerAddressRoot = "http://servername/exchange";return wpInbox;
}
4. Override the RenderWebPart method
protected override void RenderWebPart(HtmlTextWriter output)
{
try
{
wpInbox.RenderControl(output);
}
catch(Exception ex)
{
output.Write(ex.ToString());
}}
5. And finally you will add a CAB-setup for your custom webpart.
6. Just compile, deploy and use !